12345678910111213141516171819202122232425262728293031323334353637383940 |
- import time
- import feedparser
- from flask import Flask, render_template, redirect
- from bs4 import BeautifulSoup
-
- # Source: https://lingohub.com/academy/best-practices/rtl-language-list
- RTL_LANGS = ['ar', 'arc', 'dv', 'fa', 'ha', 'he', 'khw', 'ks', 'ku', 'ps', 'ur', 'yi']
-
- application = Flask(__name__)
-
- @application.route('/')
- def redirect_to_repo():
- return redirect('https://nimrodkerrett.opalstacked.com/nimrodkerrett/mymastotag')
-
- @application.route('/<string:lang>/<string:instance>/<string:user>/<string:tag>')
- def my_masto_tag(lang, instance, user, tag):
- lang = lang.lower()
- is_rtl = lang in RTL_LANGS
- title = f'@{user}@{instance} — #{tag}'
- feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
- for e in feed['entries']:
- e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
- soup = BeautifulSoup(e['description'], 'html.parser')
- for link in soup.find_all('a'):
- link['target'] = '_blank'
- link['class'] = ['link-info', 'text-decoration-none']
- e['description'] = str(soup)
- images = []
- videos =[]
- for m in e.get('media_content', []):
- mtype = m['type'].split('/')[0]
- if mtype=='image':
- images.append(m)
- elif mtype=='video':
- videos.append(m)
- e['images'] = images
- e['has_images'] = not not images
- e['videos'] = videos
- e['has_videos'] = not not videos
- return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title, feed=feed)
|