forked from vivcheng01/daily-dose-of-news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews_api.py
38 lines (28 loc) · 1.11 KB
/
news_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
''' Uses NewsAPI to fetch headlines.
https://newsapi.org/sources
For more info see https://newsapi.org/docs/endpoints/top-headlines
'''
from newsapi import NewsApiClient
import random
class NewsAPIClient:
def __init__(self, NEWS_API_TOKEN):
self.newsapi = NewsApiClient(api_key=NEWS_API_TOKEN )
def get_headlines(self, category, country):
print('Your Daily Dose of News')
# https://replicate.com/laion-ai/ongo
# https://replicate.com/mehdidc/feed_forward_vqgan_clip
resp = self.newsapi.get_top_headlines(category=category,
# language='en',
country=country)
# No headlines found
if not resp['articles']:
return None
article = random.choice(resp['articles'])
src = ''
try:
headline, src = article['title'].split('-')
except:
headline = article['title']
url = article['url']
description=article['description']
return headline, src, url, description