import os from importlib.util import find_spec from configparser import ConfigParser from flask import Flask, g, redirect, url_for, render_template, jsonify from flask_cors import CORS if find_spec('twitter_v2_facade'): from twitter_v2_facade import twitter_app as twitter_v2 import oauth2_login twitter_enabled = True else: print('twitter module not found.') twitter_enabled = False if find_spec('twitter_archive_facade'): from twitter_archive_facade import twitter_app as twitter_archive archive_enabled = True else: print('twitter archive module not found.') archive_enabled = False if find_spec('mastodon_facade'): from mastodon_facade import twitter_app as mastodon mastodon_enabled = True else: print('mastodon module not found.') mastodon_enabled = False add_account_enabled = True def import_env (): cp = ConfigParser() if os.path.exists('.env'): with open('.env') as stream: cp.read_string('[default]\n' + stream.read()) os.environ.update(dict(cp['default'])) if __name__ == '__main__': import_env() PORT = int(os.environ.get('PORT', 5000)) HOST = os.environ.get('HOST', '127.0.0.1') archive_enabled = os.environ.get('ARCHIVE_TWEETS_PATH') and True glitch_enabled = os.environ.get('PROJECT_DOMAIN') and True notes_app_url = os.environ.get('NOTES_APP_URL') if not os.path.exists('.data'): os.mkdir('.data') if not os.path.exists('.data/cache'): os.mkdir('.data/cache') api = Flask(__name__, static_url_path='') # HACK - environ from .env isn't set yet when the import happens. We should call an init function somewhere. oauth2_login.app_access_token = os.environ.get("BEARER_TOKEN") oauth2_login.app_consumer_key = os.environ.get("TWITTER_CONSUMER_KEY") oauth2_login.app_secret_key = os.environ.get("TWITTER_CONSUMER_SECRET") @api.before_request def add_config (): g.twitter_enabled = twitter_enabled g.archive_enabled = archive_enabled g.mastodon_enabled = mastodon_enabled g.add_account_enabled = add_account_enabled g.glitch_enabled = glitch_enabled if glitch_enabled: g.app_url = 'https://{}.glitch.me'.format( os.environ.get('PROJECT_DOMAIN') ) else: g.app_url = 'http://{}:{}'.format('localhost', PORT) if notes_app_url: g.notes_app_url = notes_app_url @api.context_processor def inject_config (): config = {} config['twitter_enabled'] = twitter_enabled config['archive_enabled'] = archive_enabled config['mastodon_enabled'] = mastodon_enabled config['add_account_enabled'] = add_account_enabled config['glitch_enabled'] = glitch_enabled if notes_app_url: config['notes_app_url'] = notes_app_url return config api.secret_key = os.environ.get('FLASK_SECRET') api.config['TEMPLATES_AUTO_RELOAD'] = True api.register_blueprint(twitter_v2, url_prefix='/twitter') if archive_enabled: api.register_blueprint(twitter_archive, url_prefix='/twitter-archive') if mastodon_enabled: api.register_blueprint(mastodon, url_prefix='/mastodon') CORS(api) @api.get('/login.html') def get_login_html (): return render_template('login.html') @api.get('/') def index (): return redirect(url_for('.get_login_html')) @api.get('/brand/.html') def get_brand_html (brand_id): brand = { 'id': 'ispoogedaily', 'display_name': 'iSpooge Daily', 'accounts': [ 'twitter:14520320', 'mastodon:mastodon.cloud:109271381872332822' ] } return jsonify(brand) api.run(port=PORT, host=HOST)