12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import time
- import feedparser
- from flask import Flask, render_template, redirect, url_for, request
- 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}'
- og_image = None
- tag_url = f'https://{instance}/@{user}/tagged/{tag}'
- feed = feedparser.parse(f'{tag_url}.rss')
- hashtag = '#' + tag.lower()
- if request.args.get('reverse',False):
- feed['entries'] = list(reversed(feed['entries']))
- 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'
- if link.text.lower()==hashtag:
- link['class'] = ['badge', 'rounded-pill', 'text-bg-info', 'text-decoration-none']
- else:
- 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)
- if not og_image:
- og_image = m.get('url')
- 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, tag_url=tag_url,
- og_image=og_image or url_for('static', filename='hashtag.png', _external=True),
- updated_time=int(time.time()), feed=feed)
|