-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (52 loc) · 2.17 KB
/
Copy pathapp.py
File metadata and controls
67 lines (52 loc) · 2.17 KB
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
# app.py
import os
import sqlite3
from flask import Flask, Response
from feedgen.feed import FeedGenerator
from content_encoded_extension import ContentEncodedExtension
from dccreator_extension import DCCreatorEntryExtension # Import the custom extension
# Load environment variables
FEED_CHANNELS_ENV = os.getenv('FEED_CHANNELS')
BASE_DOMAIN = os.getenv('BASE_URL')
if not FEED_CHANNELS_ENV or not BASE_DOMAIN:
print('Please set the FEED_CHANNELS and BASE_URL environment variables.')
exit(1)
# Parse FEED_CHANNELS environment variable into a list of feed names
FEED_NAMES = []
try:
for pair in FEED_CHANNELS_ENV.split(','):
name, _ = pair.split('=')
FEED_NAMES.append(name.strip())
except ValueError:
print('Error parsing FEED_CHANNELS environment variable. Please use the format feedName=channelID,anotherFeedName=anotherChannelID')
exit(1)
# Initialize Flask app
app = Flask(__name__)
# Initialize SQLite database
conn = sqlite3.connect('messages.db', check_same_thread=False)
c = conn.cursor()
@app.route('/<feed_name>')
def rss_feed(feed_name):
if feed_name not in FEED_NAMES:
return Response(f'Feed "{feed_name}" not found.', status=404)
fg = FeedGenerator()
fg.title(f'{feed_name} News Feed')
fg.link(href=f'{BASE_DOMAIN}/{feed_name}', rel='self')
fg.description(f'A RSS feed Discord Channel Mirror "{feed_name}"')
c.execute("SELECT title, content, link, pubDate, author FROM messages WHERE feed_name = ? ORDER BY pubDate DESC", (feed_name,))
msgs = c.fetchall()
print(f"Showing {len(msgs)} messages to response of feed '{feed_name}'.")
for msg in msgs:
fe = fg.add_entry()
fe.register_extension('content', ContentEncodedExtension)
fe.register_extension('dccreator', DCCreatorEntryExtension)
fe.title(msg[0])
fe.pubDate(msg[3])
# Set the creator using the custom extension
fe.dccreator.set_creator(msg[4])
# Set the 'content:encoded' element
fe.content.set_content_encoded(msg[1])
rssfeed = fg.rss_str(pretty=True)
return Response(rssfeed, mimetype='application/rss+xml')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)