feat: use app config

This commit is contained in:
Ayo Ayco 2024-06-02 09:11:52 +02:00
parent 2ab2a0ddbc
commit a452a60c9c
4 changed files with 38 additions and 16 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
config.json
# Deployment Unix socket # Deployment Unix socket
*.sock *.sock

3
app.py
View file

@ -1,11 +1,12 @@
from flask import Flask from flask import Flask
import json
from .threads import threads from .threads import threads
from .cache import cache from .cache import cache
app = Flask(__name__) app = Flask(__name__)
cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache'}) cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache'})
app.register_blueprint(threads, url_prefix='/') app.register_blueprint(threads, url_prefix='/')
app.config.from_file("config.json", load=json.load)
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='0.0.0.0') app.run(host='0.0.0.0')

14
example_config.json Normal file
View file

@ -0,0 +1,14 @@
{
"APPS": {
"threads": {
"site_name": "Thoughts",
"title":"Thoughts",
"description": "Hand-picked public posts from my social feed",
"server" : "https://social.ayco.io"
}
},
"ATTRIBUTION": {
"owner": "Author Name",
"year": "2024"
}
}

View file

@ -1,4 +1,4 @@
from flask import Blueprint, render_template from flask import Blueprint, render_template, current_app
import requests import requests
from datetime import datetime from datetime import datetime
import markdown import markdown
@ -10,7 +10,6 @@ import aiohttp
threads = Blueprint('threads', __name__, template_folder='templates') threads = Blueprint('threads', __name__, template_folder='templates')
# TODO: move following to an app config or sqlite ######### # TODO: move following to an app config or sqlite #########
server = 'https://social.ayco.io'
thread_ids = [ thread_ids = [
'112524983806134679', '112524983806134679',
'112461583113763423', '112461583113763423',
@ -26,23 +25,22 @@ thread_ids = [
# TODO: implement pagination # TODO: implement pagination
# '109545132056133905' # '109545132056133905'
] ]
app = {
"site_name": "ayco.io/threads",
"title":"Ayo's Threads",
"description": "Incubator for thoughts before they become a blog."
}
attribution = {
"owner": "Ayo Ayco",
"year": "2022" # earliest year in featured posts
}
########################################################### ###########################################################
def get_attribution():
return current_app.config['ATTRIBUTION']
def get_app_config():
return current_app.config['APPS']['threads']
@threads.before_request @threads.before_request
def middleware(): def middleware():
# check current year and put ange as attribution # check current year and put ange as attribution
currentDateTime = datetime.now() currentDateTime = datetime.now()
date = currentDateTime.date() date = currentDateTime.date()
year = date.strftime("%Y") year = date.strftime("%Y")
attribution = get_attribution()
if year != attribution['year']: if year != attribution['year']:
attribution['current_year'] = year attribution['current_year'] = year
@ -50,12 +48,16 @@ def middleware():
@cache.cached(timeout=300) @cache.cached(timeout=300)
async def home(): async def home():
statuses = await fetch_statuses() statuses = await fetch_statuses()
attribution = get_attribution()
app = get_app_config()
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>')
@cache.cached(timeout=300) @cache.cached(timeout=300)
def thread(id): def thread(id):
if id in thread_ids: if id in thread_ids:
attribution = get_attribution()
app = get_app_config()
status = fetch_thread(id) status = fetch_thread(id)
status['summary'] = clean_html(status['content']).strip() status['summary'] = clean_html(status['content']).strip()
if len(status['summary']) > 69: if len(status['summary']) > 69:
@ -85,9 +87,12 @@ async def get(url, session):
def get_status_url(ser, id): def get_status_url(ser, id):
return f'{ser}/api/v1/statuses/{id}' return f'{ser}/api/v1/statuses/{id}'
def server():
return current_app.config['APPS']['threads']['server']
async def fetch_statuses(): async def fetch_statuses():
statuses = [] statuses = []
urls = [get_status_url(server, id) for id in thread_ids] urls = [get_status_url(server(), id) for id in thread_ids]
try: try:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
statuses = await asyncio.gather(*(get(url, session) for url in urls)) statuses = await asyncio.gather(*(get(url, session) for url in urls))
@ -96,14 +101,14 @@ async def fetch_statuses():
return None return None
def fetch_thread(id): def fetch_thread(id):
status = requests.get(server + '/api/v1/statuses/' + id ).json() status = requests.get(server() + '/api/v1/statuses/' + id ).json()
status = clean_status(status) status = clean_status(status)
status['descendants'] = get_descendants(server, status) status['descendants'] = get_descendants(server(), status)
return status return status
def get_descendants(server, status): def get_descendants(server, status):
author_id = status['account']['id'] author_id = status['account']['id']
context = requests.get(server + '/api/v1/statuses/' + status['id'] + '/context').json() context = requests.get(server() + '/api/v1/statuses/' + status['id'] + '/context').json()
descendants = [] descendants = []
for reply in context['descendants']: for reply in context['descendants']:
# TODO: the following condition will include a reply to a reply of the author # TODO: the following condition will include a reply to a reply of the author