Cleened up content types
[pyTivo.git] / plugins / music / music.py
blobe28af0683aaf6abaa2d4efb8fb494a6c2fa91c1c
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_NAME = 'Music'
13 class Music(Plugin):
15 CONTENT_TYPE = 'x-container/tivo-music'
16 playable_cache = {}
17 playable_cache = LRUCache(1000)
18 media_data_cache = LRUCache(100)
20 def QueryContainer(self, handler, query):
22 subcname = query['Container'][0]
23 cname = subcname.split('/')[0]
25 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
26 handler.send_response(404)
27 handler.end_headers()
28 return
30 path = self.get_local_path(handler, query)
31 def isdir(file):
32 return os.path.isdir(os.path.join(path, file))
34 def AudioFileFilter(file):
35 full_path = os.path.join(path, file)
37 if full_path in self.playable_cache:
38 return self.playable_cache[full_path]
39 if os.path.isdir(full_path) or eyeD3.isMp3File(full_path):
40 self.playable_cache[full_path] = True
41 return True
42 else:
43 self.playable_cache[full_path] = False
44 return False
46 def media_data(file):
47 dict = {}
48 dict['path'] = file
50 file = os.path.join(path, file)
52 if file in self.media_data_cache:
53 return self.media_data_cache[file]
55 if isdir(file) or not eyeD3.isMp3File(file):
56 self.media_data_cache[file] = dict
57 return dict
59 try:
60 audioFile = eyeD3.Mp3AudioFile(file)
61 dict['Duration'] = audioFile.getPlayTime() * 1000
62 dict['SourceBitRate'] = audioFile.getBitRate()[1]
63 dict['SourceSampleRate'] = audioFile.getSampleFreq()
65 tag = audioFile.getTag()
66 dict['ArtistName'] = str(tag.getArtist())
67 dict['AlbumTitle'] = str(tag.getAlbum())
68 dict['SongTitle'] = str(tag.getTitle())
69 dict['AlbumYear'] = tag.getYear()
71 dict['MusicGenre'] = tag.getGenre().getName()
72 except:
73 pass
75 self.media_data_cache[file] = dict
76 return dict
78 handler.send_response(200)
79 handler.end_headers()
80 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
81 t.name = subcname
82 t.files, t.total, t.start = self.get_files(handler, query, AudioFileFilter)
83 t.files = map(media_data, t.files)
84 t.isdir = isdir
85 t.quote = quote
86 t.escape = escape
87 handler.wfile.write(t)