Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

app.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from datetime import datetime, timedelta
  2. import json
  3. import traceback
  4. import aiofiles
  5. import fastapi
  6. from fastapi import Cookie, File, Form, Request, UploadFile, WebSocket, WebSocketDisconnect
  7. from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse, PlainTextResponse
  8. from fastapi.staticfiles import StaticFiles
  9. from fastapi.templating import Jinja2Templates
  10. from fastapi.middleware.cors import CORSMiddleware
  11. import reporthook
  12. import inventory
  13. import sbenv
  14. import searchlib
  15. MAX_SEARCH_DAYS = 180
  16. MAX_SHOW_DAYS = 20
  17. REPORT_HORIZON = 180
  18. MAX_USER_REPORTS_PER_DAY = 3
  19. ################################
  20. # Core configuration
  21. ################################
  22. app = fastapi.FastAPI(docs_url=None)
  23. origins = [
  24. 'https://prograde.gg',
  25. 'http://localhost',
  26. 'http://localhost:5000',
  27. 'http://localhost:8080',
  28. 'http://localhost:8000'
  29. ]
  30. app.add_middleware(
  31. CORSMiddleware,
  32. allow_origins=origins,
  33. allow_credentials=True,
  34. allow_methods=['*'],
  35. allow_headers=['*']
  36. )
  37. app.mount('/static', StaticFiles(directory='static'), name='static')
  38. tmplts = Jinja2Templates(directory='templates') # TODO Get the path correctly.
  39. ################################
  40. # User-facing endpoints
  41. ################################
  42. @app.exception_handler(Exception)
  43. async def handle_exception(req: Request, exc: Exception):
  44. tb = traceback.format_exc()
  45. await reporthook.send_report(tb)
  46. return PlainTextResponse('error', status_code=500)
  47. @app.get('/')
  48. async def render_main(req: Request):
  49. raw_articles = await load_recent_articles()
  50. converted = convert_days_from_articles(raw_articles)
  51. num_days = calc_num_days(converted)
  52. p = {
  53. 'sb': {
  54. 'num_days': num_days,
  55. 'days': converted
  56. },
  57. 'notices': [
  58. {
  59. 'style': 'primary',
  60. 'text': 'There were so many incidents in August 2021 that news sites stopped reporting on it, so there\'s some missing data here.',
  61. }
  62. ],
  63. 'request': req,
  64. }
  65. return tmplts.TemplateResponse('main.htm', p)
  66. @app.post('/action/flag')
  67. async def handle_flag(req: Request, date: str = Form(...), article: str = Form(...)):
  68. ipaddr = req.client.host
  69. try:
  70. today = datetime.now()
  71. pdate = datetime.strptime(date, inventory.DATE_FORMAT)
  72. if pdate > today or (today - pdate).days > REPORT_HORIZON:
  73. raise ValueError('bad date')
  74. except Exception as e:
  75. return JSONResponse({'status': 'error'}, status_code=400)
  76. flags = await inventory.load_date_flags_async(pdate)
  77. # Make sure it's not a duplicate and limit the number of reports
  78. nreporter = 0
  79. for e in flags:
  80. if e['src'] == ipaddr:
  81. if e['url'] == article:
  82. return {'status': 'OK'}
  83. nreporter += 1
  84. if nreporter + 1 >= MAX_USER_REPORTS_PER_DAY:
  85. print('user', ipaddr, 'looking sussy')
  86. await reporthook.send_report('address %s made more reports for %s than allowed' % (ipaddr, date))
  87. return JSONResponse({'status': 'error'}, status_code=429)
  88. await reporthook.send_report('address %s reported url %s' % (ipaddr, article))
  89. flags.append({
  90. 'src': ipaddr,
  91. 'url': article,
  92. })
  93. await inventory.save_date_flags_async(pdate, flags)
  94. return make_html_redirect_response('/')
  95. @app.post('/action/submit')
  96. async def handle_submit(req: Request, article: str = Form(...)):
  97. ipaddr = req.client.host
  98. today_str = datetime.now().strftime(inventory.DATE_FORMAT)
  99. fetched_art = searchlib.fetch_article(article)
  100. if fetched_art is None:
  101. return make_html_redirect_response('/')
  102. eff_date = fetched_art['nd'] if 'nd' in fetched_art else today_str
  103. # Now process it so we can tell that it's a definite match.
  104. proced_art = searchlib.process_day_results(eff_date, [fetched_art])
  105. print(proced_art)
  106. if len(proced_art['pass']) == 0:
  107. return make_html_redirect_response('/')
  108. # If it all looks good then store it and report it.
  109. await add_article(eff_date, fetched_art)
  110. await reporthook.send_report('address %s submitted good-looking article %s' % (ipaddr, article))
  111. return make_html_redirect_response('/')
  112. ################################
  113. # API endpoints
  114. ################################
  115. @app.post('/api/addarticle')
  116. async def handle_addarticle(req: Request):
  117. if not check_admin_token(req):
  118. return JSONResponse(status_code=403, content={'error': 'forbidden'})
  119. body = await req.json()
  120. await add_article(body['date'], body['desc'])
  121. return {'status': 'OK'}
  122. async def add_article(datestr, adesc):
  123. date = datetime.strptime(datestr, inventory.DATE_FORMAT)
  124. articles = await inventory.load_date_report_async(date)
  125. articles.append(adesc)
  126. await inventory.save_date_report_async(date, articles)
  127. ################################
  128. # Utilities
  129. ################################
  130. def check_admin_token(req: Request):
  131. ak = sbenv.get_admin_key()
  132. if ak is None:
  133. raise RuntimeError('checked api endpoint without key loaded')
  134. if ak == 'UNSAFE_TESTING':
  135. return True
  136. if 'Authorization' in req.headers:
  137. auth = req.headers['Authorization']
  138. if not auth.startswith('Bearer '):
  139. return False
  140. tok = auth[len('Bearer '):]
  141. return tok == sbenv.get_admin_key()
  142. else:
  143. return False
  144. async def load_days_from_file(path):
  145. async with aiofiles.open(path, mode='r') as f:
  146. contents = await f.read()
  147. return json.loads(contents)
  148. async def load_recent_articles():
  149. today = datetime.now()
  150. day_dur = timedelta(days=1)
  151. reports = {}
  152. for i in range(MAX_SEARCH_DAYS):
  153. that_day = today - i * day_dur
  154. report = await inventory.load_date_report_async(that_day)
  155. flags = await inventory.load_date_flags_async(that_day)
  156. if len(report) > 0:
  157. reports[that_day.strftime(inventory.DATE_FORMAT)] = {
  158. 'articles': report,
  159. 'flags': flags,
  160. }
  161. return reports
  162. def convert_days_from_articles(days):
  163. output = []
  164. for dstr, parts in days.items():
  165. dr = searchlib.process_day_results(dstr, parts['articles'])
  166. flags = {e['url'] for e in parts['flags']}
  167. day = {
  168. 'date': dstr,
  169. 'links': [],
  170. 'maybe_links': []
  171. }
  172. # Process hard passes.
  173. for a in dr['pass']:
  174. ca = convert_article(a)
  175. if a['url'] not in flags:
  176. day['links'].append(ca)
  177. else:
  178. day['maybe_links'].append(ca)
  179. # Process weak articles.
  180. for a in dr['maybe']:
  181. ca = convert_article(a)
  182. if a['url'] not in flags:
  183. day['maybe_links'].append(ca)
  184. if len(day['links']) > 0:
  185. output.append(day)
  186. if len(output) > MAX_SHOW_DAYS:
  187. break
  188. return output
  189. def convert_article(a):
  190. return {
  191. 'url': a['url'],
  192. 'title': a['gtitle'],
  193. 'slug': a['slug'],
  194. }
  195. def calc_num_days(dayslist):
  196. today = datetime.now()
  197. lowest = -1
  198. for d in dayslist:
  199. pd = datetime.strptime(d['date'], inventory.DATE_FORMAT)
  200. diff = today - pd
  201. ndays = diff.days
  202. if ndays < lowest or lowest == -1:
  203. lowest = ndays
  204. return lowest
  205. def make_html_redirect_response(url):
  206. return HTMLResponse('<head><meta http-equiv="Refresh" content="0; URL=' + url + '"></head>')