MarseyWorld/files/routes/feeds.py

77 lines
2.1 KiB
Python
Raw Normal View History

2022-05-04 23:09:46 +00:00
import html
from .front import frontlist
from datetime import datetime
from files.helpers.get import *
from yattag import Doc
from files.helpers.wrappers import *
from files.helpers.jinja2 import *
from files.__main__ import app
2022-05-16 19:42:34 +00:00
@app.get('/rss')
@app.get('/feed')
2022-05-04 23:09:46 +00:00
@app.get('/rss/<sort>/<t>')
2022-05-16 19:41:19 +00:00
def feeds_user(sort='hot', t='all'):
2022-05-04 23:09:46 +00:00
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
ids, next_exists = frontlist(
sort=sort,
page=page,
t=t,
v=None,
)
posts = get_posts(ids)
domain = environ.get("DOMAIN").strip()
doc, tag, text = Doc().tagtext()
with tag("feed", ("xmlns:media","http://search.yahoo.com/mrss/"), xmlns="http://www.w3.org/2005/Atom",):
with tag("title", type="text"):
text(f"{sort} posts from {domain}")
with tag("id"):
text(SITE_FULL + request.full_path)
with tag("updated"):
text(datetime.now().isoformat()+"Z")
doc.stag("link", rel="self", type="application/atom+xml", href=SITE_FULL + request.full_path)
doc.stag("link", rel="alternate", type="text/html", href=SITE_FULL)
2022-05-04 23:09:46 +00:00
for post in posts:
with tag("entry", ("xml:base", SITE_FULL + request.full_path)):
with tag("title", type="html"):
2022-05-04 23:09:46 +00:00
text(post.realtitle(None))
with tag("id"):
text(post.permalink)
2022-05-04 23:09:46 +00:00
with tag("updated"):
if (post.edited_utc):
text(datetime.utcfromtimestamp(post.edited_utc).isoformat()+"Z")
else:
text(datetime.utcfromtimestamp(post.created_utc).isoformat()+"Z")
2022-05-04 23:09:46 +00:00
with tag("published"):
text(datetime.utcfromtimestamp(post.created_utc).isoformat()+"Z")
2022-05-04 23:09:46 +00:00
with tag("author"):
with tag("name"):
text(post.author_name)
with tag("uri"):
text(f'/@{post.author_name}')
doc.stag("link", href=post.permalink)
image_url = post.thumb_url or post.embed_url or post.url
doc.stag("media:thumbnail", url=image_url)
if len(post.body_html):
with tag("content", type="html"):
doc.cdata(f'''<img alt="{post.realtitle(None)}" loading="lazy" src="{image_url}"><br>{post.realbody(None)}''')
2022-05-04 23:09:46 +00:00
return Response( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ doc.getvalue(), mimetype="application/xml")