<commit_before>import os
from collections import deque
from flask import Flask, redirect, url_for, session, redirect, jsonify, request
from flask_oauth import OAuth
from flask_cors import CORS, cross_origin
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, Load
from models import Character, User, Draft, DraftTicket
from flask_uwsgi_websocket import GeventWebSocket
from game import handle_event, can_draft
from flask import Flask
from flask_dotenv import DotEnv
import json
import redis
import os


app = Flask(__name__)
env = DotEnv()
env_file = '/var/www/fantasyx/.env' if os.path.exists('/var/www/fantasyx/.env') else '.env'
env.init_app(app, env_file=env_file, verbose_mode=True)

# You must configure these 3 values from Google APIs console
# https://code.google.com/apis/console
REDIRECT_URI = '/api/oauth2callback'  # one of the Redirect URIs from Google APIs console
 
SECRET_KEY = 'development key'
DEBUG = True
 
CORS(app)
app.debug = DEBUG
app.secret_key = SECRET_KEY
websocket = GeventWebSocket(app)
oauth = OAuth()
 
google = oauth.remote_app('google',
                          base_url='https://www.google.com/accounts/',
                          authorize_url='https://accounts.google.com/o/oauth2/auth',
                          request_token_url=None,
                          request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
                                                'response_type': 'code',
                                                'access_type':'offline',
                                                'prompt': 'consent'
                          },
                          access_token_url='https://accounts.google.com/o/oauth2/token',
                          access_token_method='POST',
                          access_token_params={'grant_type': 'authorization_code'},
                          consumer_key=app.config['GOOGLE_CLIENT_ID'],
                          consumer_secret=app.config['GOOGLE_CLIENT_SECRET'],
                          
)

if app.config['FLASK_ENV'] == 'development':
    engine=create_engine('postgresql://admin:admin@localhost:5432/fantasyx')
else:
    engine=create_engine('postgresql://%(DB_USERNAME)s:%(DB_PASSWORD)s@%(DB_ENDPOINT)s:5432/fantasyx' % app.config)

db_session = Session(bind=engine)

@app.route('/api/')
def index():
    print("in index")
    access_token = session.get('access_token')
    if access_token is None:
        return redirect(url_for('login'))
 
    access_token = access_token[0]
    from urllib2 import Request, urlopen, URLError
 
    headers = {'Authorization': 'OAuth '+access_token}
    req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
                  None, headers)
    try:
        res = urlopen(req)
        print("response:", res)
    except URLError as e:
        if e.code == 401:
            # Unauthorized - bad token
            session.pop('access_token', None)
            return redirect(url_for('login'))
        return res.read()
    print("in passed the first try")

    try:
        import json
        response = res.read()
        data = json.loads(response)
        email = data["email"]
        user = db_session.query(User).filter(User.email==email).one()
        print(user.as_dict())
    except ValueError:
        print("Could not parse json response from Google")
    
    return redirect('http://localhost:3000/user/%s' % user.identifier)
 
 
@app.route('/api/login')
def login():
    print("in login")
    callback=url_for('authorized', _external=True)<commit_msg>adjust location of login redirect<commit_after>import os
from collections import deque
from flask import Flask, redirect, url_for, session, redirect, jsonify, request
from flask_oauth import OAuth
from flask_cors import CORS, cross_origin
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, Load
from models import Character, User, Draft, DraftTicket
from flask_uwsgi_websocket import GeventWebSocket
from game import handle_event, can_draft
from flask import Flask
from flask_dotenv import DotEnv
import json
import redis
import os


app = Flask(__name__)
env = DotEnv()
env_file = '/var/www/fantasyx/.env' if os.path.exists('/var/www/fantasyx/.env') else '.env'
env.init_app(app, env_file=env_file, verbose_mode=True)

# You must configure these 3 values from Google APIs console
# https://code.google.com/apis/console
REDIRECT_URI = '/api/oauth2callback'  # one of the Redirect URIs from Google APIs console
 
SECRET_KEY = 'development key'
DEBUG = True
 
CORS(app)
app.debug = DEBUG
app.secret_key = SECRET_KEY
websocket = GeventWebSocket(app)
oauth = OAuth()
 
google = oauth.remote_app('google',
                          base_url='https://www.google.com/accounts/',
                          authorize_url='https://accounts.google.com/o/oauth2/auth',
                          request_token_url=None,
                          request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
                                                'response_type': 'code',
                                                'access_type':'offline',
                                                'prompt': 'consent'
                          },
                          access_token_url='https://accounts.google.com/o/oauth2/token',
                          access_token_method='POST',
                          access_token_params={'grant_type': 'authorization_code'},
                          consumer_key=app.config['GOOGLE_CLIENT_ID'],
                          consumer_secret=app.config['GOOGLE_CLIENT_SECRET'],
                          
)

if app.config['FLASK_ENV'] == 'development':
    engine=create_engine('postgresql://admin:admin@localhost:5432/fantasyx')
else:
    engine=create_engine('postgresql://%(DB_USERNAME)s:%(DB_PASSWORD)s@%(DB_ENDPOINT)s:5432/fantasyx' % app.config)

db_session = Session(bind=engine)

@app.route('/api/')
def index():
    print("in index")
    access_token = session.get('access_token')
    if access_token is None:
        return redirect(url_for('login'))
 
    access_token = access_token[0]
    from urllib2 import Request, urlopen, URLError
 
    headers = {'Authorization': 'OAuth '+access_token}
    req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
                  None, headers)
    try:
        res = urlopen(req)
        print("response:", res)
    except URLError as e:
        if e.code == 401:
            # Unauthorized - bad token
            session.pop('access_token', None)
            return redirect(url_for('login'))
        return res.read()
    print("in passed the first try")

    try:
        import json
        response = res.read()
        data = json.loads(response)
        email = data["email"]
        user = db_session.query(User).filter(User.email==email).one()
        print(user.as_dict())
    except ValueError:
        print("Could not parse json response from Google")

    if app.config['FLASK_ENV'] == 'development':
        endpoint = 'localhost:3000'
    else:
        endpoint = 'lot.emlprime.com'
    return redirect('http://%s/user/%s' % (endpoint, user.identifier))
 
 
@app.route('/api/login')
def login():
    print("in login")
    callback=url_for('authorized', _external=True)
{'fraction_non_alphanumeric': 0.11357720235648719, 'fraction_numerical': 0.0063022331826277575, 'mean_word_length': 2.4894837476099427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3057873', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2091, 'n_words': 464}