__main__.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import os
  2. import sys
  3. from importlib.util import find_spec
  4. from configparser import ConfigParser
  5. from hashlib import sha256
  6. import json
  7. import requests
  8. from flask import Flask, g, redirect, url_for, render_template, jsonify, request, send_from_directory
  9. from flask_cors import CORS
  10. from .content_system import get_content
  11. from .item_collections import item_collections_bp
  12. theme_bootstrap5_enabled = False
  13. if find_spec('theme_bootstrap5'):
  14. from theme_bootstrap5 import hogumathi_theme_bootstrap5_bp
  15. theme_bootstrap5_enabled = True
  16. if find_spec('twitter_v2_facade'):
  17. # FIXME get_brand needs a refactor
  18. import twitter_v2_facade
  19. from twitter_v2_facade import twitter_app as twitter_v2
  20. from twitter_v2_facade import oauth2_login
  21. twitter_enabled = True
  22. else:
  23. print('twitter module not found.')
  24. twitter_enabled = False
  25. if find_spec('twitter_v2_live_facade'):
  26. from twitter_v2_live_facade import twitter_app as twitter_v2_live
  27. import brands
  28. from brands import brands_bp, get_brand
  29. twitter_live_enabled = True
  30. else:
  31. print('twitter live module not found.')
  32. twitter_live_enabled = False
  33. if find_spec('twitter_archive_facade'):
  34. from twitter_archive_facade import twitter_app as twitter_archive
  35. archive_enabled = True
  36. else:
  37. print('twitter archive module not found.')
  38. archive_enabled = False
  39. if find_spec('mastodon_facade'):
  40. from mastodon_facade import twitter_app as mastodon
  41. mastodon_enabled = True
  42. else:
  43. print('mastodon module not found.')
  44. mastodon_enabled = False
  45. if find_spec('feeds_facade'):
  46. from feeds_facade import twitter_app as feeds
  47. feeds_enabled = True
  48. else:
  49. print('feeds module not found.')
  50. feeds_enabled = False
  51. if find_spec('youtube_facade'):
  52. from youtube_facade import youtube_app as youtube
  53. youtube_enabled = True
  54. else:
  55. print('youtube module not found.')
  56. youtube_enabled = False
  57. if find_spec('email_facade'):
  58. from email_facade import messages_facade as messages
  59. messages_enabled = True
  60. else:
  61. print('messages module not found.')
  62. messages_enabled = False
  63. if find_spec('videojs'):
  64. from videojs import videojs_bp
  65. videojs_enabled = True
  66. else:
  67. print('videojs module not found.')
  68. videojs_enabled = False
  69. if find_spec('visjs'):
  70. from visjs import visjs_bp
  71. visjs_enabled = True
  72. else:
  73. print('messages module not found.')
  74. visjs_enabled = False
  75. #messages_enabled = False
  76. #twitter_live_enabled = True
  77. add_account_enabled = True
  78. if __name__ == '__main__':
  79. glitch_enabled = os.environ.get('PROJECT_DOMAIN') and True
  80. t = os.environ.get('BEARER_TOKEN')
  81. PORT = int(os.environ.get('PORT', 5000))
  82. HOST = os.environ.get('HOST', '127.0.0.1')
  83. archive_enabled = os.environ.get('ARCHIVE_TWEETS_PATH') and True
  84. notes_app_url = os.environ.get('NOTES_APP_URL')
  85. bedrss_app_url = os.environ.get('NOTES_APP_URL')
  86. plots_app_url = os.environ.get('PLOTS_APP_URL')
  87. videos_app_url = os.environ.get('VIDEOS_APP_URL')
  88. messages_app_url = os.environ.get('MESSAGES_APP_URL')
  89. if not os.path.exists('.data'):
  90. os.mkdir('.data')
  91. if not os.path.exists('.data/cache'):
  92. os.mkdir('.data/cache')
  93. api = Flask(__name__, static_url_path='')
  94. # HACK - environ from .env isn't set yet when the import happens. We should call an init function somewhere.
  95. oauth2_login.app_access_token = os.environ.get("BEARER_TOKEN")
  96. oauth2_login.app_consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
  97. oauth2_login.app_secret_key = os.environ.get("TWITTER_CONSUMER_SECRET")
  98. @api.before_request
  99. def add_config ():
  100. g.twitter_enabled = twitter_enabled
  101. g.archive_enabled = archive_enabled
  102. g.mastodon_enabled = mastodon_enabled
  103. g.feeds_enabled = feeds_enabled
  104. g.youtube_enabled = youtube_enabled
  105. g.messages_enabled = messages_enabled
  106. g.twitter_live_enabled = twitter_live_enabled
  107. g.add_account_enabled = add_account_enabled
  108. g.glitch_enabled = glitch_enabled
  109. g.videojs_enabled = videojs_enabled
  110. g.visjs_enabled = visjs_enabled
  111. if glitch_enabled:
  112. g.app_url = 'https://{}.glitch.me'.format( os.environ.get('PROJECT_DOMAIN') )
  113. else:
  114. g.app_url = 'http://{}:{}'.format('localhost', PORT)
  115. if notes_app_url:
  116. g.notes_app_url = notes_app_url
  117. if bedrss_app_url:
  118. g.bedrss_app_url = bedrss_app_url
  119. if plots_app_url:
  120. g.plots_app_url = plots_app_url
  121. if videos_app_url:
  122. g.videos_app_url = videos_app_url
  123. if messages_app_url:
  124. g.messages_app_url = messages_app_url
  125. if theme_bootstrap5_enabled:
  126. g.theme_bootstrap5_enabled = theme_bootstrap5_enabled
  127. @api.context_processor
  128. def inject_config ():
  129. config = {}
  130. config['twitter_enabled'] = twitter_enabled
  131. config['archive_enabled'] = archive_enabled
  132. config['mastodon_enabled'] = mastodon_enabled
  133. config['feeds_enabled'] = feeds_enabled
  134. config['youtube_enabled'] = youtube_enabled
  135. config['messages_enabled'] = messages_enabled
  136. config['twitter_live_enabled'] = twitter_live_enabled
  137. config['add_account_enabled'] = add_account_enabled
  138. config['glitch_enabled'] = glitch_enabled
  139. config['videojs_enabled'] = videojs_enabled
  140. config['visjs_enabled'] = visjs_enabled
  141. config['theme_bootstrap5_enabled'] = theme_bootstrap5_enabled
  142. if notes_app_url:
  143. config['notes_app_url'] = notes_app_url
  144. if bedrss_app_url:
  145. config['bedrss_app_url'] = bedrss_app_url
  146. if plots_app_url:
  147. config['plots_app_url'] = plots_app_url
  148. if videos_app_url:
  149. config['videos_app_url'] = videos_app_url
  150. if messages_app_url:
  151. config['messages_app_url'] = messages_app_url
  152. return config
  153. @api.context_processor
  154. def add_nav_items_to_template_context ():
  155. nav_items = []
  156. route_nav = g.get('route_nav')
  157. if route_nav:
  158. nav_items += route_nav
  159. module_nav = g.get('module_nav')
  160. if module_nav:
  161. nav_items += module_nav
  162. #nav_items.sort(key = lambda ni: ni['order'])
  163. return dict(
  164. nav_items = nav_items
  165. )
  166. api.secret_key = os.environ.get('FLASK_SECRET')
  167. api.config['TEMPLATES_AUTO_RELOAD'] = True
  168. if theme_bootstrap5_enabled:
  169. api.register_blueprint(hogumathi_theme_bootstrap5_bp)
  170. if videojs_enabled:
  171. api.register_blueprint(videojs_bp, url_prefix='/lib/videojs')
  172. if visjs_enabled:
  173. api.register_blueprint(visjs_bp, url_prefix='/lib/visjs')
  174. twitter_v2_facade.register_content_sources()
  175. api.register_blueprint(twitter_v2, url_prefix='/twitter')
  176. if archive_enabled:
  177. api.register_blueprint(twitter_archive, url_prefix='/twitter-archive')
  178. if mastodon_enabled:
  179. api.register_blueprint(mastodon, url_prefix='/mastodon')
  180. if feeds_enabled:
  181. if not os.path.exists('.data/cache/feeds'):
  182. os.mkdir('.data/cache/feeds')
  183. api.register_blueprint(feeds, url_prefix='/feeds')
  184. if youtube_enabled:
  185. api.register_blueprint(youtube, url_prefix='/youtube')
  186. if messages_enabled:
  187. api.register_blueprint(messages, url_prefix='/messages')
  188. if twitter_live_enabled:
  189. brands.register_content_sources()
  190. api.register_blueprint(twitter_v2_live, url_prefix='/twitter-live')
  191. api.register_blueprint(item_collections_bp, url_prefix='/collections')
  192. api.register_blueprint(brands_bp, url_prefix='/')
  193. #CORS(api)
  194. @api.get('/login.html')
  195. def get_login_html ():
  196. opengraph_info = dict(
  197. type = 'webpage', # threads might be article
  198. url = g.app_url,
  199. title = 'Hogumathi',
  200. description = 'An app for Twitter, Mastodon, YouTube, etc; Open Source.'
  201. )
  202. return render_template('login.html', opengraph_info=opengraph_info)
  203. @api.get('/')
  204. def index ():
  205. return redirect(url_for('.get_login_html'))
  206. @api.get('/img')
  207. def get_image ():
  208. url = request.args['url']
  209. url_hash = sha256(url.encode('utf-8')).hexdigest()
  210. path = f'.data/cache/media/{url_hash}'
  211. if not os.path.exists(path):
  212. resp = requests.get(url)
  213. if resp.status_code >= 200 and resp.status_code < 300:
  214. with open(path, 'wb') as f:
  215. f.write(resp.content)
  216. with open(f'{path}.meta', 'w') as f:
  217. headers = dict(resp.headers)
  218. json.dump(headers, f)
  219. else:
  220. return 'not found.', 404
  221. with open(f'{path}.meta', 'r') as f:
  222. headers = json.load(f)
  223. #print(url)
  224. #print(url_hash)
  225. #print(headers)
  226. # not sure why some responses use lower case.
  227. mimetype = headers.get('Content-Type') or headers.get('content-type')
  228. return send_from_directory('.data/cache/media', url_hash, mimetype=mimetype)
  229. @api.get('/content/abc123.html')
  230. def get_abc123_html ():
  231. return 'abc123'
  232. @api.get('/content/<content_id>.html')
  233. def get_content_html (content_id, content_kwargs=None):
  234. if not content_kwargs:
  235. content_kwargs = filter(lambda e: e[0].startswith('content:'), request.args.items())
  236. content_kwargs = dict(map(lambda e: [e[0][len('content:'):], e[1]], content_kwargs))
  237. content = get_content(content_id, **content_kwargs)
  238. return jsonify(content)
  239. @api.get('/content/def456.html')
  240. def get_def456_html ():
  241. return get_content_html('brand:ispoogedaily')
  242. api.run(port=PORT, host=HOST)