<commit_before>import logging
import os
import time

from flask import Flask, flash, redirect, render_template, request, session
from flask_oauth import OAuth
from flask_sslify import SSLify
from wtforms import Form, StringField, SelectField

from analyze import analyze_followers, analyze_friends, div, get_friends_lists

logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)

app = Flask('twitter-gender-proportion')
app.config['SECRET_KEY'] = os.environ['COOKIE_SECRET']
app.config['DRY_RUN'] = False

CONSUMER_KEY = os.environ.get('CONSUMER_KEY')
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET')
TRACKING_ID = os.environ.get('TRACKING_ID')

oauth = OAuth()
twitter = oauth.remote_app(
    'twitter',
    base_url='https://api.twitter.com/1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authenticate',
    consumer_key=os.environ['CONSUMER_KEY'],
    consumer_secret=os.environ['CONSUMER_SECRET'])


@twitter.tokengetter
def get_twitter_token(token=None):
    return session.get('twitter_token')


@app.route('/login')
def login():
    callback = '/authorized'
    next_url = request.args.get('next') or request.referrer
    if next_url:
        callback += '?next=' + next_url

    # This has been particularly flakey.
    try:
        return twitter.authorize(callback=callback)
    except Exception:
        app.logger.exception("Error in twitter.authorize, retrying")
        return twitter.authorize(callback=callback)


@app.route('/logout')
def logout():
    session.pop('twitter_token')
    session.pop('twitter_user')
    flash(u'Logged out.')
    return redirect('/')


@app.route('/authorized')
@twitter.authorized_handler
def oauth_authorized(resp):
    next_url = request.args.get('next') or '/'
    if resp is None:
        flash(u'You denied the request to sign in.')
        return redirect(next_url)

    session['twitter_token'] = (resp['oauth_token'], resp['oauth_token_secret'])
    session['twitter_user'] = resp['screen_name']
    try:
        lists = get_friends_lists(resp['screen_name'],
                                  CONSUMER_KEY,
                                  CONSUMER_SECRET,
                                  resp['oauth_token'],
                                  resp['oauth_token_secret'])

        session['lists'] = [l.AsDict() for l in lists]
    except Exception:
        app.logger.exception("Error in get_friends_lists, ignoring")
        session['lists'] = []

    flash(u'You were signed in as %s' % resp['screen_name'])
    return redirect(next_url)

<commit_msg>Catch errors from Twitter OAuth<commit_after>import logging
import os
import time

from flask import Flask, flash, redirect, render_template, request, session
from flask_oauth import OAuth
from flask_sslify import SSLify
from wtforms import Form, StringField, SelectField

from analyze import analyze_followers, analyze_friends, div, get_friends_lists

logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)

app = Flask('twitter-gender-proportion')
app.config['SECRET_KEY'] = os.environ['COOKIE_SECRET']
app.config['DRY_RUN'] = False

CONSUMER_KEY = os.environ.get('CONSUMER_KEY')
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET')
TRACKING_ID = os.environ.get('TRACKING_ID')

oauth = OAuth()
twitter = oauth.remote_app(
    'twitter',
    base_url='https://api.twitter.com/1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authenticate',
    consumer_key=os.environ['CONSUMER_KEY'],
    consumer_secret=os.environ['CONSUMER_SECRET'])


@twitter.tokengetter
def get_twitter_token(token=None):
    return session.get('twitter_token')


@app.route('/login')
def login():
    callback = '/authorized'
    next_url = request.args.get('next') or request.referrer
    if next_url:
        callback += '?next=' + next_url

    # This has been particularly flakey.
    try:
        return twitter.authorize(callback=callback)
    except Exception:
        app.logger.exception("Error in twitter.authorize, retrying")
        return twitter.authorize(callback=callback)


@app.route('/logout')
def logout():
    session.pop('twitter_token')
    session.pop('twitter_user')
    flash(u'Logged out.')
    return redirect('/')


@app.route('/authorized')
def oauth_authorized():
    @twitter.authorized_handler
    def f(resp):
        next_url = request.args.get('next') or '/'
        if resp is None:
            flash(u'You denied the request to sign in.')
            return redirect(next_url)

        session['twitter_user'] = resp['screen_name']
        session['twitter_token'] = (resp['oauth_token'],
                                    resp['oauth_token_secret'])
        try:
            lists = get_friends_lists(resp['screen_name'],
                                      CONSUMER_KEY,
                                      CONSUMER_SECRET,
                                      resp['oauth_token'],
                                      resp['oauth_token_secret'])

            session['lists'] = [l.AsDict() for l in lists]
        except Exception:
            app.logger.exception("Error in get_friends_lists, ignoring")
            session['lists'] = []

        flash(u'You were signed in as %s' % resp['screen_name'])
        return redirect(next_url)

    try:
        return f()
    except Exception as exc:
        app.logger.exception("Error in oauth_authorized")
        try:
            flash(unicode(exc))
        except Exception:
            flash("Error in Twitter authorization")

        return redirect('/')


{'fraction_non_alphanumeric': 0.12320328542094455, 'fraction_numerical': 0.0006844626967830253, 'mean_word_length': 3.151278409090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4344657', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1757, 'n_words': 344}