-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_headers.py
288 lines (260 loc) · 11.5 KB
/
media_headers.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import datetime
import requests
from header_surfer import HeadersSurfer
OTHER_MEDIA = ["the-washington-post", "the-telegraph", "the-new-york-times",
"the-huffington-post", "the-guardian-uk", "reuters", "newsweek",
"cnn", "cnbc", "business-insider", "breitbart-news", "bbc-news",
"al-jazeera-english"]
def load_key(key=None):
try:
with open('newsapi_key.txt', 'r') as my_key:
return my_key.read()
except FileNotFoundError:
if key:
with open('newsapi_key.txt', 'wb') as fhandler:
fhandler.write(key)
with open('newsapi_key.txt', 'r') as my_key:
return my_key.read()
else:
print("GIVE ME NORMAL NEWSAPI KEY!")
return
class TheEconomist(HeadersSurfer):
def __init__(self, handler=None):
HeadersSurfer.__init__(self, handler)
self.url = 'https://www.economist.com/'
self.name = 'The Economist'
self.limits = (300000, 450000)
self.article_url = ''
self.api_mode = False
try:
page_content = self.get_page(self.url, limits=self.limits)
headers_area = str(self.get_area(page_content, 'div', class_='hero-component'))
self.teaser_headers = self.get_header_blocks(headers_area, 'a', class_='teaser__link')
self.recent_headers = self.get_header_blocks(headers_area, 'a', class_='latest-updates-panel-card')
except IndexError:
self.api_mode = True
alert = 'SWITCHED TO NEWS API FOR THE ECONOMIST'
# print(alert)
if self.handler:
self.write_to_txt(alert + '\n')
# print(self.name, self.url)
if self.handler:
self.write_to_txt(self.name + '\n')
self.write_to_txt(self.url + '\n')
print(str(datetime.datetime.now()))
def process_headers(self):
if self.api_mode:
key = load_key()
url = 'https://newsapi.org/v1/articles?source=the-economist&sortBy=top&apiKey=' + key
response = requests.get(url).json()
articles = response['articles']
for article in articles:
article_url = (article.get('url', 'No url') + '\n')
title = (article.get('title', 'No title') + '\n')
try:
description = (article.get('description', 'No description') + '\n\n')
except TypeError:
description = 'No description\n\n'
if self.handler:
self.write_to_txt(article_url)
self.write_to_txt(title)
self.write_to_txt(description)
else:
for header in self.teaser_headers:
href = header.get('href')
header_area = str(header)
flytitle = self.get_text(header_area, 'span', class_='flytitle-and-title__flytitle')
title = self.get_text(header_area, 'span', class_='flytitle-and-title__title')
lead = self.get_lead(href)
# print('\n', self.article_url)
title = flytitle + ': ' + title
# print(title)
# print(lead)
if self.handler:
self.write_to_txt(self.article_url + '\n')
try:
self.write_to_txt(title + '\n')
except UnicodeDecodeError:
self.write_to_txt('Failed to write title\n')
try:
self.write_to_txt(lead + '\n\n')
except UnicodeDecodeError:
self.write_to_txt('Failed to write lead\n\n')
for header in self.recent_headers:
href = header.get('href')
header_area = str(header)
title = self.get_text(header_area, 'h3', class_='latest-updates-panel-card__title')
lead = self.get_lead(href)
# print('\n', self.article_url)
# print(title)
# print(lead)
if self.handler:
self.write_to_txt(self.article_url + '\n')
try:
self.write_to_txt(title + '\n')
except UnicodeDecodeError:
self.write_to_txt('Failed to write title\n')
try:
self.write_to_txt(lead + '\n\n')
except UnicodeDecodeError:
self.write_to_txt('Failed to write lead\n\n')
def get_lead(self, href):
self.article_url = self.url + href
article_page = self.get_page(self.article_url, limits=self.limits)
try:
lead = self.get_text(article_page, 'p', class_='blog-post__rubric')
except Exception as e:
print('Error:', e)
print(self.article_url)
lead = 'none'
return lead
class WallStreetJournal(HeadersSurfer):
def __init__(self, handler=None):
HeadersSurfer.__init__(self, handler)
self.url = u'https://www.wsj.com/europe'
self.name = u'Wall Street Journal (Europe)'
page_content = self.get_page(self.url)
headers_area = str(self.get_area(page_content, 'div', class_='lead-story'))
self.headers = self.get_header_blocks(headers_area, 'div', class_='wsj-card')
# print(str(datetime.datetime.now()))
# print(self.name, self.url)
if self.handler:
self.write_to_txt(self.name + '\n')
self.write_to_txt(self.url + '\n')
def process_headers(self):
for header in self.headers:
header_area = str(header)
article_url = self.get_href(header_area, 'a', class_='wsj-headline-link')
title = self.get_text(header_area, 'a', class_='wsj-headline-link')
try:
lead_area = self.get_area(header_area, 'p', class_='wsj-summary')
lead = lead_area.find('span').text
# print('\n', article_url)
# print(title)
# print(lead)
if self.handler:
self.write_to_txt(article_url + '\n')
try:
self.write_to_txt(title + '\n')
except UnicodeDecodeError:
self.write_to_txt('Failed to write title\n')
try:
self.write_to_txt(lead + '\n\n')
except UnicodeDecodeError:
self.write_to_txt('Failed to write lead\n\n')
# self.write_to_txt(title + '\n')
# self.write_to_txt(lead + '\n\n')
except IndexError:
pass
class Bloomberg(HeadersSurfer):
def __init__(self, handler=None):
HeadersSurfer.__init__(self, handler)
self.url = 'https://www.bloomberg.com/europe'
self.name = 'Bloomberg (Europe)'
self.article_url = ''
page_content = self.get_page(self.url)
top_headers_area = str(self.get_area(page_content, 'div', class_='hero-v6__stories'))
editorials_area = str(self.get_area(page_content, 'div', class_='home__highlights'))
self.top_headers = self.get_header_blocks(top_headers_area, 'div', class_='hero-v6-story')
self.editorials = self.get_header_blocks(editorials_area, 'a', class_='commentary-v6-story__headline-link')
# print(str(datetime.datetime.now()))
# print(self.name, self.url)
if self.handler:
self.write_to_txt(self.name + '\n')
self.write_to_txt(self.url + '\n')
def process_headers(self):
for header in self.top_headers:
try:
title_area = self.get_area(str(header), 'a', class_='hero-v6-story__headline-link')
title = title_area.text
url_area = self.get_area(str(header), 'a', class_='hero-v6-story__image')
href = url_area.get('href')
try:
lead = self.get_lead(href)
# print('\n', self.article_url)
# print(title)
# print(lead)
if self.handler:
self.write_to_txt(self.article_url + '\n')
# self.write_to_txt(title + '\n')
# self.write_to_txt(lead + '\n\n')
try:
self.write_to_txt(title + u'\n')
except UnicodeDecodeError:
self.write_to_txt(u'Failed to write title\n')
try:
self.write_to_txt(lead + u'\n\n')
except UnicodeDecodeError:
self.write_to_txt(u'Failed to write lead\n\n')
except Exception as e:
print('Error:', e)
print(self.article_url)
except IndexError:
pass
for header in self.editorials:
href = header.get('href')
title = header.text
lead = self.get_lead(href)
# print('\n', self.article_url)
# print(title)
# print(lead)
if self.handler:
self.write_to_txt(self.article_url + '\n')
# self.write_to_txt(title + '\n')
# self.write_to_txt(lead + '\n\n')
try:
self.write_to_txt(title + '\n')
except UnicodeDecodeError:
self.write_to_txt(u'Failed to write title\n')
try:
self.write_to_txt(lead + '\n\n')
except UnicodeDecodeError:
self.write_to_txt(u'Failed to write lead\n\n')
def get_lead(self, href):
self.article_url = href
article_page = self.get_page(self.article_url)
article_area = self.get_area(article_page, 'div', class_='body-copy')
paragraphs = article_area.find_all('p')
if paragraphs[0].find('em'):
lead = paragraphs[1].text
else:
lead = paragraphs[0].text
return lead
class Others(HeadersSurfer):
def __init__(self, handler=None):
HeadersSurfer.__init__(self, handler)
self.key = load_key()
self.url_base = 'https://newsapi.org/v1/articles?source={}&sortBy=top&apiKey=' + self.key
def walk_through(self):
for entry in OTHER_MEDIA:
# print(entry)
api_url = self.url_base.format(entry)
# print api_url
self.collect_data(api_url)
# print('DONE')
def collect_data(self, url):
response = requests.get(url).json()
source = (response['source'] + u'\n')
if self.handler:
self.write_to_txt(source)
articles = response['articles']
for article in articles:
url = (article.get('url', u'No url') + u'\n')
title = (article.get('title', u'No title') + u'\n')
try:
description = (article.get('description', u'No description') + u'\n\n')
except TypeError:
description = u'No description\n\n'
if self.handler:
self.write_to_txt(url)
self.write_to_txt(title)
self.write_to_txt(description)
if __name__ == '__main__':
economist = TheEconomist()
economist.process_headers()
bloomberg = Bloomberg()
bloomberg.process_headers()
wsj = WallStreetJournal()
wsj.process_headers()
others = Others()
others.walk_through()