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

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