rDrama/files/routes/feeds.py

69 lines
1.9 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
import html
from .front import frontlist
from datetime import datetime
from files.helpers.get import *
from yattag import Doc
2022-01-11 21:53:49 +00:00
from files.helpers.wrappers import *
2022-01-28 21:06:31 +00:00
from files.helpers.jinja2 import *
2021-10-15 14:08:27 +00:00
from files.__main__ import app
@app.get('/rss/<sort>/<t>')
2022-01-11 21:54:41 +00:00
@auth_required
2022-01-11 21:53:49 +00:00
def feeds_user(v=None, sort='hot', t='all'):
2021-10-15 14:08:27 +00:00
2022-05-01 00:53:24 +00:00
try: page = max(int(request.values.get("page", 1)), 1)
2022-04-17 09:23:42 +00:00
except: page = 1
2021-10-15 14:08:27 +00:00
ids, next_exists = frontlist(
sort=sort,
page=page,
t=t,
v=None,
)
posts = get_posts(ids)
domain = environ.get("DOMAIN").strip()
2021-10-15 14:08:27 +00:00
doc, tag, text = Doc().tagtext()
2021-12-02 18:27:41 +00:00
with tag("feed", ("xmlns:media","http://search.yahoo.com/mrss/"), xmlns="http://www.w3.org/2005/Atom",):
2021-10-15 14:08:27 +00:00
with tag("title", type="text"):
text(f"{sort} posts from {domain}")
2022-01-28 21:42:09 +00:00
doc.stag("link", href=SITE_FULL + request.full_path)
doc.stag("link", href=SITE_FULL)
2021-10-15 14:08:27 +00:00
for post in posts:
2022-01-28 21:42:09 +00:00
with tag("entry", ("xml:base", SITE_FULL + request.full_path)):
2021-10-15 14:08:27 +00:00
with tag("title", type="text"):
2021-12-21 14:47:38 +00:00
text(post.realtitle(None))
2021-10-15 14:08:27 +00:00
with tag("id"):
text(post.fullname)
2022-01-12 01:19:13 +00:00
if (post.edited_utc):
2021-10-15 14:08:27 +00:00
with tag("updated"):
text(datetime.utcfromtimestamp(post.edited_utc).isoformat())
with tag("published"):
text(datetime.utcfromtimestamp(post.created_utc).isoformat())
with tag("author"):
with tag("name"):
2022-01-21 20:56:56 +00:00
text(post.author_name)
2021-10-15 14:08:27 +00:00
with tag("uri"):
2022-04-02 17:11:35 +00:00
text(f'/@{post.author_name}')
2021-10-15 14:08:27 +00:00
2022-01-28 20:55:59 +00:00
doc.stag("link", href=post.permalink)
2021-10-15 14:08:27 +00:00
image_url = post.thumb_url or post.embed_url or post.url
doc.stag("media:thumbnail", url=image_url)
2022-01-12 01:19:13 +00:00
if len(post.body_html):
2021-10-15 14:08:27 +00:00
with tag("content", type="html"):
2021-12-21 20:17:16 +00:00
doc.cdata(f'''<img alt="{post.realtitle(None)}" loading="lazy" src={image_url}><br>{post.realbody(None)}''')
2021-10-15 14:08:27 +00:00
2021-07-21 01:12:26 +00:00
return Response( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ doc.getvalue(), mimetype="application/xml")