feat(backend): add rejectdopamine proxy

This commit is contained in:
Ashhhleyyy 2024-03-08 17:11:38 +00:00
parent 24718ced1b
commit 238961799d
Signed by: ash
GPG key ID: 83B789081A0878FB
4 changed files with 46 additions and 1 deletions

37
backend/aci/proxy.py Normal file
View file

@ -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': [],
}

View file

@ -3,4 +3,7 @@ from datetime import datetime
def today() -> str: def today() -> str:
now = datetime.now() 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')}"

View file

@ -13,6 +13,7 @@ from aci.db import get_db
from aci.migrate import run_migrations from aci.migrate import run_migrations
from aci.models import CodeSubmission from aci.models import CodeSubmission
from aci import utils from aci import utils
from aci import proxy
app = FastAPI() 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())) 'codes': list(map(lambda c: {'code': c[0], 'score': c[1]}, code_scores.items()))
} }
app.include_router(proxy.router)
if __name__ == '__main__': if __name__ == '__main__':
run_migrations() run_migrations()
uvicorn.run(app, host='0.0.0.0') uvicorn.run(app, host='0.0.0.0')

View file

@ -13,10 +13,12 @@
dependencies = ps: with ps; [ dependencies = ps: with ps; [
fastapi fastapi
uvicorn uvicorn
aoihttp
]; ];
devDependencies = ps: with ps; [ devDependencies = ps: with ps; [
fastapi fastapi
uvicorn uvicorn
aiohttp
python-lsp-server python-lsp-server
]; ];
version = self.shortRev or self.dirtyShortDev or "dirty-inputs"; version = self.shortRev or self.dirtyShortDev or "dirty-inputs";