forked from bbusenius/Re-Feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
618 lines (502 loc) · 20 KB
/
app.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import html
import os
import subprocess
from datetime import datetime
import click
import feedparser
import requests
from flask import (
flash,
jsonify,
redirect,
render_template,
request,
send_from_directory,
url_for,
)
from flask_login import (
LoginManager,
UserMixin,
current_user,
login_required,
login_user,
logout_user,
)
from flask_wtf import FlaskForm
from flask_wtf.csrf import CSRFError, CSRFProtect
from ftfy import fix_text
from wtforms import PasswordField, StringField, SubmitField
from wtforms.validators import DataRequired, Length
from _config import app, db
from functions import (
get_change_by_id,
get_entries_by_tag_or_not,
rfc_3339_date,
update_or_create_change,
)
from models import FeedEntry, Tag
app.secret_key = app.config['SECRET_KEY']
csrf = CSRFProtect(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
class User(UserMixin):
"""Represents a user in the system.
Attributes:
id (str): The unique identifier for the user, set to the username.
"""
def __init__(self, username):
self.id = username
class TagForm(FlaskForm):
"""
Form for adding tags.
Attributes:
tags (StringField): Required field for entering tags.
submit (SubmitField): Button to submit the form.
"""
tags = StringField('Tags', validators=[DataRequired(), Length(max=50)])
submit = SubmitField('Add Tag')
class LoginForm(FlaskForm):
"""
A form for user login.
This form collects the username and password from the user and includes
a submit button to initiate the login process. Both fields are required
for form submission.
Attributes:
username (StringField): The field for the user's username.
password (PasswordField): The field for the user's password.
submit (SubmitField): The button to submit the form.
"""
username = StringField('Username', validators=[DataRequired(), Length(max=20)])
password = PasswordField('Password', validators=[DataRequired(), Length(max=100)])
submit = SubmitField('Login')
@login_manager.user_loader
def load_user(user_id):
"""Load a user by their unique identifier.
Args:
user_id (str): The unique identifier of the user to load.
Returns:
User: An instance of the User class corresponding to the user_id.
"""
return User(user_id)
def fetch_rss_feed():
"""Fetch and store RSS feed entries in the database.
This function retrieves the RSS feed from the URL specified in the
application configuration. It parses the feed, reverses the order of
the entries, and adds any new entries to the database if they do not
already exist. Each entry is stored with its title, feed ID, link,
publication date, and description.
If the RSS_FEED_URL is not configured, a message is printed to the
console indicating the missing configuration.
Returns:
None
"""
with app.app_context():
if not app.config['RSS_FEED_URL']:
print('Missing RSS_FEED_URL in config.')
feed = feedparser.parse(app.config['RSS_FEED_URL'])
feed.entries.reverse()
for entry in feed.entries:
existing_entry = FeedEntry.query.filter_by(feed_id=entry.id).first()
if not existing_entry: # Only add if it doesn't exist
title = html.escape(fix_text(entry.title, unescape_html=False))
rss_entry = FeedEntry(
title=title,
feed_id=entry.id,
link=entry.link,
published_at=datetime.strptime(
entry.published, app.config['RSS_PUBLISHED_AT_FORMAT']
),
description=entry.get('description', ''),
)
db.session.add(rss_entry)
db.session.commit()
def fetch_json_feed():
"""Fetch and store JSON feed entries in the database.
This function retrieves the JSON feed from the URL specified in the
application configuration. It sends a GET request to the JSON_FEED_URL
and, if the response is successful (HTTP status code 200), it parses
the JSON data. The function reverses the order of the entries and
adds any new entries to the database if they do not already exist.
Each entry is stored with its title, feed ID, link, publication date,
and description.
If the response status code is not 200, an error message is printed
indicating the failure to fetch the JSON feed. If there is a problem
with the URL (e.g., missing schema), a message is printed to the
console.
Returns:
None
"""
with app.app_context():
try:
response = requests.get(app.config['JSON_FEED_URL'])
if response.status_code == 200:
json_data = response.json()
json_data['data'].reverse()
for item in json_data['data']:
existing_entry = FeedEntry.query.filter_by(
feed_id=item['id']
).first()
if not existing_entry: # Only add if it doesn't exist
feed_entry = FeedEntry(
title=fix_text(item['title']),
feed_id=item['id'],
link=item['url'],
published_at=datetime.strptime(
item['date_utc'], app.config['JSON_PUBLISHED_AT_FORMAT']
),
description=item.get('description', ''),
)
db.session.add(feed_entry)
db.session.commit()
else:
print(f"Failed to fetch JSON feed: {response.status_code}")
except requests.exceptions.MissingSchema:
print('Missing or bad URL in config.')
@app.route('/', methods=['GET', 'POST'])
def login():
"""Handles user login and authentication.
This function supports both local authentication for development
and Okta authentication based on the environment configuration.
If the user is already authenticated, they are redirected to the
admin page. On a POST request, it checks the provided username
and password against the development credentials. If they match,
a user session is created and the user is redirected to the admin
page. If the credentials are invalid, an error message is flashed.
Returns:
Response: The rendered template for the login page or a
redirect to the admin page.
"""
try:
dev_username = app.config['DEV_USERNAME']
dev_password = app.config['DEV_PASSWORD']
except KeyError:
return render_template(
'index.html', logo=app.config['LOGO'], f_logo=app.config['FOOTER_LOGO']
)
if current_user.is_authenticated:
return redirect(url_for('admin'))
form = LoginForm()
# No Okta, local authentication for development
if request.method == 'POST' and form.validate_on_submit():
username = form.username.data
password = form.password.data
if username == dev_username and password == dev_password:
user_obj = User(username)
login_user(user_obj) # This creates a session for the user
return redirect(url_for('admin'))
else:
flash('Invalid username or password')
# Okta
if app.config['AUTH_MODE'] == 'okta':
# Authenticate with Okta
pass
return render_template(
'index.html', logo=app.config['LOGO'], f_logo=app.config['FOOTER_LOGO']
)
@app.route('/logout')
@login_required
def logout():
"""Logs out the current user and redirects to the login page.
This function terminates the user session and ensures that the
user is logged out. It is protected by the `login_required`
decorator, which means only authenticated users can access it.
Returns:
Response: A redirect to the login page.
"""
logout_user()
return redirect(url_for('login'))
@app.route('/admin')
@login_required
def admin():
"""Render the index page with a list of feed entries.
This function retrieves all feed entries from the database, reverses
their order, and renders the 'index.html' template. The template is
populated with the list of entries and an optional logo specified in
the application configuration.
Returns:
str: The rendered HTML template for the index page.
"""
form = TagForm()
custom_css = os.path.join(app.static_folder, 'custom.css')
custom_css_exists = os.path.exists(custom_css)
entries = FeedEntry.query.all()
entries.reverse()
return render_template(
'admin.html',
entries=entries,
logo=app.config['LOGO'],
f_logo=app.config['FOOTER_LOGO'],
has_custom_css=custom_css_exists,
tag_form=form,
)
@app.route('/static/<path:path>')
def send_static(path):
"""Serve static files from the 'static' directory.
Args:
path (str): The relative path to the static file.
Returns:
Response: The requested static file.
"""
return send_from_directory('static', path)
@app.errorhandler(CSRFError)
def handle_csrf_error(e):
"""Handle CSRF (Cross-Site Request Forgery) errors.
This function is triggered when a CSRFError is raised in the application.
It renders an error template with a message describing the error and
includes a logo from the application configuration.
Args:
e (CSRFError): The CSRFError instance containing details about the error.
Returns:
str: Rendered HTML of the error template with the error message and logo.
"""
return render_template(
'error.html',
msg=e.description,
logo=app.config['LOGO'],
f_logo=app.config['FOOTER_LOGO'],
)
@app.route('/tag_entry/<int:entry_id>', methods=['POST'])
def tag_entry(entry_id):
"""Add a tag to a feed entry.
This function retrieves a feed entry by its ID and adds a tag to it.
If the tag does not exist, it creates a new tag. The tag name is
taken from the form data, converted to lowercase, and stripped of
whitespace.
Args:
entry_id (int): The ID of the feed entry to tag.
Returns:
Response:
- If the entry is found and the tag is successfully added,
it returns a JSON response containing the tag ID if the
request was made via AJAX. Otherwise, if the request was
a normal POST (non-AJAX), it redirects to the admin index
page.
- If the entry is not found, it returns an error page with
a message indicating the entry was not found.
- If the form validation fails, it returns an error response
in JSON format for AJAX requests or renders an error page
for non-AJAX requests, indicating the validation issue.
Raises:
Exception: Any exceptions raised during database operations
will be handled by the Flask error handling mechanism, which
may return a generic error response.
"""
is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest'
form = TagForm()
if not form.validate_on_submit():
error_msg = 'The form did not validate. Your tag name is probably too long.'
if is_ajax:
return jsonify({'error': error_msg}), 400
return render_template(
'error.html',
msg=error_msg,
logo=app.config['LOGO'],
f_logo=app.config['FOOTER_LOGO'],
)
tag_name = form.tags.data.strip().lower()
entry = db.session.get(FeedEntry, entry_id)
if entry:
tag = Tag.query.filter(Tag.name.ilike(tag_name)).first()
if not tag: # If the tag doesn't exist, create it
tag = Tag(name=tag_name)
db.session.add(tag)
db.session.commit() # Commit to get the tag ID
if tag not in entry.tags:
entry.tags.append(tag)
db.session.commit()
update_or_create_change(1)
if is_ajax:
return jsonify(tag.id)
return redirect(url_for('admin'))
return render_template(
'error.html',
msg='Entry not found!',
logo=app.config['LOGO'],
f_logo=app.config['FOOTER_LOGO'],
)
@app.route('/delete_tag/<int:entry_id>/<int:tag_id>', methods=['POST', 'DELETE'])
def delete_tag(entry_id, tag_id):
"""Remove a tag from a feed entry.
This function retrieves a feed entry and a tag by their IDs. If both
exist and the tag is associated with the entry, the tag is removed.
If the tag is not associated with the entry or if either the entry
or tag is not found, an error message is rendered.
Args:
entry_id (int): The ID of the feed entry.
tag_id (int): The ID of the tag to remove.
Returns:
Response: A redirect to the index page or an error page if the
entry or tag is not found, or if the tag is not associated with
the entry.
"""
entry = db.session.get(FeedEntry, entry_id)
tag = db.session.get(Tag, tag_id)
if entry and tag:
# Remove the tag from the entry
if tag in entry.tags:
entry.tags.remove(tag)
db.session.commit()
update_or_create_change(1)
return redirect(url_for('admin'))
else:
return render_template(
'error.html',
msg='Tag not associated with this entry!',
logo=app.config['LOGO'],
f_logo=app.config['FOOTER_LOGO'],
)
return render_template(
'error.html',
msg='Entry or tag not found!',
logo=app.config['LOGO'],
f_logo=app.config['FOOTER_LOGO'],
)
@app.route('/get_feed_rss', methods=['GET'])
@app.route('/get_feed_rss/tag/<string:tag_name>', methods=['GET'])
@app.route('/get_feed_rss/tag/<string:tag_name>/<int:limit>', methods=['GET'])
@app.route('/get_feed_rss/<int:limit>', methods=['GET'])
def get_feed_rss(tag_name=None, limit=None):
"""Generate an RSS feed of feed entries.
This function retrieves feed entries, optionally filtered by a tag
name and/or limited to a specified number of entries. It constructs
and returns an RSS feed in XML format.
Args:
tag_name (str, optional): The name of the tag to filter entries.
limit (int, optional): The maximum number of entries to return.
Returns:
tuple: A tuple containing the RSS feed as a string, an HTTP status
code (200), and a content type of 'application/rss+xml'.
"""
entries = get_entries_by_tag_or_not(tag_name, limit)
feed = ''
feed += '<?xml version="1.0" encoding="utf-8" ?>\n'
feed += '<rss version="2.0">\n'
feed += '<channel>\n'
for entry in entries:
feed += '<item>\n'
feed += f'<title>{entry.title}</title>\n'
feed += f'<link>{entry.link}</link>\n'
feed += f'<description>{entry.description}</description>\n'
for tag in entry.tags:
feed += f'<category term="{tag.name}"/>\n'
feed += '</item>\n'
feed += '</channel>\n</rss>'
return feed, 200, {'Content-Type': 'application/rss+xml'}
@app.route('/get_feed_atom', methods=['GET'])
@app.route('/get_feed_atom/tag/<string:tag_name>', methods=['GET'])
@app.route('/get_feed_atom/tag/<string:tag_name>/<int:limit>', methods=['GET'])
@app.route('/get_feed_atom/<int:limit>', methods=['GET'])
def get_feed_atom(tag_name=None, limit=None):
"""Generate an Atom feed of feed entries.
This function retrieves feed entries, optionally filtered by a tag
name and/or limited to a specified number of entries. It constructs
and returns an Atom feed in XML format, including metadata such as
the feed title and last updated time.
Args:
tag_name (str, optional): The name of the tag to filter entries.
limit (int, optional): The maximum number of entries to return.
Returns:
tuple: A tuple containing the Atom feed as a string, an HTTP status
code (200), and a content type of 'application/atom+xml'.
"""
try:
updated = rfc_3339_date(get_change_by_id(1).updated)
except AttributeError:
updated = rfc_3339_date(update_or_create_change(1).updated)
feed_title = app.config['FEED_TITLE']
entries = get_entries_by_tag_or_not(tag_name, limit)
feed = ''
feed += '<?xml version="1.0" encoding="utf-8" ?>\n'
feed += '<feed xmlns="http://www.w3.org/2005/Atom">\n'
feed += f'<title type="html">{feed_title}</title>\n'
feed += f'<id>{request.base_url}</id>\n'
feed += f'<updated>{updated}</updated>\n'
for entry in entries:
feed += '<entry>\n'
feed += f'<title type="html">{entry.title}</title>\n'
feed += f'<id>{entry.id}</id>\n'
feed += f'<link href="{entry.link}" rel="alternate" type="text/html"/>\n'
feed += f'<content type="html"><![CDATA[ {entry.description} ]]></content>\n'
for tag in entry.tags:
feed += f'<category term="{tag.name}"/>\n'
feed += '</entry>\n'
feed += '</feed>\n'
return feed, 200, {'Content-Type': 'application/rss+xml'}
@app.route('/get_feed_json', methods=['GET'])
@app.route('/get_feed_json/tag/<string:tag_name>', methods=['GET'])
@app.route('/get_feed_json/tag/<string:tag_name>/<int:limit>', methods=['GET'])
@app.route('/get_feed_json/<int:limit>', methods=['GET'])
def get_feed_json(tag_name=None, limit=None):
"""Generate a JSON feed of feed entries.
This function retrieves feed entries, optionally filtered by a tag
name and/or limited to a specified number of entries. It constructs
and returns a JSON representation of the feed entries.
Args:
tag_name (str, optional): The name of the tag to filter entries.
limit (int, optional): The maximum number of entries to return.
Returns:
Response: A JSON response containing a list of feed entries.
"""
entries = get_entries_by_tag_or_not(tag_name, limit)
feed_data = []
for entry in entries:
entry_data = {
'title': entry.title,
'link': entry.link,
'description': entry.description,
'tags': [tag.name for tag in entry.tags],
}
feed_data.append(entry_data)
return jsonify(feed_data)
@app.route('/refresh_feed', methods=['POST'])
def refresh_feed():
"""Refresh the feed based on the configured FETCH_MODE."""
fetch_mode = app.config.get('FETCH_MODE', 'json').lower()
if fetch_mode not in ['json', 'rss']:
flash('Invalid feed type configured. Must be "json" or "rss".')
return redirect(url_for('admin'))
try:
subprocess.run(['flask', 'fetch_feed', fetch_mode], check=True)
except subprocess.CalledProcessError:
flash(f'Failed to refresh {fetch_mode} feed.')
return redirect(url_for('admin'))
@app.cli.command('fetch_feed')
@click.argument('mode')
def fetch_feed(mode):
"""Fetch and import feed entries into the SQLite database.
This command fetches entries from a specified feed type (JSON or RSS)
and imports them into the SQLite database. The mode argument determines
the type of feed to fetch.
Args:
mode (str): The type of feed to fetch. Must be either "json" or "rss".
Usage:
To fetch a JSON feed:
flask fetch_feed json
To fetch an RSS feed:
flask fetch_feed rss
"""
mode = mode.lower()
if mode == 'json':
fetch_json_feed()
elif mode == 'rss':
fetch_rss_feed()
else:
print(
'You must specify what kind of a feed to fetch. Only "json" and "rss" are recognized.'
)
# Load optional overrides
try:
from custom_functions import * # noqa
except ImportError:
pass
with app.app_context():
db.create_all()
if __name__ == '__main__':
with app.app_context():
# Fetch feeds when the app starts
if app.config['FETCH_MODE'].lower() == 'json':
fetch_json_feed()
else:
fetch_rss_feed()
app.run(debug=True)