2012-09-08 17:37:51 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import with_statement, division
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from urllib import unquote
|
|
|
|
from threading import Thread
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import ox
|
|
|
|
|
|
|
|
class Rhythmbox(object):
|
|
|
|
tracks = []
|
|
|
|
locations = {}
|
|
|
|
def __init__(self, xml):
|
|
|
|
self.xml = os.path.expanduser(xml)
|
|
|
|
t = Thread(target=self.parse_xml, args=[])
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.xml
|
|
|
|
|
|
|
|
def parse_xml(self):
|
|
|
|
from lxml import etree
|
|
|
|
with open(self.xml) as f:
|
|
|
|
self.library = etree.fromstring(f.read())
|
|
|
|
self.tracks = self.load_tracks()
|
|
|
|
|
|
|
|
def load_tracks(self):
|
|
|
|
tracks = []
|
|
|
|
keys = [
|
2012-09-08 17:43:20 +00:00
|
|
|
'id', 'name', 'artist', 'album', 'year', 'duration', 'size',
|
2012-09-08 17:37:51 +00:00
|
|
|
'sortArtist', 'albumArtist', 'sortAlbumArtist', 'compliation',
|
|
|
|
]
|
|
|
|
key_map = {
|
|
|
|
'title': 'name',
|
|
|
|
'file-size': 'size',
|
|
|
|
}
|
|
|
|
for e in self.library.xpath('//entry[@type="song"]'):
|
|
|
|
item = {}
|
|
|
|
path = e.find('location').text.split('file://')[-1]
|
|
|
|
path = unquote(path)
|
|
|
|
item['id'] = ox.oshash(path)
|
|
|
|
self.locations[item['id']] = path
|
|
|
|
for c in e:
|
|
|
|
if c.tag in keys + key_map.keys():
|
|
|
|
key = key_map.get(c.tag, c.tag)
|
|
|
|
if key == 'duration':
|
|
|
|
item[key] = int(c.text)
|
|
|
|
else:
|
|
|
|
item[key] = c.text
|
|
|
|
info = ox.avinfo(path)
|
|
|
|
metadata = info.get('metadata', {})
|
|
|
|
if 'date' in metadata:
|
|
|
|
item['year'] = metadata['date']
|
|
|
|
tracks.append(item)
|
|
|
|
return tracks
|
|
|
|
|
|
|
|
def track(self, track_id):
|
|
|
|
return self.locations.get(track_id)
|
|
|
|
|
|
|
|
def path():
|
|
|
|
path = os.path.expanduser('~/.local/share/rhythmbox/rhythmdb.xml')
|
|
|
|
if not os.path.exists(path):
|
|
|
|
path = None
|
|
|
|
return path
|