__main__.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import os
  2. import sys
  3. from importlib.util import find_spec
  4. from configparser import ConfigParser
  5. import json
  6. import requests
  7. from flask import Flask, g, redirect, url_for, render_template, jsonify, request, send_from_directory
  8. from flask_cors import CORS
  9. from .web import api
  10. from . import content_system as h_cs
  11. from . import item_collections, view_model as h_vm
  12. from .item_collections import item_collections_bp
  13. theme_bootstrap5_enabled = False
  14. if find_spec('theme_bootstrap5'):
  15. from theme_bootstrap5 import hogumathi_theme_bootstrap5_bp
  16. theme_bootstrap5_enabled = True
  17. if find_spec('twitter_v2_facade'):
  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. import twitter_v2_live_facade
  27. from twitter_v2_live_facade import twitter_app as twitter_v2_live
  28. import brands
  29. from brands import brands_bp
  30. twitter_live_enabled = True
  31. else:
  32. print('twitter live module not found.')
  33. twitter_live_enabled = False
  34. if find_spec('twitter_archive_facade'):
  35. import twitter_archive_facade
  36. from twitter_archive_facade import twitter_app as twitter_archive
  37. archive_enabled = True
  38. else:
  39. print('twitter archive module not found.')
  40. archive_enabled = False
  41. if find_spec('mastodon_facade'):
  42. from mastodon_facade import twitter_app as mastodon
  43. mastodon_enabled = True
  44. else:
  45. print('mastodon module not found.')
  46. mastodon_enabled = False
  47. if find_spec('feeds_facade'):
  48. import feeds_facade
  49. from feeds_facade import twitter_app as feeds
  50. feeds_enabled = True
  51. else:
  52. print('feeds module not found.')
  53. feeds_enabled = False
  54. if find_spec('youtube_v3_facade'):
  55. import youtube_v3_facade
  56. from youtube_v3_facade import youtube_app as youtube
  57. youtube_enabled = True
  58. else:
  59. print('youtube module not found.')
  60. youtube_enabled = False
  61. if find_spec('messages_facade'):
  62. from messages_facade import messages_facade as messages
  63. messages_enabled = True
  64. else:
  65. print('messages module not found.')
  66. messages_enabled = False
  67. if find_spec('instagram_embed_facade'):
  68. import instagram_embed_facade
  69. instagram_embed_enabled = True
  70. else:
  71. print('instagram_embed module not found.')
  72. instagram_embed_enabled = False
  73. if find_spec('instagram_facade'):
  74. import instagram_facade
  75. instagram_enabled = True
  76. else:
  77. print('instagram module not found.')
  78. instagram_enabled = False
  79. if find_spec('videojs'):
  80. from videojs import videojs_bp
  81. videojs_enabled = True
  82. else:
  83. print('videojs module not found.')
  84. videojs_enabled = False
  85. if find_spec('visjs'):
  86. from visjs import visjs_bp
  87. visjs_enabled = True
  88. else:
  89. print('messages module not found.')
  90. visjs_enabled = False
  91. """
  92. By default we use embed because it's less likely to get banned, because it does not
  93. use scraping. Once we've used scraping for a while and not been banned we can switch to that.
  94. Biz note: scraping could also be an early access feature.
  95. """
  96. print('disabling instagram_facade to avoid scraping... enable at your own risk')
  97. instagram_enabled = False
  98. #messages_enabled = False
  99. #twitter_live_enabled = True
  100. add_account_enabled = True
  101. def record_content_analytics (content_id, content):
  102. if type(content) == h_vm.CollectionPage:
  103. prefix = None
  104. if content_id.startswith('twitter:feed:user:') or content_id.startswith('twitter:bookmarks:') or content_id.startswith('twitter:feed:reverse_chronological:user:'):
  105. prefix = 'twitter:tweet:'
  106. if not prefix:
  107. return;
  108. for item in content.items:
  109. h_cs.invoke_hooks('got_content', f'{prefix}{item.id}', item)
  110. elif type(content) == h_vm.FeedItem:
  111. print(f'record_content_analytics: feed item: {content_id}')
  112. elif type(content) == h_vm.FeedServiceUser:
  113. print(f'record_content_analytics: user: {content_id}')
  114. else:
  115. print(f'record_content_analytics: unknown type: {content_id}')
  116. if __name__ == '__main__':
  117. glitch_enabled = os.environ.get('PROJECT_DOMAIN') and True
  118. t = os.environ.get('BEARER_TOKEN')
  119. PORT = int(os.environ.get('PORT', 5000))
  120. HOST = os.environ.get('HOST', '127.0.0.1')
  121. archive_enabled = os.environ.get('ARCHIVE_TWEETS_PATH') and True
  122. notes_app_url = os.environ.get('NOTES_APP_URL')
  123. bedrss_app_url = os.environ.get('NOTES_APP_URL')
  124. plots_app_url = os.environ.get('PLOTS_APP_URL')
  125. videos_app_url = os.environ.get('VIDEOS_APP_URL')
  126. messages_app_url = os.environ.get('MESSAGES_APP_URL')
  127. if not os.path.exists('.data'):
  128. os.mkdir('.data')
  129. if not os.path.exists('.data/cache'):
  130. os.mkdir('.data/cache')
  131. # HACK - environ from .env isn't set yet when the import happens. We should call an init function somewhere.
  132. oauth2_login.app_access_token = os.environ.get("BEARER_TOKEN")
  133. oauth2_login.app_consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
  134. oauth2_login.app_secret_key = os.environ.get("TWITTER_CONSUMER_SECRET")
  135. h_cs.register_hook('got_content', record_content_analytics)
  136. @api.before_request
  137. def add_config ():
  138. g.twitter_enabled = twitter_enabled
  139. g.archive_enabled = archive_enabled
  140. g.mastodon_enabled = mastodon_enabled
  141. g.feeds_enabled = feeds_enabled
  142. g.youtube_enabled = youtube_enabled
  143. g.messages_enabled = messages_enabled
  144. g.twitter_live_enabled = twitter_live_enabled
  145. g.add_account_enabled = add_account_enabled
  146. g.glitch_enabled = glitch_enabled
  147. g.videojs_enabled = videojs_enabled
  148. g.visjs_enabled = visjs_enabled
  149. if glitch_enabled:
  150. g.app_url = 'https://{}.glitch.me'.format( os.environ.get('PROJECT_DOMAIN') )
  151. else:
  152. g.app_url = 'http://{}:{}'.format('localhost', PORT)
  153. if notes_app_url:
  154. g.notes_app_url = notes_app_url
  155. if bedrss_app_url:
  156. g.bedrss_app_url = bedrss_app_url
  157. if plots_app_url:
  158. g.plots_app_url = plots_app_url
  159. if videos_app_url:
  160. g.videos_app_url = videos_app_url
  161. if messages_app_url:
  162. g.messages_app_url = messages_app_url
  163. if theme_bootstrap5_enabled:
  164. g.theme_bootstrap5_enabled = theme_bootstrap5_enabled
  165. @api.context_processor
  166. def inject_config ():
  167. config = {}
  168. config['twitter_enabled'] = twitter_enabled
  169. config['archive_enabled'] = archive_enabled
  170. config['mastodon_enabled'] = mastodon_enabled
  171. config['feeds_enabled'] = feeds_enabled
  172. config['youtube_enabled'] = youtube_enabled
  173. config['messages_enabled'] = messages_enabled
  174. config['twitter_live_enabled'] = twitter_live_enabled
  175. config['add_account_enabled'] = add_account_enabled
  176. config['glitch_enabled'] = glitch_enabled
  177. config['videojs_enabled'] = videojs_enabled
  178. config['visjs_enabled'] = visjs_enabled
  179. config['theme_bootstrap5_enabled'] = theme_bootstrap5_enabled
  180. if notes_app_url:
  181. config['notes_app_url'] = notes_app_url
  182. if bedrss_app_url:
  183. config['bedrss_app_url'] = bedrss_app_url
  184. if plots_app_url:
  185. config['plots_app_url'] = plots_app_url
  186. if videos_app_url:
  187. config['videos_app_url'] = videos_app_url
  188. if messages_app_url:
  189. config['messages_app_url'] = messages_app_url
  190. return config
  191. api.secret_key = os.environ.get('FLASK_SECRET')
  192. api.config['TEMPLATES_AUTO_RELOAD'] = True
  193. # the new way
  194. #api.config['notes_app_url'] = notes_app_url
  195. if theme_bootstrap5_enabled:
  196. api.register_blueprint(hogumathi_theme_bootstrap5_bp)
  197. if videojs_enabled:
  198. api.register_blueprint(videojs_bp, url_prefix='/lib/videojs')
  199. if visjs_enabled:
  200. api.register_blueprint(visjs_bp, url_prefix='/lib/visjs')
  201. twitter_v2_facade.register_content_sources()
  202. api.register_blueprint(twitter_v2, url_prefix='/twitter')
  203. if archive_enabled:
  204. twitter_archive_facade.register_content_sources()
  205. api.register_blueprint(twitter_archive, url_prefix='/twitter-archive')
  206. if mastodon_enabled:
  207. api.register_blueprint(mastodon, url_prefix='/mastodon')
  208. if youtube_enabled:
  209. youtube_v3_facade.register_content_sources()
  210. api.register_blueprint(youtube, url_prefix='/youtube')
  211. if messages_enabled:
  212. api.register_blueprint(messages, url_prefix='/messages')
  213. item_collections.register_content_sources()
  214. api.register_blueprint(item_collections_bp, url_prefix='/collections')
  215. if twitter_live_enabled:
  216. twitter_v2_live_facade.register_content_sources()
  217. api.register_blueprint(twitter_v2_live, url_prefix='/twitter-live')
  218. brands.register_content_sources()
  219. api.register_blueprint(brands_bp, url_prefix='/')
  220. if instagram_enabled:
  221. instagram_facade.register_content_sources()
  222. if instagram_embed_enabled:
  223. instagram_embed_facade.register_content_sources()
  224. if feeds_enabled:
  225. if not os.path.exists('.data/cache/feeds'):
  226. os.mkdir('.data/cache/feeds')
  227. feeds_facade.register_content_sources()
  228. api.register_blueprint(feeds, url_prefix='/feeds')
  229. #CORS(api)
  230. api.run(port=PORT, host=HOST)