feat: create flask blueprint

This commit is contained in:
Ayo Ayco 2024-04-24 11:32:36 +02:00
parent 9eddcb790b
commit 08de6b89b6
3 changed files with 116 additions and 83 deletions

46
app.py
View file

@ -1,49 +1,9 @@
from flask import Flask, render_template
import requests
from flask import Flask
from threads import threads
app = Flask(__name__)
app.register_blueprint(threads, url_prefix='/')
server = 'https://social.ayco.io'
thread_ids = ['112294405672971916', '112258065967208438']
@app.route('/')
def home():
threads = fetch_threads();
return render_template('index.html', threads=threads)
@app.route('/api')
def api():
threads = fetch_threads();
return threads;
def fetch_threads():
threads = []
for id in thread_ids:
status = requests.get(server + '/api/v1/statuses/' + id ).json()
status = clean_status(status)
status['descendants'] = get_descendants(server, status)
threads.append(status)
return threads
def get_descendants(server, status):
author_id = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json()
descendants = []
for reply in context['descendants']:
if reply['account']['id'] == author_id and reply['in_reply_to_account_id'] == author_id:
descendants.append(clean_status(reply))
return descendants
def clean_author(account):
return clean_dict(account, ['avatar', 'display_name', 'id'])
def clean_status(status):
clean = clean_dict(status, ['id', 'content', 'created_at', 'url', 'media_attachments', 'card'])
clean['account'] = clean_author(status['account'])
return clean
def clean_dict(dict, keys):
return {k: dict[k] for k in keys}
if __name__ == '__main__':
app.run(host='0.0.0.0')

View file

@ -4,25 +4,46 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ayo's Threads</title>
<link rel="stylesheet" href="https://webcomponent.io/reset.css" />
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
max-width: 570px;
margin: 0 auto;
padding: 1em;
}
header {
background: linear-gradient(45deg, #3054bf, #416fff);
color: white;
width: 100%;
padding: 1em;
border-radius: 5px;
}
summary {
cursor: pointer;
margin-top: 1em;
}
ul {
list-style: none;
}
.card {
display:block;
margin-left: -40px;
margin-bottom: 15px;
margin-top: 1em;
border: 1px solid rgba(34, 34, 34, 0.15);
border-radius: 5px;
padding:1em;
max-width: 500px;
background-color: #fff;
box-shadow: 5px 25px 10px -25px rgba(34, 34, 34, 0.15);
}
.card_meta {
font-size: small; color: #888;
}
.card_content {
& * {
margin-top: 1em;
}
& img {
border-radius: 5px;
max-width: 100%;
@ -33,13 +54,16 @@
</style>
</head>
<body>
<header>
<h1>Ayo's Threads</h1>
<p>Total: {{threads | length}}</p>
</header>
<main>
<ul>
{% for thread in threads %}
<li class="card">
<div class="card_meta">
<span>{{ thread.created_at }}</span> &bullet; <a href="{{thread.url}}" target="_blank">&nearr;</a>
<span>{{ thread.created_at }}</span> &#8729; <a href="{{thread.url}}" target="_blank">&nearr;</a>
</div>
<div class="card_content">
{{thread.content | safe}}
@ -51,7 +75,7 @@
{% endfor %}
</div>
<details>
<summary style="cursor:pointer">See more</summary>
<summary>See more</summary>
<ul>
{% for descendant in thread.descendants %}
<li class="card">
@ -73,5 +97,6 @@
</li>
{% endfor %}
</ul>
</main>
</body>
</html>

48
threads.py Executable file
View file

@ -0,0 +1,48 @@
from flask import Blueprint, render_template
import requests
threads = Blueprint('threads', __name__, template_folder='template')
server = 'https://social.ayco.io'
thread_ids = ['112319729193615365', '112294405672971916', '112258065967208438']
@threads.route('/')
def home():
threads = fetch_threads();
return render_template('index.html', threads=threads)
@threads.route('/api')
def api():
threads = fetch_threads();
return threads;
def fetch_threads():
threads = []
for id in thread_ids:
status = requests.get(server + '/api/v1/statuses/' + id ).json()
status = clean_status(status)
status['descendants'] = get_descendants(server, status)
threads.append(status)
return threads
def get_descendants(server, status):
author_id = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json()
descendants = []
for reply in context['descendants']:
if reply['account']['id'] == author_id and reply['in_reply_to_account_id'] == author_id:
descendants.append(clean_status(reply))
return descendants
def clean_author(account):
return clean_dict(account, ['avatar', 'display_name', 'id'])
def clean_status(status):
clean = clean_dict(status, ['id', 'content', 'created_at', 'url', 'media_attachments', 'card'])
clean['account'] = clean_author(status['account'])
return clean
def clean_dict(dict, keys):
return {k: dict[k] for k in keys}