iframeable display of all posts by a matodon @user@instance account that contain a #hashtag
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mymastotag.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536
  1. import time
  2. import feedparser
  3. from flask import Flask, render_template
  4. from bs4 import BeautifulSoup
  5. # Source: https://lingohub.com/academy/best-practices/rtl-language-list
  6. RTL_LANGS = ['ar', 'arc', 'dv', 'fa', 'ha', 'he', 'khw', 'ks', 'ku', 'ps', 'ur', 'yi']
  7. application = Flask(__name__)
  8. @application.route('/<string:lang>/<string:instance>/<string:user>/<string:tag>')
  9. def my_masto_tag(lang, instance, user, tag):
  10. lang = lang.lower()
  11. is_rtl = lang in RTL_LANGS
  12. title = f'@{user}@{instance} &mdash; #{tag}'
  13. feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
  14. for e in feed['entries']:
  15. e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
  16. soup = BeautifulSoup(e['description'], 'html.parser')
  17. for link in soup.find_all('a'):
  18. link['target'] = '_blank'
  19. link['class'] = ['link-info', 'text-decoration-none']
  20. e['description'] = str(soup)
  21. images = []
  22. videos =[]
  23. for m in e['media_content']:
  24. mtype = m['type'].split('/')[0]
  25. if mtype=='image':
  26. images.append(m)
  27. elif mtype=='video':
  28. videos.append(m)
  29. e['images'] = images
  30. e['has_images'] = not not images
  31. e['videos'] = videos
  32. e['has_videos'] = not not videos
  33. return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title, feed=feed)