hogumathi_app.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import os
  2. import sys
  3. from importlib.util import find_spec
  4. from configparser import ConfigParser
  5. from flask import Flask, g, redirect, url_for, render_template, jsonify
  6. from flask_cors import CORS
  7. sys.path.append('.data/extensions')
  8. if find_spec('twitter_v2_facade'):
  9. from twitter_v2_facade import twitter_app as twitter_v2
  10. import oauth2_login
  11. twitter_enabled = True
  12. else:
  13. print('twitter module not found.')
  14. twitter_enabled = False
  15. if find_spec('twitter_v2_live_facade'):
  16. from twitter_v2_live_facade import twitter_app as twitter_v2_live
  17. import oauth2_login
  18. twitter_live_enabled = True
  19. else:
  20. print('twitter live module not found.')
  21. twitter_live_enabled = False
  22. if find_spec('twitter_archive_facade'):
  23. from twitter_archive_facade import twitter_app as twitter_archive
  24. archive_enabled = True
  25. else:
  26. print('twitter archive module not found.')
  27. archive_enabled = False
  28. if find_spec('mastodon_facade'):
  29. from mastodon_facade import twitter_app as mastodon
  30. mastodon_enabled = True
  31. else:
  32. print('mastodon module not found.')
  33. mastodon_enabled = False
  34. if find_spec('feeds_facade'):
  35. from feeds_facade import twitter_app as feeds
  36. feeds_enabled = True
  37. else:
  38. print('feeds module not found.')
  39. feeds_enabled = False
  40. if find_spec('youtube_facade'):
  41. from youtube_facade import youtube_app as youtube
  42. youtube_enabled = True
  43. else:
  44. print('youtube module not found.')
  45. youtube_enabled = False
  46. if find_spec('email_facade'):
  47. from email_facade import messages_facade as messages
  48. messages_enabled = True
  49. else:
  50. print('messages module not found.')
  51. messages_enabled = False
  52. if find_spec('videojs'):
  53. from videojs import videojs_bp
  54. videojs_enabled = True
  55. else:
  56. print('videojs module not found.')
  57. videojs_enabled = False
  58. if find_spec('visjs'):
  59. from visjs import visjs_bp
  60. visjs_enabled = True
  61. else:
  62. print('messages module not found.')
  63. visjs_enabled = False
  64. #messages_enabled = False
  65. #twitter_live_enabled = False
  66. add_account_enabled = True
  67. def import_env ():
  68. cp = ConfigParser()
  69. if os.path.exists('.env'):
  70. with open('.env') as stream:
  71. cp.read_string('[default]\n' + stream.read())
  72. os.environ.update(dict(cp['default']))
  73. if __name__ == '__main__':
  74. glitch_enabled = os.environ.get('PROJECT_DOMAIN') and True
  75. if glitch_enabled:
  76. t = os.environ.get('BEARER_TOKEN')
  77. if t:
  78. t = t.replace('%%', '%')
  79. os.environ['BEARER_TOKEN'] = t
  80. else:
  81. import_env()
  82. PORT = int(os.environ.get('PORT', 5000))
  83. HOST = os.environ.get('HOST', '127.0.0.1')
  84. archive_enabled = os.environ.get('ARCHIVE_TWEETS_PATH') and True
  85. notes_app_url = os.environ.get('NOTES_APP_URL')
  86. bedrss_app_url = os.environ.get('NOTES_APP_URL')
  87. plots_app_url = os.environ.get('PLOTS_APP_URL')
  88. videos_app_url = os.environ.get('VIDEOS_APP_URL')
  89. messages_app_url = os.environ.get('MESSAGES_APP_URL')
  90. if not os.path.exists('.data'):
  91. os.mkdir('.data')
  92. if not os.path.exists('.data/cache'):
  93. os.mkdir('.data/cache')
  94. api = Flask(__name__, static_url_path='')
  95. # HACK - environ from .env isn't set yet when the import happens. We should call an init function somewhere.
  96. oauth2_login.app_access_token = os.environ.get("BEARER_TOKEN")
  97. oauth2_login.app_consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
  98. oauth2_login.app_secret_key = os.environ.get("TWITTER_CONSUMER_SECRET")
  99. @api.before_request
  100. def add_config ():
  101. g.twitter_enabled = twitter_enabled
  102. g.archive_enabled = archive_enabled
  103. g.mastodon_enabled = mastodon_enabled
  104. g.feeds_enabled = feeds_enabled
  105. g.youtube_enabled = youtube_enabled
  106. g.messages_enabled = messages_enabled
  107. g.twitter_live_enabled = twitter_live_enabled
  108. g.add_account_enabled = add_account_enabled
  109. g.glitch_enabled = glitch_enabled
  110. g.videojs_enabled = videojs_enabled
  111. g.visjs_enabled = visjs_enabled
  112. if glitch_enabled:
  113. g.app_url = 'https://{}.glitch.me'.format( os.environ.get('PROJECT_DOMAIN') )
  114. else:
  115. g.app_url = 'http://{}:{}'.format('localhost', PORT)
  116. if notes_app_url:
  117. g.notes_app_url = notes_app_url
  118. if bedrss_app_url:
  119. g.bedrss_app_url = bedrss_app_url
  120. if plots_app_url:
  121. g.plots_app_url = plots_app_url
  122. if videos_app_url:
  123. g.videos_app_url = videos_app_url
  124. if messages_app_url:
  125. g.messages_app_url = messages_app_url
  126. @api.context_processor
  127. def inject_config ():
  128. config = {}
  129. config['twitter_enabled'] = twitter_enabled
  130. config['archive_enabled'] = archive_enabled
  131. config['mastodon_enabled'] = mastodon_enabled
  132. config['feeds_enabled'] = feeds_enabled
  133. config['youtube_enabled'] = youtube_enabled
  134. config['messages_enabled'] = messages_enabled
  135. config['twitter_live_enabled'] = twitter_live_enabled
  136. config['add_account_enabled'] = add_account_enabled
  137. config['glitch_enabled'] = glitch_enabled
  138. config['videojs_enabled'] = videojs_enabled
  139. config['visjs_enabled'] = visjs_enabled
  140. if notes_app_url:
  141. config['notes_app_url'] = notes_app_url
  142. if bedrss_app_url:
  143. config['bedrss_app_url'] = bedrss_app_url
  144. if plots_app_url:
  145. config['plots_app_url'] = plots_app_url
  146. if videos_app_url:
  147. config['videos_app_url'] = videos_app_url
  148. if messages_app_url:
  149. config['messages_app_url'] = messages_app_url
  150. return config
  151. @api.context_processor
  152. def add_nav_items_to_template_context ():
  153. nav_items = []
  154. route_nav = g.get('route_nav')
  155. if route_nav:
  156. nav_items += route_nav
  157. module_nav = g.get('module_nav')
  158. if module_nav:
  159. nav_items += module_nav
  160. #nav_items.sort(key = lambda ni: ni['order'])
  161. return dict(
  162. nav_items = nav_items
  163. )
  164. api.secret_key = os.environ.get('FLASK_SECRET')
  165. api.config['TEMPLATES_AUTO_RELOAD'] = True
  166. if videojs_enabled:
  167. api.register_blueprint(videojs_bp, url_prefix='/lib/videojs')
  168. if visjs_enabled:
  169. api.register_blueprint(visjs_bp, url_prefix='/lib/visjs')
  170. api.register_blueprint(twitter_v2, url_prefix='/twitter')
  171. if archive_enabled:
  172. api.register_blueprint(twitter_archive, url_prefix='/twitter-archive')
  173. if mastodon_enabled:
  174. api.register_blueprint(mastodon, url_prefix='/mastodon')
  175. if feeds_enabled:
  176. if not os.path.exists('.data/cache/feeds'):
  177. os.mkdir('.data/cache/feeds')
  178. api.register_blueprint(feeds, url_prefix='/feeds')
  179. if youtube_enabled:
  180. api.register_blueprint(youtube, url_prefix='/youtube')
  181. if messages_enabled:
  182. api.register_blueprint(messages, url_prefix='/messages')
  183. if twitter_live_enabled:
  184. api.register_blueprint(twitter_v2_live, url_prefix='/twitter-live')
  185. CORS(api)
  186. @api.get('/login.html')
  187. def get_login_html ():
  188. opengraph_info = dict(
  189. type = 'webpage', # threads might be article
  190. url = g.app_url,
  191. title = 'Hogumathi',
  192. description = 'An app for Twitter, Mastodon, YouTube, etc; Open Source.'
  193. )
  194. return render_template('login.html', opengraph_info=opengraph_info)
  195. @api.get('/')
  196. def index ():
  197. return redirect(url_for('.get_login_html'))
  198. @api.get('/brand/<brand_id>.html')
  199. def get_brand_html (brand_id):
  200. brands = {
  201. 'ispoogedaily': {
  202. 'id': 'ispoogedaily',
  203. 'display_name': 'iSpooge Daily',
  204. 'accounts': [
  205. 'twitter:14520320',
  206. 'mastodon:mastodon.cloud:109271381872332822',
  207. 'youtube:UCUMn9G0yzhQWXiRTOmPLXOg'
  208. ]
  209. }
  210. }
  211. brand = brands.get(brand_id.lower())
  212. if not brand:
  213. return '{"error": "no such brand."}', 404
  214. return jsonify(brand)
  215. api.run(port=PORT, host=HOST)