feat: create flask blueprint
This commit is contained in:
parent
9eddcb790b
commit
08de6b89b6
3 changed files with 116 additions and 83 deletions
46
app.py
46
app.py
|
@ -1,49 +1,9 @@
|
||||||
from flask import Flask, render_template
|
from flask import Flask
|
||||||
import requests
|
from threads import threads
|
||||||
|
|
||||||
app = Flask(__name__)
|
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__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0')
|
app.run(host='0.0.0.0')
|
||||||
|
|
|
@ -4,25 +4,46 @@
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Ayo's Threads</title>
|
<title>Ayo's Threads</title>
|
||||||
|
<link rel="stylesheet" href="https://webcomponent.io/reset.css" />
|
||||||
<style>
|
<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 {
|
ul {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
}
|
}
|
||||||
.card {
|
.card {
|
||||||
display:block;
|
display:block;
|
||||||
margin-left: -40px;
|
margin-left: -40px;
|
||||||
margin-bottom: 15px;
|
margin-top: 1em;
|
||||||
border: 1px solid rgba(34, 34, 34, 0.15);
|
border: 1px solid rgba(34, 34, 34, 0.15);
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
padding:1em;
|
padding:1em;
|
||||||
max-width: 500px;
|
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
box-shadow: 5px 25px 10px -25px rgba(34, 34, 34, 0.15);
|
box-shadow: 5px 25px 10px -25px rgba(34, 34, 34, 0.15);
|
||||||
}
|
}
|
||||||
.card_meta {
|
.card_meta {
|
||||||
font-size: small; color: #888;
|
font-size: small; color: #888;
|
||||||
}
|
}
|
||||||
|
|
||||||
.card_content {
|
.card_content {
|
||||||
|
& * {
|
||||||
|
margin-top: 1em;
|
||||||
|
}
|
||||||
& img {
|
& img {
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
|
@ -33,45 +54,49 @@
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ayo's Threads</h1>
|
<header>
|
||||||
<p>Total: {{threads | length}}</p>
|
<h1>Ayo's Threads</h1>
|
||||||
<ul>
|
<p>Total: {{threads | length}}</p>
|
||||||
{% for thread in threads %}
|
</header>
|
||||||
<li class="card">
|
<main>
|
||||||
<div class="card_meta">
|
<ul>
|
||||||
<span>{{ thread.created_at }}</span> • <a href="{{thread.url}}" target="_blank">↗</a>
|
{% for thread in threads %}
|
||||||
</div>
|
<li class="card">
|
||||||
<div class="card_content">
|
<div class="card_meta">
|
||||||
{{thread.content | safe}}
|
<span>{{ thread.created_at }}</span> ∙ <a href="{{thread.url}}" target="_blank">↗</a>
|
||||||
|
</div>
|
||||||
|
<div class="card_content">
|
||||||
|
{{thread.content | safe}}
|
||||||
|
|
||||||
{% for media in thread.media_attachments%}
|
{% for media in thread.media_attachments%}
|
||||||
{% if media.type == 'image'%}
|
{% if media.type == 'image'%}
|
||||||
<img src="{{media.url}}" />
|
<img src="{{media.url}}" />
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
<details>
|
|
||||||
<summary style="cursor:pointer">See more</summary>
|
|
||||||
<ul>
|
|
||||||
{% for descendant in thread.descendants %}
|
|
||||||
<li class="card">
|
|
||||||
<div class="card_meta">
|
|
||||||
<span>{{ descendant.created_at }}</span> • <a href="{{descendant.url}}" target="_blank">↗</a>
|
|
||||||
</div>
|
|
||||||
<div class="card_content">
|
|
||||||
{{ descendant.content | safe }}
|
|
||||||
{% for media in descendant.media_attachments%}
|
|
||||||
{% if media.type == 'image'%}
|
|
||||||
<img src="{{media.url}}" />
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
<li>
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</div>
|
||||||
</details>
|
<details>
|
||||||
</li>
|
<summary>See more</summary>
|
||||||
{% endfor %}
|
<ul>
|
||||||
</ul>
|
{% for descendant in thread.descendants %}
|
||||||
|
<li class="card">
|
||||||
|
<div class="card_meta">
|
||||||
|
<span>{{ descendant.created_at }}</span> • <a href="{{descendant.url}}" target="_blank">↗</a>
|
||||||
|
</div>
|
||||||
|
<div class="card_content">
|
||||||
|
{{ descendant.content | safe }}
|
||||||
|
{% for media in descendant.media_attachments%}
|
||||||
|
{% if media.type == 'image'%}
|
||||||
|
<img src="{{media.url}}" />
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</details>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
48
threads.py
Executable file
48
threads.py
Executable 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}
|
||||||
|
|
Loading…
Reference in a new issue