def getSession(request, data):
    token = Token.load(data['token'])
    if not token:
        print "Invalid token"
        return "NOPE"

    if not token.user:
        print "Token not validated"
        return "NOPE"

    print "GRANTING SESSION for token %s" % token.token
    token.consume()
    session = Session.create(token.user)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status="ok"):
        with tag('session'):
            with tag('name'):
                text(session.user.name)
            with tag('key'):
                text(session.id)
            with tag('subscriber'):
                text('0')

    return '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue())
def now_playing(request, data):
    sk = data['sk']
    session = Session.load(sk)
    if not session:
        print "Invalid session"
        return "NOPE"

    track   = data['track']
    artist  = data['artist']
    album   = data['album']
    albumArtist   = data['albumArtist']
    
    print "NOW PLAYING- User: %s, Artist: %s, Track: %s, Album: %s"  \
        % (session.user.name, artist, track, album)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status="ok"):
        with tag('nowplaying'):
            with tag('track', corrected="0"):
                text(track)
            with tag('artist', corrected="0"):
                text(artist)
            with tag('album', corrected="0"):
                text(album)
            with tag('albumArtist', corrected="0"):
                text(albumArtist)
            with tag('ignoredMessage', code="0"):
                text('')

    return '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue())
def scrobble(request, data):
    sk = data['sk']
    session = Session.load(sk)
    if not session:
        print "Invalid session"
        return "NOPE"

    # FUUUUUUU PHP ARRAYS
    lookup = defaultdict(dict)
    for key, value in data.items():
        matches = re.match('(.*)\[(\d+)\]', key)
        if matches:
            key = matches.group(1)
            number = matches.group(2)
        else:
            number = 0
        lookup[number][key] = value

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status="ok"):
        with tag('scrobbles'):

            for _, dataset in lookup.items():
                if 'track' not in dataset:
                    continue

                artist      = dataset['artist']
                track       = dataset['track']
                album       = dataset['album']
                albumArtist = dataset['albumArtist']
                timestamp   = dataset['timestamp']

                print "SCROBBLE- User: %s, Artist: %s, Track: %s, Album: %s"  \
                    % (session.user.name, artist, track, album)

                session.user.scrobble(timestamp, artist, track, album, albumArtist)

                with tag('scrobble'):
                    with tag('track', corrected="0"):
                        text(track)
                    with tag('artist', corrected="0"):
                        text(artist)
                    with tag('album', corrected="0"):
                        text(album)
                    with tag('albumArtist', corrected="0"):
                        text(albumArtist)
                    with tag('timestamp', corrected="0"):
                        text(timestamp)
                    with tag('ignoredMessage', code="0"):
                        text('')

    return '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue())
def user_info(request, data):
    sk = data['sk']
    session = Session.load(sk)
    if not session:
        print "Invalid session"
        return "NOPE"

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status="ok"):
        with tag('user'):
            with tag('name'):
                text(session.user.name)
            with tag('realname'):
                text(session.user.name)
            with tag('url'):
                text('http://foo.bar/user/' + session.user.name)
            with tag('playcount'):
                text('9001')
            with tag('registered', unixtime=str(time)):
                text(session.user.timestamp)

    return '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue())
def getSessionInfo(request, data):
    sk  = data['sk']
    
    session = Session.load(sk)
    if not session:
        print "Invalid session"
        return "NOPE"

    print "SESSION INFO for session %s, user %s" % (session.id, session.user.name)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status="ok"):
        with tag('application'):
            with tag('session'):
                with tag('name'):
                    text(session.user.name)
                with tag('key'):
                    text(session.id)
                with tag('subscriber'):
                    text('0')
            with tag('country'):
                text('US')

    return '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue())