Skip to content

Instantly share code, notes, and snippets.

@messa
Forked from Visgean/getTags.py
Created Jul 27, 2010
Embed
What would you like to do?
#! /usr/bin/python
# -*- coding: UTF-8 -*-
import settings
import urllib
import xml.etree.ElementTree as etree
class getTags:
def __init__(self):
self.tracks = {#name:(artist,playcount)
}
self.totalPages = 0
self.parseTracks(self.makeTracksUrl()) # We need to parse number of pages...
#for i in range(1,3): # self.totalPages + 1
# self.parseTracks(self.makeTracksUrl(i))
def getTotalPages(self):
"Method for getting number of pages"
urlObj = urllib.urlopen(self.makeTracksUrl)
data = urlObj.read()
tree = etree.fromstring(data)
if tree.get("status") != "ok":
raise BaseException
else:
tracks = tree.find("tracks")
self.totalPages = tracks.get("totalPages")
def makeTracksUrl(self, page = 1):
url = "http://ws.audioscrobbler.com/2.0/?method=library.gettracks&api_key=%s&user=%s&page=%s" % (
settings.apiKey, settings.userName,page)
return url
def parseTracks(self, url):
urlObj = urllib.urlopen(url)
data = urlObj.read()
tree = etree.fromstring(data)
if tree.get("status") != "ok":
raise BaseException
else:
tracks = tree.find("tracks")
for track in tracks.findall("track"):
print track.find("name").text
getTags()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment