<commit_before>import inspect
import os
import sys

import tweepy
from flask import Flask, request, redirect, url_for, session, flash
from flask import send_file
import tweepy
from flask import Flask, request, redirect, url_for, session, flash
from flask import send_file
from flask.templating import render_template

from backend import config, dal, consts
from . flask_oauth import OAuth

from backend import config, dal, consts
from backend.adapters.request_adapter import request_adapter_wrapper
from backend.adapters.response_adapter import response_adapter_wrapper
from backend.config import frontend_path
from backend.entities.base_request import BaseRequest
from backend.logs.logger import logger

sys.path.append(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))

# initialize Flask
app = Flask(__name__, static_folder="../")
logger.info(" ################## SEARCH STARTING ##################" +
            "\n - Configuration name: " + config.name)
SECRET_KEY = "development key"
app.secret_key = SECRET_KEY


oauth = OAuth()

api = None

# Use Twitter as example remote application
twitter = oauth.remote_app('twitter',
                           # unless absolute urls are used to make requests, this will be added
                           # before all URLs.  This is also true for request_token_url and others.
                           base_url='https://api.twitter.com/1.1/',
                           # where flask should look for new request tokens
                           request_token_url='https://api.twitter.com/oauth/request_token',
                           # where flask should exchange the token with the remote application
                           access_token_url='https://api.twitter.com/oauth/access_token',
                           # twitter knows two authorizatiom URLs.  /authorize and /authenticate.
                           # they mostly work the same, but for sign on /authenticate is
                           # expected because this will give the user a slightly different
                           # user interface on the twitter side.
                           authorize_url='https://api.twitter.com/oauth/authorize',
                           # the consumer keys from the twitter application registry.
                           consumer_key=consts.consumer_key,
                           consumer_secret=consts.consumer_secret
                           )


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


@app.route('/perform_login')
def perform_login():
    return twitter.authorize(callback=url_for('oauth_authorized',
                                              next=request.args.get('next') or request.referrer or None))


@app.route('/logout')
def logout():
    session.pop('screen_name', None)
    flash('You were signed out')
    return redirect(request.referrer or url_for('/'))


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

    session['access_key'] = resp['oauth_token']
    session['access_secret'] = resp['oauth_token_secret']
    session['screen_name'] = resp['screen_name']

    session['twitter_token'] = (
        resp['oauth_token'],
        resp['oauth_token_secret']
    )

    auth = tweepy.OAuthHandler(consts.consumer_key, consts.consumer_secret)
    auth.set_access_token(session['access_key'], session['access_secret'])

    global api
    api = tweepy.API(auth)

    return redirect("/search")


@app.route("/retweet/<tweet_id>/<content>")
def retweet(content, tweet_id):
    # tweetId = 900804629885222913
    screen_name = api.statuses_lookup(id_=[tweet_id, ])[0].user.screen_name
    res = api.update_status('@{username} {content}'.format(username=screen_name, content=content), tweetId)<commit_msg>Now logging in twice wont crash<commit_after>import inspect
import os
import sys
from functools import wraps

import tweepy
from flask import Flask, request, redirect, url_for, session, flash
from flask import send_file
import tweepy
from flask import Flask, request, redirect, url_for, session, flash
from flask import send_file
from flask.templating import render_template

from backend import config, dal, consts
from . flask_oauth import OAuth

from backend import config, dal, consts
from backend.adapters.request_adapter import request_adapter_wrapper
from backend.adapters.response_adapter import response_adapter_wrapper
from backend.config import frontend_path
from backend.entities.base_request import BaseRequest
from backend.logs.logger import logger

sys.path.append(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))

# initialize Flask
app = Flask(__name__, static_folder="../")
logger.info(" ################## SEARCH STARTING ##################" +
            "\n - Configuration name: " + config.name)
SECRET_KEY = "development key"
app.secret_key = SECRET_KEY


oauth = OAuth()

api = None

# Use Twitter as example remote application
twitter = oauth.remote_app('twitter',
                           # unless absolute urls are used to make requests, this will be added
                           # before all URLs.  This is also true for request_token_url and others.
                           base_url='https://api.twitter.com/1.1/',
                           # where flask should look for new request tokens
                           request_token_url='https://api.twitter.com/oauth/request_token',
                           # where flask should exchange the token with the remote application
                           access_token_url='https://api.twitter.com/oauth/access_token',
                           # twitter knows two authorizatiom URLs.  /authorize and /authenticate.
                           # they mostly work the same, but for sign on /authenticate is
                           # expected because this will give the user a slightly different
                           # user interface on the twitter side.
                           authorize_url='https://api.twitter.com/oauth/authorize',
                           # the consumer keys from the twitter application registry.
                           consumer_key=consts.consumer_key,
                           consumer_secret=consts.consumer_secret
                           )


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


@app.route('/perform_login')
def perform_login():
    return twitter.authorize(callback=url_for('oauth_authorized',
                                              next=request.args.get('next') or request.referrer or None))


@app.route('/logout')
def logout():
    session.pop('screen_name', None)
    flash('You were signed out')
    return redirect("/")


def skip_if_authorized(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        if session.get("screen_name"):
            return redirect("/search")
        return func(*args, *kwargs)
    return wrapped


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

    session['access_key'] = resp['oauth_token']
    session['access_secret'] = resp['oauth_token_secret']
    session['screen_name'] = resp['screen_name']

    session['twitter_token'] = (
        resp['oauth_token'],
        resp['oauth_token_secret']
    )

    auth = tweepy.OAuthHandler(consts.consumer_key, consts.consumer_secret)
    auth.set_access_token(session['access_key'], session['access_secret'])

    global api
    api = tweepy.API(auth)

    return redirect("/search")


@app.route("/retweet/<tweet_id>/<content>")
def retweet(content, tweet_id):
    # tweetId = 900804629885222913
    screen_name = api.statuses_lookup(id_=[tweet_id, ])[0].user.screen_name
    res = api.update_status('@{username} {content}'.format(username=screen_name, content=content), tweetId)
{'fraction_non_alphanumeric': 0.10362504577077994, 'fraction_numerical': 0.005126327352618088, 'mean_word_length': 2.8058522991175106, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1156120', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2209, 'n_words': 580}