Commit 1a3ac068 authored by Daniel Watkins's avatar Daniel Watkins

import.py now uses a GobbleServer object.

parent 45928cd2
...@@ -14,50 +14,74 @@ from urllib import urlencode ...@@ -14,50 +14,74 @@ from urllib import urlencode
from urllib2 import urlopen from urllib2 import urlopen
usage = "%prog <USERNAME> <SERVER> <SCROBBLE DUMP>" class GobbleException(Exception):
parser = OptionParser(usage=usage)
opts,args = parser.parse_args() pass
if len(args) != 3:
parser.error("All arguments are required.")
class GobbleServer(object):
username,server,data = args
if server[:7] != "http://": def __init__(self, server_name, username, password):
server = "http://%s" % (server,) if server_name[:7] != "http://":
password = getpass.getpass() server_name = "http://%s" % (server_name,)
self.name = server_name
timestamp = int(time.time()) self.password = password
token = md5hash(md5hash(password).hexdigest() + str(timestamp)).hexdigest() self.post_data = []
auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=imp" % (server, username, self.session_id = None
timestamp, token) self.submit_url = None
response = urlopen(auth_url).read() self.username = username
lines = response.split("\n") self._handshake()
if lines[0] != "OK":
parser.error("Server returned: %s" % (response,))
session_id = lines[1] def _handshake(self):
submit_url = lines[3] timestamp = int(time.time())
token = (md5hash(md5hash(self.password).hexdigest()
def post(post_data): + str(timestamp)).hexdigest())
post_data.append(('s', session_id)) auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=imp" % (self.name,
response = urlopen(submit_url, urlencode(post_data)).read() self.username,
if response != "OK\n": timestamp, token)
parser.error("Server returned: %s" % (response,)) response = urlopen(auth_url).read()
lines = response.split("\n")
i = 0 if lines[0] != "OK":
post_data = [] raise GobbleException("Server returned: %s" % (response,))
for line in file(data): self.session_id = lines[1]
artist,track,timestamp = line.strip().split("\t") self.submit_url = lines[3]
dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
timestamp = str(int(time.mktime(dt.utctimetuple()))) def submit(self):
post_data += [('a[%d]' % i, artist), ('t[%d]' % i, track), if len(self.post_data) == 0:
('i[%d]' % i, timestamp)] return
print "Adding to post %s playing %s" % (artist, track) self.post_data.append(('s', self.session_id))
i += 1 response = urlopen(self.submit_url,
if i > 49: urlencode(self.post_data)).read()
print "Posting..." if response != "OK\n":
post(post_data) raise GobbleException("Server returned: %s" % (response,))
i = 0 self.post_data = []
post_data = []
time.sleep(1) time.sleep(1)
if len(post_data) > 0: def add_track(self, artist, track, dt):
post(post_data) 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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment