__main__.py 10 KB

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