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
from urllib2 import urlopen
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
if server[:7] != "http://":
server = "http://%s" % (server,)
password = getpass.getpass()
timestamp = int(time.time())
token = md5hash(md5hash(password).hexdigest() + str(timestamp)).hexdigest()
auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=imp" % (server, username,
timestamp, token)
response = urlopen(auth_url).read()
lines = response.split("\n")
if lines[0] != "OK":
parser.error("Server returned: %s" % (response,))
session_id = lines[1]
submit_url = lines[3]
def post(post_data):
post_data.append(('s', session_id))
response = urlopen(submit_url, urlencode(post_data)).read()
if response != "OK\n":
parser.error("Server returned: %s" % (response,))
i = 0
post_data = []
for line in file(data):
artist,track,timestamp = line.strip().split("\t")
dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
timestamp = str(int(time.mktime(dt.utctimetuple())))
post_data += [('a[%d]' % i, artist), ('t[%d]' % i, track),
('i[%d]' % i, timestamp)]
print "Adding to post %s playing %s" % (artist, track)
i += 1
if i > 49:
print "Posting..."
post(post_data)
i = 0
post_data = []
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 = []
time.sleep(1)
if len(post_data) > 0:
post(post_data)
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()
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