-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatest_news.py
182 lines (138 loc) · 5.78 KB
/
latest_news.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
from flask import Flask, render_template
from bs4 import BeautifulSoup
from datetime import *
import requests
import re
app = Flask(__name__)
def get_latest_news():
## TECHRADAR ##
# Get techradar news page
page = requests.get("https://www.techradar.com/uk/news")
soup = BeautifulSoup(page.content, "html.parser")
# Get article name, synopsis from techradar
latest_news_title = soup.find_all("h3", class_="article-name")
latest_news_synop = soup.find_all("p", class_="synopsis")
latest_news_link = []
# For each link, get aria-label and href attributes value
for item in soup.find_all("a", class_="article-link"):
if item.has_attr("aria-label") and item.has_attr("href"):
latest_news_link.append({
"label": item["aria-label"],
"link": item["href"]
})
latest_news = []
# For each article name, add a dictionary with the title of news article and which site its from
for i in range(0, len(latest_news_title)):
latest_news.append({
"title": latest_news_title[i].text,
"publisher": "TechRadar"
})
# For each article synopsis, add a synopsis key to the dictionary
for i in range(0, len(latest_news_synop)):
latest_news[i]["synop"] = latest_news_synop[i].text
# Add link for each article to latest_news
count = 0
for i in range(0, len(latest_news_link)):
if latest_news_link[i]["label"] == latest_news[count]["title"]:
latest_news[count]["link"] = latest_news_link[i]["link"]
count += 1
if count == len(latest_news):
break
# Get time articles were posted
latest_time = soup.find_all("time")
# Add time posted to latest_news
for i in range(0, len(latest_news)):
posted_time = latest_time[i]["datetime"].split("T")
date = posted_time[0].split("-")
time = re.split(":|Z", posted_time[1])
latest_news[i]["year"] = date[0]
latest_news[i]["month"] = date[1]
latest_news[i]["day"] = date[2]
latest_news[i]["hour"] = time[0]
latest_news[i]["minute"] = time[1]
latest_news[i]["dt"] = datetime(
year=int(latest_news[i]["year"]),
month=int(latest_news[i]["month"]),
day=int(latest_news[i]["day"]),
hour=int(latest_news[i]["hour"]),
minute=int(latest_news[i]["minute"])
)
## TECHCRUNCH ##
page = requests.get("https://techcrunch.com/")
soup = BeautifulSoup(page.content, "html.parser")
crunch_latest_news = []
# Get title, link from techcrunch latest articles
for item in soup.find_all("a", class_="post-block__title__link"):
if item.has_attr("href"):
crunch_latest_news.append({
"title": item.text,
"link": item["href"],
"publisher": "TechCrunch"
})
# Get synopsis
crunch_latest_synop = soup.find_all("div", class_="post-block__content")
# Add synopsis to crunch latest news
for i in range(0, len(crunch_latest_synop)):
crunch_latest_news[i]["synop"] = crunch_latest_synop[i].text
# Get time posted
crunch_latest_time = soup.find_all("time")
# Add time posted to crunch_latest_news
for i in range(0, len(crunch_latest_time)):
posted_time = crunch_latest_time[i]["datetime"].split("T")
date = posted_time[0].split("-")
time = re.split(":|-", posted_time[1])
crunch_latest_news[i]["year"] = date[0]
crunch_latest_news[i]["month"] = date[1]
crunch_latest_news[i]["day"] = date[2]
crunch_latest_news[i]["hour"] = time[0]
crunch_latest_news[i]["minute"] = time[1]
crunch_latest_news[i]["dt"] = datetime(
year=int(crunch_latest_news[i]["year"]),
month=int(crunch_latest_news[i]["month"]),
day=int(crunch_latest_news[i]["day"]),
hour=int(crunch_latest_news[i]["hour"]),
minute=int(crunch_latest_news[i]["minute"])
)
# Append crunch_latest_news items to latest_news
for i in crunch_latest_news:
latest_news.append(i)
# Remove latest news without date and append to no_date, if title is not included/is whitespace remove completely
no_date = []
count = 0
while count < len(latest_news):
if "title" not in latest_news[count] or latest_news[count]["title"].isspace():
latest_news.pop(count)
elif "dt" not in latest_news[count]:
no_date.append(latest_news.pop(count))
else:
count += 1
# Sort latest_news by date
latest_news.sort(key = lambda x:x["dt"])
# Add news with no date back to the end of latest_news
for i in no_date:
latest_news.append(i)
return latest_news
@app.route("/")
def index():
latest_news = get_latest_news()
return render_template("news_index.html", latest_news=latest_news)
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
@app.route("/sort/newest")
def sort_newest():
# Sort latest_news by newest date (default)
latest_news = get_latest_news()
return render_template("news_index.html", latest_news=latest_news)
@app.route("/sort/title")
def sort_title():
# Sort latest_news by title in ascending order
latest_news = sorted(get_latest_news(), key=lambda x: x["title"])
return render_template("news_index.html", latest_news=latest_news)
@app.route("/sort/publisher")
def sort_publisher():
# Sort latest_news by publisher in ascending order
latest_news = sorted(get_latest_news(), key=lambda x: x["publisher"])
return render_template("news_index.html", latest_news=latest_news)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=80)