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
9 SCRIPTDIR
= os
.path
.dirname(__file__
)
15 CONTENT_TYPE
= 'x-container/tivo-music'
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)
35 def AudioFileFilter(file, filter_type
= None):
38 filter_start
= filter_type
.split('/')[0]
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
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]
57 return self
.playable_cache
[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
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()
90 self
.media_data_cache
[file] = dict
93 handler
.send_response(200)
95 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
98 t
.files
, t
.total
, t
.start
= self
.get_files(handler
, query
, AudioFileFilter
)
99 t
.files
= map(media_data
, t
.files
)
102 handler
.wfile
.write(t
)