We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

import.py 2.72 KB
Newer Older
Daniel Watkins's avatar
Daniel Watkins committed
1 2 3
#!/usr/bin/python
from datetime import datetime
import getpass
jarkko's avatar
jarkko committed
4 5 6 7 8 9 10
# The md5 module is deprecated since Python 2.5
try:
    import hashlib
    md5hash = hashlib.md5
except ImportError:
    import md5
    md5hash = md5.new
Daniel Watkins's avatar
Daniel Watkins committed
11 12 13 14 15 16
from optparse import OptionParser
import time
from urllib import urlencode
from urllib2 import urlopen


17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class GobbleException(Exception):

    pass


class GobbleServer(object):

    def __init__(self, server_name, username, password):
        if server_name[:7] != "http://":
            server_name = "http://%s" % (server_name,)
        self.name = server_name
        self.password = password
        self.post_data = []
        self.session_id = None
        self.submit_url = None
        self.username = username
        self._handshake()


    def _handshake(self):
        timestamp = int(time.time())
        token = (md5hash(md5hash(self.password).hexdigest()
                    + str(timestamp)).hexdigest())
        auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=imp" % (self.name,
                                                               self.username,
                                                               timestamp, token)
        response = urlopen(auth_url).read()
        lines = response.split("\n")
        if lines[0] != "OK":
            raise GobbleException("Server returned: %s" % (response,))
        self.session_id = lines[1]
        self.submit_url = lines[3]

    def submit(self):
        if len(self.post_data) == 0:
            return
        self.post_data.append(('s', self.session_id))
        response = urlopen(self.submit_url,
                           urlencode(self.post_data)).read()
        if response != "OK\n":
            raise GobbleException("Server returned: %s" % (response,))
        self.post_data = []
Daniel Watkins's avatar
Daniel Watkins committed
59 60
        time.sleep(1)

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    def add_track(self, artist, track, dt):
        timestamp = str(int(time.mktime(dt.utctimetuple())))
        i = len(self.post_data) / 3
        if i > 49:
            self.submit()
            i = 0
        self.post_data += [('a[%d]' % i, artist), ('t[%d]' % i, track),
                           ('i[%d]' % i, timestamp)]


if __name__ == '__main__':
    usage = "%prog <USERNAME> <SERVER> <SCROBBLE DUMP>"
    parser = OptionParser(usage=usage)
    opts,args = parser.parse_args()
    if len(args) != 3:
        parser.error("All arguments are required.")

    username,server,data = args
    password = getpass.getpass()
    gobbler = GobbleServer(server, username, password)

    for line in file(data):
        artist,track,timestamp = line.strip().split("\t")
        dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
        gobbler.add_track(artist, track, dt)
        print "Adding to post %s playing %s" % (artist, track)
    gobbler.submit()