74 lines
2.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from enum import Enum
from secrets import token_urlsafe
from flask import Flask, Response, render_template
from flask import request
from mastodon_toot_follower import mastodon_util, path_util
from mastodon_toot_follower.mastodon_factory import MastodonFactory as MastodonFactory
from mastodon_toot_follower.conversation.conversation import Conversation
# Create Flask's `app` object
app = Flask(
__name__,
instance_relative_config=False,
template_folder=str(path_util.get_templates_dir()),
static_folder=str(path_util.get_static_dir()),
)
mastodon_factory = MastodonFactory()
class Templates(Enum):
index = 'index.html'
updates = 'updates.html'
@app.route('/', methods=['GET'])
def hello_world():
return render_template(Templates.index.value, seed=token_urlsafe(16))
@app.route('/rss/')
def rss():
url = request.args.get('url', 'None')
instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url)
conversation = Conversation(mastodon_factory=mastodon_factory, mastodon_instance=instance_url, toot_id=toot_id)
fg = conversation.as_feed(request.url)
return Response(fg.rss_str(), mimetype='application/rss+xml')
@app.route('/html/<string:seed>/')
def html(seed: str):
url = request.args.get('url', 'None')
instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url)
conversation = Conversation(mastodon_factory=mastodon_factory,
mastodon_instance=instance_url,
toot_id=toot_id)
reading_position = conversation.get_and_update_reading_position(user_id=seed)
return render_template(Templates.updates.value, updates=conversation.updates(reading_position=reading_position))
@app.route('/json/<string:seed>/')
def json(seed: str):
url = request.args.get('url', 'None')
instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url)
conversation = Conversation(mastodon_factory=mastodon_factory,
mastodon_instance=instance_url,
toot_id=toot_id)
reading_position = conversation.get_and_update_reading_position(user_id=seed)
# If you return a dict or list from a view, it will be converted to a JSON response.
return [update.dict for update in conversation.updates(reading_position=reading_position)]
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)