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'
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)
30 path
= self
.get_local_path(handler
, query
)
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
43 self
.playable_cache
[full_path
] = False
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
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()
75 self
.media_data_cache
[file] = dict
78 handler
.send_response(200)
80 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
82 t
.files
, t
.total
, t
.start
= self
.get_files(handler
, query
, AudioFileFilter
)
83 t
.files
= map(media_data
, t
.files
)
87 handler
.wfile
.write(t
)