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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import time
  2. import feedparser
  3. from flask import Flask, render_template, redirect
  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('/')
  9. def redirect_to_repo():
  10. return redirect('https://nimrodkerrett.opalstacked.com/nimrodkerrett/mymastotag')
  11. @application.route('/<string:lang>/<string:instance>/<string:user>/<string:tag>')
  12. def my_masto_tag(lang, instance, user, tag):
  13. lang = lang.lower()
  14. is_rtl = lang in RTL_LANGS
  15. title = f'@{user}@{instance} &mdash; #{tag}'
  16. feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
  17. for e in feed['entries']:
  18. e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
  19. soup = BeautifulSoup(e['description'], 'html.parser')
  20. for link in soup.find_all('a'):
  21. link['target'] = '_blank'
  22. link['class'] = ['link-info', 'text-decoration-none']
  23. e['description'] = str(soup)
  24. images = []
  25. videos =[]
  26. for m in e.get('media_content', []):
  27. mtype = m['type'].split('/')[0]
  28. if mtype=='image':
  29. images.append(m)
  30. elif mtype=='video':
  31. videos.append(m)
  32. e['images'] = images
  33. e['has_images'] = not not images
  34. e['videos'] = videos
  35. e['has_videos'] = not not videos
  36. return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title, feed=feed)