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__
)
13 content_type
= 'x-container/tivo-music'
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)
28 path
= self
.get_local_path(handler
, query
)
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
41 self
.playable_cache
[full_path
] = False
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
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()
73 self
.media_data_cache
[file] = dict
76 handler
.send_response(200)
78 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
80 t
.files
, t
.total
, t
.start
= self
.get_files(handler
, query
, AudioFileFilter
)
81 t
.files
= map(media_data
, t
.files
)
85 handler
.wfile
.write(t
)