-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnews_fetch.py
243 lines (208 loc) · 9.07 KB
/
news_fetch.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
''' Functions and code to automatically fetch and download news articles from google news.'''
import json
import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import re
import random
import pandas as pd
from datetime import datetime, timedelta
import calendar
from trafilatura import fetch_url, extract, html2txt
from trafilatura.meta import reset_caches
import urllib.parse
import feedparser
import html
class NewsFetcher:
def __init__(self):
'''initializes the news fetcher class to fetch data from the google news website'''
self.news_list_full = []
self.date_arr = []
self.news_output = []
self.rss_url = ''
self.json_feed = {}
def generate_weekly_dates(self, start_year, start_month, num_weeks):
date_arr = []
start_date = datetime(start_year, start_month, 1)
# Adjust start date to the first Monday of the month
if start_date.weekday() != 0:
start_date += timedelta(days=(7 - start_date.weekday()))
for i in range(num_weeks):
end_date = start_date + timedelta(days=6)
date_arr.append((start_date.date().strftime("%Y-%m-%d"), end_date.date().strftime("%Y-%m-%d")))
# print(f"Week {i + 1}: Start Date = {start_date.date()}, End Date = {end_date.date()}")
start_date += timedelta(days=7)
self.date_arr = date_arr
return None
def generate_monthly_dates(self, start_year, start_month, num_months):
date_arr = []
start_date = datetime(start_year, start_month, 1)
for i in range(num_months):
year = start_date.year
month = start_date.month
# Calculate the last day of the month
last_day = calendar.monthrange(year, month)[1]
end_date = datetime(year, month, last_day)
date_arr.append((start_date.date().strftime("%Y-%m-%d"), end_date.date().strftime("%Y-%m-%d")))
# Move to the first day of the next month
if month == 12:
start_date = datetime(year + 1, 1, 1)
else:
start_date = datetime(year, month + 1, 1)
self.date_arr = date_arr
return date_arr
def generate_yearly_dates(self, start_year, num_years):
date_arr = []
start_date = datetime(start_year, 1, 1)
for i in range(num_years):
year = start_date.year
# Start date is always January 1 of the current year
start_date = datetime(year, 1, 1)
# End date is always December 31 of the current year
end_date = datetime(year, 12, 31)
date_arr.append((start_date.date().strftime("%Y-%m-%d"), end_date.date().strftime("%Y-%m-%d")))
# Move to the first day of the next year
start_date = datetime(year + 1, 1, 1)
self.date_arr = date_arr
return date_arr
def get_news_data(self):
chrome_opt = webdriver.firefox.options.Options()
chrome_opt.add_argument('--headless')
chrome_opt.add_argument('--disable-notifications')
# chrome_opt.headless = True
service = Service()
browser = webdriver.Firefox(service=service, options=chrome_opt)
browser.set_page_load_timeout(30)
for news_element in self.news_output:
if 'contents' in news_element:
continue
elif 'contents' not in news_element or news_element['contents'] == None:
article_text = ''
try:
browser.get(news_element['link'])
print('Website has been opened!')
except:
assert 'A problem has occured'
time.sleep(5)
start_time = time.time()
page_loaded = False
while not page_loaded:
page_loaded = browser.execute_script("return document.readyState") == "complete"
if time.time() - start_time > 20:
print("Page load timed out after 20 seconds.")
break
try:
html = browser.page_source
bs = BeautifulSoup(html, 'html.parser')
p_data = ''
for data in bs.find_all('p'):
p_data = p_data + ' ' + str(data.get_text()).replace('\n', ' ')
# print(p_data)
except:
assert 'Error in extracting content!'
try:
news_element['contents'] = p_data
news_element['title'] = news_element['title'].encode('latin-1').decode('utf-8')
# news_article_data.append(news_element)
news_element['link'] = browser.current_url
except:
continue
# time.sleep(3)
browser.quit()
return None
def trif_fetch(self):
for article in self.news_output:
url = article['link']
try:
downloaded = fetch_url(url)
result = extract(downloaded)
except:
print('Error in fetching with trif!')
# print(result
if result != None:
article['contents'] = result
reset_caches()
return None
def get_gnews_rss_url(self, search_string, country_code="IN", language_code="en", time_period="", start_date=None, end_date=None):
base_url = "https://news.google.com/rss/search"
query_params = {
"q": search_string,
"hl": language_code,
"gl": country_code,
"ceid": f"{country_code}:{language_code}"
}
if start_date:
query_params["q"] += f" after:{start_date}"
if end_date:
query_params["q"] += f" before:{end_date}"
query_string = urllib.parse.urlencode(query_params)
self.rss_url = f"{base_url}?{query_string}"
return self.rss_url
def convert_unicode(self, text):
if all(ord(char) < 128 for char in text):
return text
else:
return bytes(text, 'utf-8').decode('unicode-escape')
def rss_to_json(self, encoding='utf-8'):
"""
Convert an RSS feed to JSON format.
Args:
rss_url (str): The URL of the RSS feed.
encoding (str, optional): The character encoding of the RSS feed. Default is 'utf-8'.
Returns:
dict: The RSS feed data in JSON format.
"""
feed = feedparser.parse(self.rss_url, response_headers={'Content-Type': f'application/rss+xml; charset={encoding}'})
json_feed = {
"feed": {
"title": html.unescape(self.convert_unicode(feed.feed.title)),
"description": html.unescape(self.convert_unicode(feed.feed.description)),
"link": feed.feed.link
},
"entries": []
}
for entry in feed.entries:
json_entry = {
"title": html.unescape(self.convert_unicode(entry.title)),
"link": entry.link,
"published": entry.published,
"summary": html.unescape(self.convert_unicode(entry.summary))
}
json_feed["entries"].append(json_entry)
self.news_output += json_feed["entries"]
return json_feed
def get_news_list(self):
return self.news_list_full
def add_full(self):
self.news_list_full += self.news_output
try:
self.news_output = []
except:
print("Error in emptying news output!")
newsFetcher = NewsFetcher()
df = pd.read_csv('states.csv')
date_arr = newsFetcher.generate_monthly_dates(2024, 1, 2)
for month in date_arr:
for index, row in df.iterrows():
search_string = (str(row['State']) + ' landslide reports').lower()
search_string= re.sub(r'\band\b', '', search_string)
# Remove extra spaces resulting from the removal
search_string= re.sub(r'\s+', ' ', search_string).strip()
gnews_rss_url = newsFetcher.get_gnews_rss_url(search_string, country_code='IN', language_code='en', start_date=month[0], end_date=month[1])
print(gnews_rss_url)
print(newsFetcher.rss_to_json())
newsFetcher.get_news_data()
newsFetcher.trif_fetch()
newsFetcher.add_full()
filename = 'landslide_news_data_test_1.json'
with open(filename, 'w') as json_file:
json.dump(newsFetcher.get_news_list(), json_file, indent=4)