twitter_app.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import os
  2. from importlib.util import find_spec
  3. from configparser import ConfigParser
  4. from flask import Flask, g, redirect, url_for, render_template, jsonify
  5. from flask_cors import CORS
  6. if find_spec('twitter_v2_facade'):
  7. from twitter_v2_facade import twitter_app as twitter_v2
  8. import oauth2_login
  9. twitter_enabled = True
  10. else:
  11. print('twitter module not found.')
  12. twitter_enabled = False
  13. if find_spec('twitter_archive_facade'):
  14. from twitter_archive_facade import twitter_app as twitter_archive
  15. archive_enabled = True
  16. else:
  17. print('twitter archive module not found.')
  18. archive_enabled = False
  19. if find_spec('mastodon_facade'):
  20. from mastodon_facade import twitter_app as mastodon
  21. mastodon_enabled = True
  22. else:
  23. print('mastodon module not found.')
  24. mastodon_enabled = False
  25. add_account_enabled = True
  26. def import_env ():
  27. cp = ConfigParser()
  28. if os.path.exists('.env'):
  29. with open('.env') as stream:
  30. cp.read_string('[default]\n' + stream.read())
  31. os.environ.update(dict(cp['default']))
  32. if __name__ == '__main__':
  33. import_env()
  34. PORT = int(os.environ.get('PORT', 5000))
  35. HOST = os.environ.get('HOST', '127.0.0.1')
  36. archive_enabled = os.environ.get('ARCHIVE_TWEETS_PATH') and True
  37. glitch_enabled = os.environ.get('PROJECT_DOMAIN') and True
  38. notes_app_url = os.environ.get('NOTES_APP_URL')
  39. if not os.path.exists('.data'):
  40. os.mkdir('.data')
  41. if not os.path.exists('.data/cache'):
  42. os.mkdir('.data/cache')
  43. api = Flask(__name__, static_url_path='')
  44. # HACK - environ from .env isn't set yet when the import happens. We should call an init function somewhere.
  45. oauth2_login.app_access_token = os.environ.get("BEARER_TOKEN")
  46. oauth2_login.app_consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
  47. oauth2_login.app_secret_key = os.environ.get("TWITTER_CONSUMER_SECRET")
  48. @api.before_request
  49. def add_config ():
  50. g.twitter_enabled = twitter_enabled
  51. g.archive_enabled = archive_enabled
  52. g.mastodon_enabled = mastodon_enabled
  53. g.add_account_enabled = add_account_enabled
  54. g.glitch_enabled = glitch_enabled
  55. if glitch_enabled:
  56. g.app_url = 'https://{}.glitch.me'.format( os.environ.get('PROJECT_DOMAIN') )
  57. else:
  58. g.app_url = 'http://{}:{}'.format('localhost', PORT)
  59. if notes_app_url:
  60. g.notes_app_url = notes_app_url
  61. @api.context_processor
  62. def inject_config ():
  63. config = {}
  64. config['twitter_enabled'] = twitter_enabled
  65. config['archive_enabled'] = archive_enabled
  66. config['mastodon_enabled'] = mastodon_enabled
  67. config['add_account_enabled'] = add_account_enabled
  68. config['glitch_enabled'] = glitch_enabled
  69. if notes_app_url:
  70. config['notes_app_url'] = notes_app_url
  71. return config
  72. api.secret_key = os.environ.get('FLASK_SECRET')
  73. api.config['TEMPLATES_AUTO_RELOAD'] = True
  74. api.register_blueprint(twitter_v2, url_prefix='/twitter')
  75. if archive_enabled:
  76. api.register_blueprint(twitter_archive, url_prefix='/twitter-archive')
  77. if mastodon_enabled:
  78. api.register_blueprint(mastodon, url_prefix='/mastodon')
  79. CORS(api)
  80. @api.get('/login.html')
  81. def get_login_html ():
  82. return render_template('login.html')
  83. @api.get('/')
  84. def index ():
  85. return redirect(url_for('.get_login_html'))
  86. @api.get('/brand/<brand_id>.html')
  87. def get_brand_html (brand_id):
  88. brand = {
  89. 'id': 'ispoogedaily',
  90. 'display_name': 'iSpooge Daily',
  91. 'accounts': [
  92. 'twitter:14520320',
  93. 'mastodon:mastodon.cloud:109271381872332822'
  94. ]
  95. }
  96. return jsonify(brand)
  97. api.run(port=PORT, host=HOST)