<commit_before>from datetime import datetime
import md5
import random

from django.http import HttpResponse

from gobbler.models import Album, Artist, Gobble, GobblerUser, Md5Password, NowPlaying, Session, Track
from web.views import frontpage


def handshake_11(request):
    g = request.GET
    try:
        client_id,client_version,user = g['c'], g['v'], g['u']
    except KeyError:
        return HttpResponse("FAILED The request was malformed\nINTERVAL 0\n")
    challenge = md5.new(str(random.random())).hexdigest()
    try:
        guser = GobblerUser.objects.get(username=user)
    except GobblerUser.DoesNotExist:
        return HttpResponse("BADUSER\nINTERVAL 0\n")
    session,created = Session.objects.get_or_create(user=guser)
    session.key = challenge
    session.save()
    return HttpResponse("\n".join(["UPTODATE",
                                   challenge,
                                   "http://127.0.0.1:80/protocol_1.1/",
                                   "INTERVAL 0"]))


def handshake_12(request):
    g = request.GET
    try:
        client_id,client_version,user,timestamp,auth = (
            g['c'], g['v'], g['u'], g['t'], g['a'])
    except KeyError:
        return HttpResponse("FAILED The request was malformed\n")
    try:
        guser = GobblerUser.objects.get(username=user)
    except GobblerUser.DoesNotExist:
        return HttpResponse("BADAUTH\n")
    expected_token = md5.new(guser.get_md5() + timestamp)
    if auth != expected_token.hexdigest():
        return HttpResponse("BADAUTH\n")
    session_id = expected_token.copy()
    session_id.update(str(datetime.now()))
    session,created = Session.objects.get_or_create(user=guser)
    session.key = session_id.hexdigest()
    session.save()
    return HttpResponse("\n".join(["OK",
                                   session_id.hexdigest(),
                                   "http://127.0.0.1:80/nowplaying/",
                                   "http://127.0.0.1:80/protocol_1.2/"]))


def index(request):
    g = request.GET
    handshake = g.get('hs', None)
    if handshake:
        try:
            protocol_version = g['p']
        except KeyError:
            return HttpResponse("FAILED The request was malformed\n")
        if protocol_version == "1.2" or protocol_version == "1.2.1":
            return handshake_12(request)
        elif protocol_version == "1.1":
            return handshake_11(request)
        else:
            return HttpResponse("FAILED Unsupported protocol version\n")
    else:<commit_msg>Use sites framework to determine submit URLs.<commit_after>from datetime import datetime
import md5
import random

from django.contrib.sites.models import Site
from django.http import HttpResponse

from gobbler.models import Album, Artist, Gobble, GobblerUser, Md5Password, NowPlaying, Session, Track
from web.views import frontpage


def handshake_11(request):
    g = request.GET
    try:
        client_id,client_version,user = g['c'], g['v'], g['u']
    except KeyError:
        return HttpResponse("FAILED The request was malformed\nINTERVAL 0\n")
    challenge = md5.new(str(random.random())).hexdigest()
    try:
        guser = GobblerUser.objects.get(username=user)
    except GobblerUser.DoesNotExist:
        return HttpResponse("BADUSER\nINTERVAL 0\n")
    session,created = Session.objects.get_or_create(user=guser)
    session.key = challenge
    session.save()
    domain = Site.objects.get_current().domain
    return HttpResponse("\n".join(["UPTODATE",
                                   challenge,
                                   "http://%s/protocol_1.1/" % (domain,),
                                   "INTERVAL 0"]))


def handshake_12(request):
    g = request.GET
    try:
        client_id,client_version,user,timestamp,auth = (
            g['c'], g['v'], g['u'], g['t'], g['a'])
    except KeyError:
        return HttpResponse("FAILED The request was malformed\n")
    try:
        guser = GobblerUser.objects.get(username=user)
    except GobblerUser.DoesNotExist:
        return HttpResponse("BADAUTH\n")
    expected_token = md5.new(guser.get_md5() + timestamp)
    if auth != expected_token.hexdigest():
        return HttpResponse("BADAUTH\n")
    session_id = expected_token.copy()
    session_id.update(str(datetime.now()))
    session,created = Session.objects.get_or_create(user=guser)
    session.key = session_id.hexdigest()
    session.save()
    domain = Site.objects.get_current().domain
    return HttpResponse("\n".join(["OK",
                                   session_id.hexdigest(),
                                   "http://%s/nowplaying/" % (domain,),
                                   "http://%s/protocol_1.2/" % (domain,)]))


def index(request):
    g = request.GET
    handshake = g.get('hs', None)
    if handshake:
        try:
            protocol_version = g['p']
        except KeyError:
            return HttpResponse("FAILED The request was malformed\n")
        if protocol_version == "1.2" or protocol_version == "1.2.1":
            return handshake_12(request)
        elif protocol_version == "1.1":
            return handshake_11(request)
        else:
            return HttpResponse("FAILED Unsupported protocol version\n")
    else:
{'fraction_non_alphanumeric': 0.12163452358220356, 'fraction_numerical': 0.014894023295780026, 'mean_word_length': 2.3924870466321244, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, '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': '87068', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1522, 'n_words': 322}