neddit

the frontend of the frontpage of the internet  https://neddit.ce9e.org
git clone https://git.ce9e.org/neddit.git

commit
ae23a4f9d880a822d07decedfbf36d7d9ea161d8
parent
bd712bc63522d5e2d0e70befce8f9a55b0ac55be
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-06-28 07:03
support short links

Diffstat

M neddit.py 31 ++++++++++++++++++++++++++-----

1 files changed, 26 insertions, 5 deletions


diff --git a/neddit.py b/neddit.py

@@ -7,6 +7,7 @@ from pathlib import Path
    7     7 import aiohttp
    8     8 import jinja2
    9     9 from aiohttp import web
   -1    10 from yarl import URL
   10    11 
   11    12 BASE_DIR = Path(__file__).parent
   12    13 
@@ -72,6 +73,14 @@ def dash_resize(url):
   72    73 env.filters['dash_resize'] = dash_resize
   73    74 
   74    75 
   -1    76 def raise_for_status(response):
   -1    77     if response.status == 404:
   -1    78         raise web.HTTPNotFound
   -1    79     if response.status == 429:
   -1    80         raise web.HTTPServiceUnavailable
   -1    81     response.raise_for_status()
   -1    82 
   -1    83 
   75    84 async def fetch(url, **params):
   76    85     async with aiohttp.ClientSession() as session:
   77    86         async with session.get(
@@ -79,14 +88,23 @@ async def fetch(url, **params):
   79    88             headers={'User-agent': 'neddit'},
   80    89             params={**params, 'raw_json': 1},
   81    90         ) as response:
   82    -1             if response.status == 404:
   83    -1                 raise web.HTTPNotFound
   84    -1             if response.status == 429:
   85    -1                 raise web.HTTPServiceUnavailable
   86    -1             response.raise_for_status()
   -1    91             raise_for_status(response)
   87    92             return await response.json()
   88    93 
   89    94 
   -1    95 async def check_redirect(url):
   -1    96     async with aiohttp.ClientSession() as session:
   -1    97         async with session.get(
   -1    98             url,
   -1    99             headers={'User-agent': 'neddit'},
   -1   100             allow_redirects=False,
   -1   101         ) as response:
   -1   102             raise_for_status(response)
   -1   103             if 'Location' in response.headers:
   -1   104                 url = URL(response.headers['Location'])
   -1   105                 raise web.HTTPFound(location=url.path)
   -1   106 
   -1   107 
   90   108 @async_cache()
   91   109 async def fetch_subreddit(name):
   92   110     return await fetch(f'https://www.reddit.com/r/{name}/about.json')
@@ -105,6 +123,9 @@ async def generic(request):
  105   123         'params': request.query,
  106   124     }
  107   125 
   -1   126     if '/s/' in path:
   -1   127         await check_redirect(f'https://www.reddit.com/{path}/')
   -1   128 
  108   129     if path.startswith('r/'):
  109   130         subreddit = path.split('/')[1]
  110   131         context['subreddit'] = await fetch_subreddit(subreddit)