feat: async fetch of statuses
This commit is contained in:
parent
0e126921ea
commit
3fa2767e49
3 changed files with 30 additions and 13 deletions
|
@ -2,3 +2,5 @@ flask
|
||||||
requests
|
requests
|
||||||
markdown
|
markdown
|
||||||
Flask-Caching
|
Flask-Caching
|
||||||
|
aiohttp
|
||||||
|
flask[async]
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
import TimeAgo from 'https://esm.sh/v135/@github/relative-time-element@4.4.0'
|
import TimeAgo from 'https://esm.sh/v135/@github/relative-time-element@4.4.0'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<link rel="stylesheet" href="/reset.css" />
|
<link rel="stylesheet" href="https://webcomponent.io/reset.css" />
|
||||||
<style>
|
<style>
|
||||||
html {
|
html {
|
||||||
scroll-behavior: smooth;
|
scroll-behavior: smooth;
|
||||||
|
|
35
threads.py
35
threads.py
|
@ -4,6 +4,8 @@ from datetime import datetime
|
||||||
import markdown
|
import markdown
|
||||||
import re
|
import re
|
||||||
from .cache import cache
|
from .cache import cache
|
||||||
|
import asyncio
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
threads = Blueprint('threads', __name__, template_folder='templates')
|
threads = Blueprint('threads', __name__, template_folder='templates')
|
||||||
|
|
||||||
|
@ -44,8 +46,8 @@ def middleware():
|
||||||
attribution['current_year'] = year
|
attribution['current_year'] = year
|
||||||
|
|
||||||
@threads.route('/')
|
@threads.route('/')
|
||||||
def home():
|
async def home():
|
||||||
statuses = fetch_statuses()
|
statuses = await fetch_statuses()
|
||||||
return render_template('threads.html', threads=statuses, app=app, attribution=attribution, render_date=datetime.now())
|
return render_template('threads.html', threads=statuses, app=app, attribution=attribution, render_date=datetime.now())
|
||||||
|
|
||||||
@threads.route('/<path:id>')
|
@threads.route('/<path:id>')
|
||||||
|
@ -60,21 +62,34 @@ def thread(id):
|
||||||
return '<h1>Not Found</h1><p>¯\_(ツ)_/¯</p><a href="/">go home</a>', 404
|
return '<h1>Not Found</h1><p>¯\_(ツ)_/¯</p><a href="/">go home</a>', 404
|
||||||
|
|
||||||
@threads.route('/api')
|
@threads.route('/api')
|
||||||
def api():
|
async def api():
|
||||||
return fetch_statuses();
|
return await fetch_statuses();
|
||||||
|
|
||||||
@threads.route('/api/<path:id>')
|
@threads.route('/api/<path:id>')
|
||||||
def api_thread(id):
|
def api_thread(id):
|
||||||
return fetch_thread(id)
|
return fetch_thread(id)
|
||||||
|
|
||||||
@cache.cached(timeout=300)
|
async def get(url, session):
|
||||||
def fetch_statuses():
|
try:
|
||||||
|
async with session.get(url=url) as response:
|
||||||
|
res = await response.json()
|
||||||
|
return clean_status(res)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unable to get url {url} due to {e.__class__}")
|
||||||
|
|
||||||
|
def get_status_url(ser, id):
|
||||||
|
return f'{ser}/api/v1/statuses/{id}'
|
||||||
|
|
||||||
|
# @cache.cached(timeout=300)
|
||||||
|
async def fetch_statuses():
|
||||||
statuses = []
|
statuses = []
|
||||||
for id in thread_ids:
|
urls = [get_status_url(server, id) for id in thread_ids]
|
||||||
status = requests.get(server + '/api/v1/statuses/' + id ).json()
|
try:
|
||||||
status = clean_status(status)
|
async with aiohttp.ClientSession() as session:
|
||||||
statuses.append(status)
|
statuses = await asyncio.gather(*(get(url, session) for url in urls))
|
||||||
return statuses
|
return statuses
|
||||||
|
except:
|
||||||
|
return []
|
||||||
|
|
||||||
@cache.cached(timeout=300)
|
@cache.cached(timeout=300)
|
||||||
def fetch_thread(id):
|
def fetch_thread(id):
|
||||||
|
|
Loading…
Reference in a new issue