-
Notifications
You must be signed in to change notification settings - Fork 17
/
news.py
executable file
·263 lines (213 loc) · 8.16 KB
/
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
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
"""
Greynir: Natural language processing for Icelandic
Copyright (C) 2023 Miðeind ehf.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
News-related routes
"""
from __future__ import annotations
from typing import List, Optional, Union
from flask.wrappers import Response
from . import routes, max_age, better_jsonify
from datetime import datetime, timedelta, timezone
from flask import request, render_template
from settings import changedlocale
from db import Session, SessionContext, desc
from db.models import Article, Root, Location, ArticleTopic, Topic
# Default number of top news items to show in /news
_DEFAULT_NUM_ARTICLES = 20
_MAX_NUM_ARTICLES = 100
def fetch_articles(
topic: Optional[str]=None,
offset: int=0,
limit: int=_DEFAULT_NUM_ARTICLES,
start: Optional[datetime]=None,
location: Optional[str]=None,
country: Optional[str]=None,
root: Optional[str]=None,
author: Optional[str]=None,
enclosing_session: Optional[Session]=None,
):
"""Return a list of articles in chronologically reversed order.
Articles can be filtered by start date, location, country, root etc."""
toplist: List[ArticleDisplay] = []
now = datetime.now(timezone.utc)
with SessionContext(read_only=True, session=enclosing_session) as session:
q = (
session.query(Article)
.filter(Article.tree != None)
.filter(Article.timestamp != None)
.filter(Article.timestamp <= now)
.filter(Article.heading > "")
.filter(Article.num_sentences > 0)
.join(Root)
.filter(Root.visible == True)
)
# Filter by date
if start is not None:
q = q.filter(Article.timestamp > start)
if location or country:
q = q.join(Location)
if location:
# Filter by location
q = q.filter(Location.name == location)
if country:
# Filter by country code
q = q.filter(Location.country == country)
# Filter by source (root) using domain (e.g. "kjarninn.is")
if root:
q = q.filter(Root.domain == root)
# Filter by author name
if author:
q = q.filter(Article.author == author)
# Filter by topic identifier
if topic:
q = q.join(ArticleTopic).join(Topic).filter(Topic.identifier == topic)
q = (
q.order_by(desc(Article.timestamp))
.offset(offset)
.limit(limit)
)
class ArticleDisplay:
"""Utility class to carry information about an article to the web template"""
def __init__(
self,
heading: str,
timestamp: datetime,
url: str,
uuid: str,
num_sentences: int,
num_parsed: int,
icon: str,
localized_date: str,
source: str,
):
self.heading = heading
self.timestamp = timestamp
self.url = url
self.uuid = uuid
self.num_sentences = num_sentences
self.num_parsed = num_parsed
self.icon = icon
self.localized_date = localized_date
self.source = source
@property
def width(self) -> str:
"""The ratio of parsed sentences to the total number of sentences,
expressed as a percentage string"""
if self.num_sentences == 0:
return "0%"
return "{0}%".format((100 * self.num_parsed) // self.num_sentences)
@property
def time(self) -> str:
return self.timestamp.isoformat()[11:16]
@property
def date(self) -> str:
now = datetime.now(timezone.utc)
if now.year == self.timestamp.year:
return self.localized_date
return self.fulldate
@property
def fulldate(self) -> str:
return self.localized_date + self.timestamp.strftime(" %Y")
with changedlocale(category="LC_TIME"):
for a in q:
# Instantiate article objects from results
source = a.root.domain
icon = source + ".png"
locdate = a.timestamp.strftime("%-d. %b")
d = ArticleDisplay(
heading=a.heading,
timestamp=a.timestamp,
url=a.url,
uuid=a.id,
num_sentences=a.num_sentences,
num_parsed=a.num_parsed,
icon=icon,
localized_date=locdate,
source=source,
)
toplist.append(d)
return toplist
@routes.route("/news")
@max_age(seconds=60)
def news() -> Union[str, Response]:
"""Handler for a page with a list of articles + pagination"""
topic = request.args.get("topic")
root = request.args.get("root")
author = request.args.get("author")
try:
offset = max(0, int(request.args.get("offset", 0)))
limit = max(0, int(request.args.get("limit", _DEFAULT_NUM_ARTICLES)))
except:
offset = 0
limit = _DEFAULT_NUM_ARTICLES
limit = min(limit, _MAX_NUM_ARTICLES) # Cap at max 100 results per page
now = datetime.now(timezone.utc)
with SessionContext(read_only=True) as session:
# Fetch articles
articles = fetch_articles(
topic=topic,
offset=offset,
limit=limit,
root=root,
author=author,
enclosing_session=session,
)
# If all articles in the list are timestamped within 24 hours of now,
# we display their times in HH:MM format. Otherwise, we display full date.
display_time = True
if articles and (now - articles[-1].timestamp).days >= 1:
display_time = False
# Fetch lists of article topics
q = session.query(Topic.identifier, Topic.name).order_by(Topic.name).all()
d = {t[0]: t[1] for t in q}
topics = dict(id=topic, name=d.get(topic, ""), topic_list=q)
# Fetch list of article sources (roots)
q = (
session.query(Root.domain, Root.description)
.filter(Root.visible == True)
.order_by(Root.description)
)
roots = dict(q.all())
return render_template(
"news.html",
title="Fréttir",
articles=articles,
topics=topics,
display_time=display_time,
offset=offset,
limit=limit,
selected_root=root,
roots=roots,
author=author,
)
ARTICLES_LIST_MAXITEMS = 50
@routes.route("/articles", methods=["GET"])
def articles_list() -> Response:
"""Returns rendered HTML article list as a JSON payload"""
locname = request.args.get("locname")
country = request.args.get("country")
period = request.args.get("period")
days = 7 if period == "week" else 1
start_date = datetime.now(timezone.utc) - timedelta(days=days)
# Fetch articles
articles = fetch_articles(
start=start_date,
location=locname,
country=country,
limit=ARTICLES_LIST_MAXITEMS,
)
# Render template
count = len(articles)
html = render_template("articles.html", articles=articles)
# Return payload
return better_jsonify(payload=html, count=count)