-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
87 lines (70 loc) · 2.18 KB
/
main.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
import feedparser
from operator import itemgetter
import time
import httpx
from io import BytesIO
import asyncio
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Modify to Suit - List of Allowed Origins
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Used to Test for Slow Feeds Locally
check_feed_performance = False
feedlist = []
parsedfeeds = []
posts = []
list_of_post_dicts = []
with open("feeds.txt", "r") as feed_file:
feed_lines = feed_file.readlines()
for line in feed_lines:
feedlist.append(line.strip())
async def get_feed(feedurl):
if check_feed_performance:
start_request = time.perf_counter()
try:
async with httpx.AsyncClient() as client:
feed_response = await client.get(feedurl, timeout=5.0)
content = BytesIO(feed_response.content)
feed = feedparser.parse(content)
parsedfeeds.append(feed)
if check_feed_performance:
end_request = time.perf_counter()
print(f"{end_request - start_request} - {feedurl}")
except Exception:
pass
async def process_feeds():
parsedfeeds.clear()
posts.clear()
list_of_post_dicts.clear()
feed_responses = await asyncio.gather(*map(get_feed, feedlist))
for parsedfeed in parsedfeeds:
for entries in parsedfeed.entries:
posts.append((entries.title, entries.published_parsed, entries.description))
# Fresher the Better
posts.sort(key=itemgetter(1))
posts.reverse()
# Convert the Whole Mess to JSON
for post in posts:
post_time = time.strftime('%H:%M:%S %d-%m-%Y ', post[1])
post_dict = {
'time': post_time,
'title': post[0],
'link': post[2],
'description': post[3]
}
list_of_post_dicts.append(post_dict)
return list_of_post_dicts
# Only Using a Single Route - Can Be Freely Changed
@app.get("/rss")
async def rss():
json = await process_feeds()
return JSONResponse(content=json)