From 238961799ddeb9db08a750d49a23fc32ea877aaf Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Fri, 8 Mar 2024 17:11:38 +0000 Subject: [PATCH] feat(backend): add rejectdopamine proxy --- backend/aci/proxy.py | 37 +++++++++++++++++++++++++++++++++++++ backend/aci/utils.py | 5 ++++- backend/app.py | 3 +++ flake.nix | 2 ++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 backend/aci/proxy.py diff --git a/backend/aci/proxy.py b/backend/aci/proxy.py new file mode 100644 index 0000000..7b7363b --- /dev/null +++ b/backend/aci/proxy.py @@ -0,0 +1,37 @@ +import aiohttp + +from datetime import datetime + +from fastapi import APIRouter + +from .utils import format_checkin_date + +router = APIRouter(prefix='/3p-proxy') + +def reformat_reject_date(date: str): + return format_checkin_date(datetime.strptime(date, '%a %b %d %Y')) + +@router.get('/reject') +async def reject_proxy(date: str, time: str, activity: str, space: str): + """We proxy the rejectdopamine API to maintain user pivacy (the extension does not transmit user IP addresses to third-party services)""" + async with aiohttp.ClientSession(headers={'User-Agent': 'Mozilla 5.0 (compatible); aci-backend/1.0 (+https://git.ashhhleyyy.dev/ash/aci)'}) as session: + async with session.get('https://rejectdopamine.com/api/app/active/yrk/cs/1') as resp: + response = await resp.json() + activities = [] + for session in response['sessions']: + if reformat_reject_date(session['startDate']) == date and time == f'{session["startTime"]} - {session["endTime"]}' and session['location'] in space: + # Likely a correct result, let's return it + return { + 'date': date, + 'time': time, + 'space': space, + 'activity': activity, + 'codes': list(map(lambda c: {'code': f'{c["checkinCode"]:06}', 'score': 0}, session['codes'])) + } + return { + 'date': date, + 'time': time, + 'space': space, + 'activity': activity, + 'codes': [], + } diff --git a/backend/aci/utils.py b/backend/aci/utils.py index 67f2aa3..846850e 100644 --- a/backend/aci/utils.py +++ b/backend/aci/utils.py @@ -3,4 +3,7 @@ from datetime import datetime def today() -> str: now = datetime.now() - return f"{now.strftime('%A')} {now.day:02} {now.strftime('%B')}" + return format_checkin_date(now) + +def format_checkin_date(dt: datetime): + return f"{dt.strftime('%A')} {dt.day:02} {dt.strftime('%B')}" diff --git a/backend/app.py b/backend/app.py index cfb0953..2fcb6ad 100644 --- a/backend/app.py +++ b/backend/app.py @@ -13,6 +13,7 @@ from aci.db import get_db from aci.migrate import run_migrations from aci.models import CodeSubmission from aci import utils +from aci import proxy app = FastAPI() @@ -82,6 +83,8 @@ def query_codes(date: str, time: str, activity: str, space: str, db: sqlite3.Cur 'codes': list(map(lambda c: {'code': c[0], 'score': c[1]}, code_scores.items())) } +app.include_router(proxy.router) + if __name__ == '__main__': run_migrations() uvicorn.run(app, host='0.0.0.0') diff --git a/flake.nix b/flake.nix index 54f9df8..1a96045 100644 --- a/flake.nix +++ b/flake.nix @@ -13,10 +13,12 @@ dependencies = ps: with ps; [ fastapi uvicorn + aoihttp ]; devDependencies = ps: with ps; [ fastapi uvicorn + aiohttp python-lsp-server ]; version = self.shortRev or self.dirtyShortDev or "dirty-inputs";