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 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. hashtag = '#' + tag.lower()
  19. for e in feed['entries']:
  20. e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
  21. soup = BeautifulSoup(e['description'], 'html.parser')
  22. for link in soup.find_all('a'):
  23. link['target'] = '_blank'
  24. if link.text.lower()==hashtag:
  25. link['class'] = ['badge', 'rounded-pill', 'text-bg-info', 'text-decoration-none']
  26. else:
  27. link['class'] = ['link-info', 'text-decoration-none']
  28. e['description'] = str(soup)
  29. images = []
  30. videos =[]
  31. for m in e.get('media_content', []):
  32. mtype = m['type'].split('/')[0]
  33. if mtype=='image':
  34. images.append(m)
  35. if not og_image:
  36. og_image = m.get('url')
  37. elif mtype=='video':
  38. videos.append(m)
  39. e['images'] = images
  40. e['has_images'] = not not images
  41. e['videos'] = videos
  42. e['has_videos'] = not not videos
  43. return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title,
  44. og_image=og_image or url_for('static', filename='hashtag.png', _external=True),
  45. updated_time=int(time.time()), feed=feed)