def __init__(self): self.api = praw.Reddit( client_id=Option.get('REDDIT_CLIENT_ID'), username=Option.get('REDDIT_USER'), password=Option.get('REDDIT_PASSWORD'), client_secret=Option.get('REDDIT_CLIENT_SECRET'), user_agent='RaspberryFrame') self.api.read_only = True
def auth(): """ Callback method from Instagram after a user accepts the app. """ payload = { 'client_id': Option.get('INSTAGRAM_CLIENT_ID').value, 'client_secret': Option.get('INSTAGRAM_CLIENT_SECRET').value, 'grant_type': 'authorization_code', 'redirect_uri': 'https://scs.bad-at.life/insta', 'code': request.args.get("code") } token_url = "https://api.instagram.com/oauth/access_token" r = requests.post(token_url, data=payload) if not r.status_code == 200: return 'Error!: %s\n %s' % (r.status_code, r.text) json_resp = json.loads(r.text) token = Option.get('INSTAGRAM_TOKEN') if not token: token = Option() token.name = "INSTAGRAM_TOKEN" token.value = json_resp['access_token'] token.save() return 'We good'
def register_options(): """ Creates the default values for options. """ defaults = { 'admin-url': os.environ.get('SH_ADMIN_URL'), 'hosted-file-url': os.environ.get('SH_HOSTED_FILES_URL'), 'admin-user': os.environ.get('SH_ADMIN_USER'), 'admin-pass': generate_password_hash(os.environ.get('SH_ADMIN_PASS')), 'google-analytics': None, 'enable-custom-python-uris': None, } Option.set_defaults(defaults)
def register_admin(app): """ Starts the admin utility :param app: Current Flask application :type app: <Flask 'app'> obj :returns: Flask Admin with registered controllers :rtype: <flask_admin> obj """ admin_url = Option.get('admin-url') if admin_url: admin_url = admin_url.value else: admin_url = os.environ.get('SH_ADMIN_URL') admin = Admin(app, url="/%s" % admin_url, name='Simple-Honey', template_mode='bootstrap3') admin.add_view(UriModelView(Uri, db.session, name="Uris")) admin.add_view( WebRequestModelView(WebRequest, db.session, name="Web Requests")) admin.add_view(KnownIpModelView(KnownIp, db.session, name="Known IPs")) admin.add_view( FileAdmin(os.environ.get('SH_HOSTED_FILES'), name='Hosted Files')) admin.add_view(OptionModelView(Option, db.session, name='Options')) admin.add_view(AuthView(name="Logout", endpoint='/auth/logout')) return admin
def auth_url(): """ Creates and displays the url for authorization """ url = "https://api.instagram.com/oauth/authorize/?client_id=%s&redirect_uri=%s&response_type=code" % ( Option.get('INSTAGRAM_CLIENT_ID').value, urllib.quote_plus('https://scs.bad-at.life/insta'), ) return str(url)
def connect(self): """ Gets the Option table item for INSTAGRAM_TOKEN and logs in. """ INSTAGRAM_CLIENT_ID = Option.get('INSTAGRAM_CLIENT_ID').value INSTAGRAM_CLIENT_SECRET = Option.get("INSTAGRAM_CLIENT_SECRET").value INSTAGRAM_TOKEN = Option.get('INSTAGRAM_TOKEN').value if not INSTAGRAM_CLIENT_ID or not INSTAGRAM_CLIENT_SECRET or not INSTAGRAM_TOKEN: app.logger.error('Missing Instagram option values!') print 'INSTAGRAM_CLIENT_ID %s' % INSTAGRAM_CLIENT_ID print 'INSTAGRAM_CLIENT_SECRET %s' % INSTAGRAM_CLIENT_SECRET print 'INSTAGRAM_TOKEN: %s' % INSTAGRAM_TOKEN exit(1) self.INSTAGRAM_USER_ID = '374169053' self.api = InstagramAPI(access_token=INSTAGRAM_TOKEN, client_id=INSTAGRAM_CLIENT_ID, client_secret=INSTAGRAM_CLIENT_SECRET)
def save_serialized_file(): """ Saves options and uri map as a serialized cache file. """ options = Option.load_all() data = { 'options': options, 'uris': get_uri_map() } with open(os.environ.get('SH_CACHE_FILE'), 'wb') as handle: pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL)
def _create_options(self, waypoints, **bool_dict_kwargs): for wp in waypoints: for opt in wp["options"]: option_dict = { "sourceWaypoint_id": self.wp_mapping[wp["id"]], "destinationWaypoint_id": self.wp_mapping[opt["destinationWaypoint_id"]], "linkText": opt["linkText"] } opt = Option(**option_dict) db.session.add(opt) db.session.flush()
"""Install """ from app import app from app import db from app.models.option import Option DEFAULT_OPTIONS = ['INSTAGRAM_CLIENT_ID', 'INSTAGRAM_CLIENT_SECRET', 'INSTAGRAM_TOKEN'] if __name__ == '__main__': app.logger.info('Runing installer') Option.set_defaults(DEFAULT_OPTIONS) db.create_all() app.logger.info('Finished installing')