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

Commit 0d75558a authored by Jonas Haraldsson's avatar Jonas Haraldsson

Merge commit 'refs/merge-requests/57' of git://gitorious.org/foocorp/gnu-fm into merge-requests/57

parents d0be37ff 19edc572
......@@ -3,7 +3,9 @@
# Jamendo database dumps can be fetched from: http://img.jamendo.com/data/dbdump_artistalbumtrack.xml.gz
import xml.etree.cElementTree as ElementTree
import sys, gzip, time, os, os.path, urllib, threading
import sys, gzip, time, os, os.path, urllib, threading, statvfs
JAMENDO_DUMP_URL="http://img.jamendo.com/data/dbdump_artistalbumtrack.xml.gz"
MAX_THREADS = 10
running_threads = 0
......@@ -30,9 +32,15 @@ class DownloadJamendo:
if not os.path.exists(destination):
os.mkdir(destination)
self.destination = destination
self.MAX_FILENAME_LENGTH = os.statvfs(destination)[statvfs.F_NAMEMAX]
print "Downloading Jamendo dump from %s" % JAMENDO_DUMP_URL
(filename, headers) = urllib.urlretrieve(JAMENDO_DUMP_URL, os.path.join(destination, "dbdump_artistalbumtrack.xml.gz"))
print "Jamendo dump saved: %s" % filename
self.dump = gzip.open(filename, "r")
def parse(self, dump):
dump = dump or self.dump
for event, elem in ElementTree.iterparse(dump):
if elem.tag == "artist":
artist = self.proc_artist(elem)
......@@ -98,16 +106,18 @@ class DownloadJamendo:
for track in album["tracks"]:
if track["id"] and track["name"] and album["name"] and artist["name"] and self.free_license(track["license"]):
trackurl = "http://api.jamendo.com/get2/stream/track/redirect/?id=%d&streamencoding=ogg2" % track["id"]
trackfile = os.path.join(self.destination, "%s-%s-%s.ogg" % (artist["name"].replace("/", ""), album["name"].replace("/", ""), track["name"].replace("/", " ")))
trackfile = trackfile.encode('utf-8')
if os.path.exists(trackfile):
print "Already downloaded track %s" % trackfile
trackfile = "%s-%s-%s-%s" % (track["id"], artist["name"].replace("/", ""), album["name"].replace("/", ""), track["name"].replace("/", " "))
trackfile = "%s.ogg" % trackfile.encode('utf-8')[:self.MAX_FILENAME_LENGTH-4].decode('utf-8','ignore').encode('utf-8')
trackfilepath = os.path.join(self.destination, trackfile)
if os.path.exists(trackfilepath):
print "Already downloaded track %s" % trackfilepath
else:
while running_threads > MAX_THREADS:
time.sleep(5)
print "Downloading %s to %s" % (trackurl, trackfile)
d = Downloader(trackfile, trackurl)
print "Downloading %s to %s" % (trackurl, trackfilepath)
d = Downloader(trackfilepath, trackurl)
d.start()
os.symlink(trackfile, os.path.join(self.destination, "%s.ogg2" % track["id"]))
......@@ -119,14 +129,19 @@ class DownloadJamendo:
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: download-jamendo.py <database dump> <destination>"
if len(sys.argv) != 2:
print "Usage: download-jamendo.py [<database dump>] <destination>"
sys.exit(1)
if sys.argv[1][-2:] == "gz":
dump = gzip.open(sys.argv[1], "r")
if len(sys.argv) == 3:
destination = sys.argv[2]
if sys.argv[1][-2:] == "gz":
dump = gzip.open(sys.argv[1], "r")
else:
dump = open(sys.argv[1], "r")
else:
dump = open(sys.argv[1], "r")
destination = sys.argv[1]
dump = None
downloader = DownloadJamendo(sys.argv[2])
downloader = DownloadJamendo(destination)
downloader.parse(dump)
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