-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnews_automator.py
More file actions
95 lines (81 loc) · 2.91 KB
/
news_automator.py
File metadata and controls
95 lines (81 loc) · 2.91 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import requests
from sys import argv
import time
API_KEY = '5c39dc84da9a48799329249151d25228'
URL = 'https://newsapi.org/v2/everything'
def get_articles_by_category(category):
query_parameters = {
"category": category,
"sortBy": "top",
"country": "gb",
"apiKey": API_KEY
}
return _get_articles(query_parameters)
def get_articles_by_query(query):
query_parameters = {
"q": query,
"sortBy": "publishedAt",
"language": "en",
"apiKey": API_KEY
}
return _get_articles(query_parameters)
def _get_articles(params):
max_retries = 3
timeout = 10 # seconds
for attempt in range(max_retries):
try:
response = requests.get(URL, params=params, timeout=timeout)
if response.status_code == 200:
articles = response.json().get('articles', [])
results = [{"title": article["title"], "url": article["url"]} for article in articles]
return results
else:
print(f"Error: Unable to fetch articles. Status code: {response.status_code}")
except requests.exceptions.Timeout:
print("Error: Request timed out. Retrying...")
except requests.exceptions.RequestException as e:
print(f"Error: An error occurred. {e}")
time.sleep(2) # wait before retrying
return []
def get_sources_by_category(category):
url = 'https://newsapi.org/v2/top-headlines/sources'
query_parameters = {
"category": category,
"language": "en",
"apiKey": API_KEY
}
max_retries = 3
timeout = 10 # seconds
for attempt in range(max_retries):
try:
response = requests.get(url, params=query_parameters, timeout=timeout)
if response.status_code == 200:
sources = response.json().get('sources', [])
for source in sources:
print(source['name'])
print(source['url'])
return
else:
print(f"Error: Unable to fetch sources. Status code: {response.status_code}")
except requests.exceptions.Timeout:
print("Error: Request timed out. Retrying...")
except requests.exceptions.RequestException as e:
print(f"Error: An error occurred. {e}")
time.sleep(2) # wait before retrying
print("No sources found.")
if __name__ == "__main__":
if len(argv) > 1:
category = argv[1]
print(f"Getting news for {category}...\n")
articles = get_articles_by_category(category)
else:
query = "ChatGPT"
print(f"Getting news for query: {query}...\n")
articles = get_articles_by_query(query)
if articles:
for article in articles:
print(article['title'])
print(article['url'])
print('')
else:
print("No articles found.")