hogumathi_app.py 8.1 KB

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