iframeable display of all posts by a matodon @user@instance account that contain a #hashtag
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mymastotag.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import time
  2. import feedparser
  3. from flask import Flask, render_template, redirect, url_for
  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. og_image = None
  17. feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
  18. for e in feed['entries']:
  19. e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
  20. soup = BeautifulSoup(e['description'], 'html.parser')
  21. for link in soup.find_all('a'):
  22. link['target'] = '_blank'
  23. link['class'] = ['link-info', 'text-decoration-none']
  24. e['description'] = str(soup)
  25. images = []
  26. videos =[]
  27. for m in e.get('media_content', []):
  28. mtype = m['type'].split('/')[0]
  29. if mtype=='image':
  30. images.append(m)
  31. if not og_image:
  32. og_image = m.get('url')
  33. elif mtype=='video':
  34. videos.append(m)
  35. e['images'] = images
  36. e['has_images'] = not not images
  37. e['videos'] = videos
  38. e['has_videos'] = not not videos
  39. return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title, og_image=og_image or url_for('static', filename='hashtag.png', _external=True), feed=feed)