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

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