#!/usr/bin/env python
# (C) by Robby Stephenson, http://periapsis.org, 2006
# GPL v2
# Script for convertin amarok database into Tellico data file
# http://periapsis.org/tellico/
# http://amarok.kde.org
import os
import sys
import string
import locale
import md5
import base64
albumnames = {}
artistnames = {}
genrenames = {}
yearnames = {}
coverdir = ""
images = set()
def esc(s):
return s.replace('&', '&').replace('<', '<').replace('>', '>')
def get_album_info():
cmd = """dcop amarok collection query "SELECT id,';',name,'|' from album"|tr -d "\n"|sed -e 's/|/\\n/g' """
lines = os.popen(cmd).readlines()
for line in lines:
words = string.split(line.rstrip("\n"), ";")
albumnames[words[0]] = words[1]
def get_artist_info():
cmd = """dcop amarok collection query "SELECT id,';',name,'|' from artist"|tr -d "\n"|sed -e 's/|/\\n/g' """
lines = os.popen(cmd).readlines()
for line in lines:
words = string.split(line.rstrip("\n"), ";")
artistnames[words[0]] = words[1]
def get_genre_info():
cmd = """dcop amarok collection query "SELECT id,';',name,'|' from genre"|tr -d "\n"|sed -e 's/|/\\n/g' """
lines = os.popen(cmd).readlines()
for line in lines:
words = string.split(line.rstrip("\n"), ";")
genrenames[words[0]] = words[1]
def get_year_info():
cmd = """dcop amarok collection query "SELECT id,';',name,'|' from year"|tr -d "\n"|sed -e 's/|/\\n/g' """
lines = os.popen(cmd).readlines()
for line in lines:
words = string.split(line.rstrip("\n"), ";")
yearnames[words[0]] = words[1]
def get_tracks():
albumdict = {}
cmd = """dcop amarok collection query "SELECT album,';',title,';',artist,';',track,';',length,';',year,';',genre,'|' FROM tags"|tr -d "\n"|sed -e 's/|/\\n/g' """
lines = os.popen(cmd).readlines()
for line in lines:
words = string.split(line.rstrip("\n"), ";")
id = words[0]
title = words[1]
artist = words[2]
if artistnames.has_key(artist):
artist = artistnames[artist]
if len(words[3]) == 0:
continue
track = "%03d" % int(words[3])
length = words[4]
year = words[5]
genre = words[6]
info = { "title" : title, "artist" : artist, "length" : length }
if albumdict.has_key(id):
albumdict[id]["tracks"][track] = info
else:
albumdict[id] = {"tracks" : {track : info} }
if albumdict[id].has_key("artist"):
if albumdict[id]["artist"] != artist:
albumdict[id]["artist"] = "(Various)"
else:
albumdict[id]["artist"] = artist
if albumdict[id].has_key("year"):
if albumdict[id]["year"] != year:
albumdict[id]["year"] = ""
else:
albumdict[id]["year"] = year
if albumdict[id].has_key("genre"):
if albumdict[id]["genre"] != genre:
albumdict[id]["genre"] = ""
else:
albumdict[id]["genre"] = genre
return albumdict
def get_albums():
get_artist_info()
get_album_info()
get_genre_info()
get_year_info()
albumdict = get_tracks()
return albumdict
def print_entry(id, album):
year = album["year"]
if len(year) > 0:
year = esc(yearnames[year])
genre = album["genre"]
if len(genre) > 0:
genre = esc(genrenames[genre])
out = "\n%s\n%s\n%s\n%s\n" \
% (id, esc(albumnames[id]), esc(album["artist"]), year, genre)
imagename = md5.new( "%s%s" % (album["artist"].lower(), albumnames[id].lower()) ).hexdigest()
if len(imagename) > 0 and os.path.isfile(coverdir + imagename):
out += "%s.PNG\n" % imagename
images.add(imagename)
out += "\t\n"
tracks = album["tracks"].keys()
tracks.sort()
currTrack = 1
for num in tracks:
while currTrack < int(num):
out += "\n"
currTrack += 1
info = album["tracks"][num]
mins,secs = divmod(int(info["length"]), 60)
out += "\t\t\n" % (esc(info["title"]), esc(info["artist"]), mins, secs)
currTrack = int(num) + 1
out += "\t\n"
out += "\n"
return out
def generate_images():
out = "\n"
for img in images:
filename = coverdir + img
try:
file = open(filename, "rb")
data = file.read();
out += "\n" % (img + '.PNG')
out += base64.encodestring(data)
out += '\n'
except IOError:
pass
out += "\n"
return out
def generate_xml():
out = """
"""
albums = get_albums()
for id in albums.keys():
out += print_entry(id, albums[id])
out += generate_images()
out += "\n"
return out
if __name__ == "__main__":
coverdir = os.environ['KDEHOME'] + "/share/apps/amarok/albumcovers/large/"
xml = generate_xml()
lang, enc = locale.getdefaultlocale()
print xml.decode(enc).encode('utf-8')