web.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from hashlib import sha256
  2. import os
  3. from pathlib import Path
  4. import json
  5. import requests
  6. from flask import Flask, g, redirect, url_for, render_template, jsonify, request, send_from_directory
  7. from . import content_system as h_cs
  8. api = Flask(__name__, static_url_path='')
  9. @api.context_processor
  10. def add_nav_items_to_template_context ():
  11. nav_items = []
  12. route_nav = g.get('route_nav')
  13. if route_nav:
  14. nav_items += route_nav
  15. module_nav = g.get('module_nav')
  16. if module_nav:
  17. nav_items += module_nav
  18. #nav_items.sort(key = lambda ni: ni['order'])
  19. return dict(
  20. nav_items = nav_items
  21. )
  22. @api.get('/login.html')
  23. def get_login_html ():
  24. opengraph_info = dict(
  25. type = 'webpage', # threads might be article
  26. url = g.app_url,
  27. title = 'Hogumathi',
  28. description = 'An app for Twitter, Mastodon, YouTube, etc; Open Source.'
  29. )
  30. return render_template('login.html', opengraph_info=opengraph_info)
  31. @api.get('/')
  32. def index ():
  33. return redirect(url_for('.get_login_html'))
  34. @api.get('/img')
  35. def get_image ():
  36. print('GET IMG')
  37. url = request.args['url']
  38. url_hash = sha256(url.encode('utf-8')).hexdigest()
  39. path = f'.data/cache/media/{url_hash}'
  40. print(f'path = {path}')
  41. if not os.path.exists(path):
  42. resp = requests.get(url)
  43. print(f'status_code = {resp.status_code}')
  44. if resp.status_code >= 200 and resp.status_code < 300:
  45. with open(path, 'wb') as f:
  46. f.write(resp.content)
  47. with open(f'{path}.meta', 'w') as f:
  48. headers = dict(resp.headers)
  49. json.dump(headers, f)
  50. else:
  51. return 'not found.', 404
  52. with open(f'{path}.meta', 'r') as f:
  53. headers = json.load(f)
  54. #print(url)
  55. #print(url_hash)
  56. #print(headers)
  57. # not sure why some responses use lower case.
  58. mimetype = headers.get('Content-Type') or headers.get('content-type')
  59. # Flask goes relative to the module as opposed to the working directory.
  60. media_cache_dir = Path(Path.cwd(), '.data/cache/media')
  61. return send_from_directory(media_cache_dir, url_hash, mimetype=mimetype)
  62. @api.get('/content/abc123.html')
  63. def get_abc123_html ():
  64. return 'abc123'
  65. @api.get('/content/<content_id>.html')
  66. def get_content_html (content_id, content_kwargs=None):
  67. if not content_kwargs:
  68. content_kwargs = filter(lambda e: e[0].startswith('content:'), request.args.items())
  69. content_kwargs = dict(map(lambda e: [e[0][len('content:'):], e[1]], content_kwargs))
  70. content = h_cs.get_content(content_id, **content_kwargs)
  71. if type(content) == h_vm.FeedItem:
  72. return render_template('tweet-collection.html', tweets=[content], user = {}, query = {})
  73. elif type(content) == h_vm.CollectionPage:
  74. pagination_token = request.args.get('pagination_token')
  75. if content.next_token:
  76. print(f'next_token = {content.next_token}')
  77. return render_template('tweet-collection.html', tweets=content.items, user = {}, query = {})
  78. else:
  79. return jsonify(content)
  80. @api.get('/content/def456.html')
  81. def get_def456_html ():
  82. return get_content_html('brand:ispoogedaily')