<commit_before>import logging
import urllib
import base64

from hashlib import md5
from urllib import quote
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from django.utils import simplejson
from model import get_current_youtify_user_model

try:
    import config
except ImportError:
    import config_template as config

VALIDATE_CERTIFICATE = True

def lastfm_request(method, t, options, user = None):
    options.update({
        'method': method,
        'api_key': '<KEY>'
    })

    if user:
        options['sk'] = user.lastfm_access_token

    keys = options.keys()

    keys.sort()

    signature = reduce(lambda s, v: s + v, [k + options[k].encode('utf8') for k in keys]) + '75dada4b3f3794de3be2db291c20528b'

    signature = md5(signature).hexdigest()

    options = reduce(lambda s, v: '%s&%s' % (s, v), ['%s=%s' % (k, quote(options[k].encode('utf8'))) for k in keys])

    url = 'http://ws.audioscrobbler.com/2.0/?%s&format=json&api_sig=%s' % (options, signature)

    http_method = urlfetch.GET if t == 'GET' else urlfetch.POST # TODO: Fix this

    try:
        response = urlfetch.fetch(url=url, method=http_method, validate_certificate=VALIDATE_CERTIFICATE)
        return simplejson.loads(response.content)
    except e:
        return simplejson.loads({
            'message': 'urlfetch failed ' + str(e)
        })

class ConnectHandler(webapp.RequestHandler):
    """Initiate the Last.fm authentication dance"""
    def get(self):
        redirect_uri = self.request.get('redirect_uri')

        if redirect_uri and redirect_uri != 'deleted':
            self.response.headers['Set-Cookie'] = 'redirect_uri=' + redirect_uri

        url = 'http://www.last.fm/api/auth/?api_key=db8b8dccb3afd186b6df786775a62cb5&cb=' + config.LASTFM_REDIRECT_URL

        self.redirect(url)

class DisconnectHandler(webapp.RequestHandler):
    """Remove the current user's Last.fm access token"""
    def get(self):
        redirect_uri = self.request.get('redirect_uri', '/')

        user = get_current_youtify_user_model()

        user.lastfm_user_name = None<commit_msg>Fix "global name 'e' is not defined"<commit_after>import logging
import urllib
import base64

from hashlib import md5
from urllib import quote
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from django.utils import simplejson
from model import get_current_youtify_user_model

try:
    import config
except ImportError:
    import config_template as config

VALIDATE_CERTIFICATE = True

def lastfm_request(method, t, options, user = None):
    options.update({
        'method': method,
        'api_key': '<KEY>'
    })

    if user:
        options['sk'] = user.lastfm_access_token

    keys = options.keys()

    keys.sort()

    signature = reduce(lambda s, v: s + v, [k + options[k].encode('utf8') for k in keys]) + '75dada4b3f3794de3be2db291c20528b'

    signature = md5(signature).hexdigest()

    options = reduce(lambda s, v: '%s&%s' % (s, v), ['%s=%s' % (k, quote(options[k].encode('utf8'))) for k in keys])

    url = 'http://ws.audioscrobbler.com/2.0/?%s&format=json&api_sig=%s' % (options, signature)

    http_method = urlfetch.GET if t == 'GET' else urlfetch.POST # TODO: Fix this

    try:
        response = urlfetch.fetch(url=url, method=http_method, validate_certificate=VALIDATE_CERTIFICATE)
        return simplejson.loads(response.content)
    except Exception:
        return simplejson.loads({
            'message': 'urlfetch failed'
        })

class ConnectHandler(webapp.RequestHandler):
    """Initiate the Last.fm authentication dance"""
    def get(self):
        redirect_uri = self.request.get('redirect_uri')

        if redirect_uri and redirect_uri != 'deleted':
            self.response.headers['Set-Cookie'] = 'redirect_uri=' + redirect_uri

        url = 'http://www.last.fm/api/auth/?api_key=db8b8dccb3afd186b6df786775a62cb5&cb=' + config.LASTFM_REDIRECT_URL

        self.redirect(url)

class DisconnectHandler(webapp.RequestHandler):
    """Remove the current user's Last.fm access token"""
    def get(self):
        redirect_uri = self.request.get('redirect_uri', '/')

        user = get_current_youtify_user_model()

        user.lastfm_user_name = None
{'fraction_non_alphanumeric': 0.12012912151256629, 'fraction_numerical': 0.01936822688494351, 'mean_word_length': 3.5953389830508473, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3765017', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1391, 'n_words': 326}