Spelling
[pyTivo.git] / plugins / music / music.py
blob4dd53bd02ae190f39569812d8508b6eb5266d678
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'
17 AUDIO = 'audio'
18 DIRECTORY = 'dir'
20 playable_cache = {}
21 playable_cache = LRUCache(1000)
22 media_data_cache = LRUCache(100)
24 def QueryContainer(self, handler, query):
26 subcname = query['Container'][0]
27 cname = subcname.split('/')[0]
28 local_base_path = self.get_local_base_path(handler, query)
30 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
31 handler.send_response(404)
32 handler.end_headers()
33 return
35 def AudioFileFilter(file, filter_type = None):
37 if filter_type:
38 filter_start = filter_type.split('/')[0]
39 else:
40 filter_start = filter_type
42 if file not in self.playable_cache:
43 if os.path.isdir(file):
44 self.playable_cache[file] = self.DIRECTORY
46 elif eyeD3.isMp3File(file):
47 self.playable_cache[file] = self.AUDIO
48 else:
49 self.playable_cache[file] = False
51 if filter_start == self.AUDIO:
52 if self.playable_cache[file] == self.AUDIO:
53 return self.playable_cache[file]
54 else:
55 return False
56 else:
57 return self.playable_cache[file]
60 def media_data(file):
61 dict = {}
62 dict['path'] = file
63 dict['part_path'] = file.replace(local_base_path, '')
64 dict['name'] = os.path.split(file)[1]
65 dict['is_dir'] = os.path.isdir(file)
67 if file in self.media_data_cache:
68 return self.media_data_cache[file]
70 if os.path.isdir(file) or not eyeD3.isMp3File(file):
71 self.media_data_cache[file] = dict
72 return dict
74 try:
75 audioFile = eyeD3.Mp3AudioFile(file)
76 dict['Duration'] = audioFile.getPlayTime() * 1000
77 dict['SourceBitRate'] = audioFile.getBitRate()[1]
78 dict['SourceSampleRate'] = audioFile.getSampleFreq()
80 tag = audioFile.getTag()
81 dict['ArtistName'] = str(tag.getArtist())
82 dict['AlbumTitle'] = str(tag.getAlbum())
83 dict['SongTitle'] = str(tag.getTitle())
84 dict['AlbumYear'] = tag.getYear()
86 dict['MusicGenre'] = tag.getGenre().getName()
87 except:
88 pass
90 self.media_data_cache[file] = dict
91 return dict
93 handler.send_response(200)
94 handler.end_headers()
95 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
96 t.name = subcname
97 t.container = cname
98 t.files, t.total, t.start = self.get_files(handler, query, AudioFileFilter)
99 t.files = map(media_data, t.files)
100 t.quote = quote
101 t.escape = escape
102 handler.wfile.write(t)