Fixed new lines
[pyTivo.git] / plugins / music / music.py
blobccc9fd14bb51732650f0193074e2c3bd93104fed
1 import os, socket, re, sys
2 from Cheetah.Template import Template
3 from plugin import Plugin
4 from urllib import unquote_plus, quote, unquote
5 from xml.sax.saxutils import escape
6 from lrucache import LRUCache
7 import eyeD3
9 SCRIPTDIR = os.path.dirname(__file__)
11 class music(Plugin):
13 content_type = 'x-container/tivo-music'
14 playable_cache = {}
15 playable_cache = LRUCache(1000)
16 media_data_cache = LRUCache(100)
18 def QueryContainer(self, handler, query):
20 subcname = query['Container'][0]
21 cname = subcname.split('/')[0]
23 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
24 handler.send_response(404)
25 handler.end_headers()
26 return
28 path = self.get_local_path(handler, query)
29 def isdir(file):
30 return os.path.isdir(os.path.join(path, file))
32 def AudioFileFilter(file):
33 full_path = os.path.join(path, file)
35 if full_path in self.playable_cache:
36 return self.playable_cache[full_path]
37 if os.path.isdir(full_path) or eyeD3.isMp3File(full_path):
38 self.playable_cache[full_path] = True
39 return True
40 else:
41 self.playable_cache[full_path] = False
42 return False
44 def media_data(file):
45 dict = {}
46 dict['path'] = file
48 file = os.path.join(path, file)
50 if file in self.media_data_cache:
51 return self.media_data_cache[file]
53 if isdir(file) or not eyeD3.isMp3File(file):
54 self.media_data_cache[file] = dict
55 return dict
57 try:
58 audioFile = eyeD3.Mp3AudioFile(file)
59 dict['Duration'] = audioFile.getPlayTime() * 1000
60 dict['SourceBitRate'] = audioFile.getBitRate()[1]
61 dict['SourceSampleRate'] = audioFile.getSampleFreq()
63 tag = audioFile.getTag()
64 dict['ArtistName'] = str(tag.getArtist())
65 dict['AlbumTitle'] = str(tag.getAlbum())
66 dict['SongTitle'] = str(tag.getTitle())
67 dict['AlbumYear'] = tag.getYear()
69 dict['MusicGenre'] = tag.getGenre().getName()
70 except:
71 pass
73 self.media_data_cache[file] = dict
74 return dict
76 handler.send_response(200)
77 handler.end_headers()
78 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
79 t.name = subcname
80 t.files, t.total, t.start = self.get_files(handler, query, AudioFileFilter)
81 t.files = map(media_data, t.files)
82 t.isdir = isdir
83 t.quote = quote
84 t.escape = escape
85 handler.wfile.write(t)