1 import subprocess
, os
, random
, re
, shutil
, socket
, sys
, urllib
, time
, cgi
3 from plugins
.video
.transcode
import kill
4 from Cheetah
.Template
import Template
5 from Cheetah
.Filters
import Filter
6 from plugin
import Plugin
, quote
, unquote
7 from xml
.sax
.saxutils
import escape
8 from lrucache
import LRUCache
9 from urlparse
import urlparse
12 SCRIPTDIR
= os
.path
.dirname(__file__
)
15 return config
.get('Server', 'ffmpeg')
19 PLAYLISTS
= ('.m3u', '.m3u8', '.ram', '.pls', '.b4s', '.wpl', '.asx',
22 TRANSCODE
= ('.mp4', '.m4a', '.flc', '.ogg', '.wma', '.aac', '.wav',
23 '.aif', '.aiff', '.au', '.flac')
25 # Search strings for different playlist types
26 asxfile
= re
.compile('ref +href *= *"(.+)"', re
.IGNORECASE
).search
27 wplfile
= re
.compile('media +src *= *"(.+)"', re
.IGNORECASE
).search
28 b4sfile
= re
.compile('Playstring="file:(.+)"').search
29 plsfile
= re
.compile('[Ff]ile(\d+)=(.+)').match
30 plstitle
= re
.compile('[Tt]itle(\d+)=(.+)').match
31 plslength
= re
.compile('[Ll]ength(\d+)=(\d+)').match
33 # Duration -- parse from ffmpeg output
34 durre
= re
.compile(r
'.*Duration: (.{2}):(.{2}):(.{2})\.(.),').search
36 # Preload the templates
37 tfname
= os
.path
.join(SCRIPTDIR
, 'templates', 'container.tmpl')
38 tpname
= os
.path
.join(SCRIPTDIR
, 'templates', 'm3u.tmpl')
39 folder_template
= file(tfname
, 'rb').read()
40 playlist_template
= file(tpname
, 'rb').read()
43 # subprocess is broken for me on windows so super hack
44 def patchSubprocess():
45 o
= subprocess
.Popen
._make
_inheritable
47 def _make_inheritable(self
, handle
):
48 if not handle
: return subprocess
.GetCurrentProcess()
49 return o(self
, handle
)
51 subprocess
.Popen
._make
_inheritable
= _make_inheritable
53 mswindows
= (sys
.platform
== "win32")
58 def __init__(self
, name
, isdir
):
61 self
.isplay
= os
.path
.splitext(name
)[1].lower() in PLAYLISTS
65 class EncodeUnicode(Filter
):
66 def filter(self
, val
, **kw
):
67 """Encode Unicode strings, by default in UTF-8"""
69 if kw
.has_key('encoding'):
70 encoding
= kw
['encoding']
74 if type(val
) == type(u
''):
75 filtered
= val
.encode(encoding
)
82 CONTENT_TYPE
= 'x-container/tivo-music'
88 media_data_cache
= LRUCache(300)
89 recurse_cache
= LRUCache(5)
90 dir_cache
= LRUCache(10)
92 def send_file(self
, handler
, container
, name
):
96 path
, query
= handler
.path
.split('?')
100 opts
= cgi
.parse_qs(query
)
102 seek
= int(opts
['Seek'][0])
103 if 'Duration' in opts
:
104 seek
= int(opts
['Duration'][0])
106 fname
= os
.path
.join(os
.path
.normpath(container
['path']),
107 unquote(path
)[len(name
) + 2:])
108 fname
= unicode(fname
, 'utf-8')
110 needs_transcode
= os
.path
.splitext(fname
)[1].lower() in TRANSCODE \
113 handler
.send_response(200)
114 handler
.send_header('Content-Type', 'audio/mpeg')
115 if not needs_transcode
:
116 fsize
= os
.path
.getsize(fname
)
117 handler
.send_header('Content-Length', fsize
)
118 handler
.send_header('Connection', 'close')
119 handler
.end_headers()
123 fname
= fname
.encode('iso8859-1')
124 cmd
= [ffmpeg_path(), '-i', fname
, '-ab',
125 '320k', '-ar', '44100', '-f', 'mp3', '-']
127 cmd
[-1:] = ['-ss', '%.3f' % (seek
/ 1000.0), '-']
129 cmd
[-1:] = ['-t', '%.3f' % (duration
/ 1000.0), '-']
131 ffmpeg
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
133 shutil
.copyfileobj(ffmpeg
.stdout
, handler
.wfile
)
137 f
= file(fname
, 'rb')
139 shutil
.copyfileobj(f
, handler
.wfile
)
143 def QueryContainer(self
, handler
, query
):
145 def AudioFileFilter(f
, filter_type
=None):
146 ext
= os
.path
.splitext(f
)[1].lower()
148 if ext
in ('.mp3', '.mp2') or ext
in TRANSCODE
:
153 if not filter_type
or filter_type
.split('/')[0] != self
.AUDIO
:
155 file_type
= self
.PLAYLIST
156 elif os
.path
.isdir(f
):
157 file_type
= self
.DIRECTORY
162 if f
.name
in self
.media_data_cache
:
163 return self
.media_data_cache
[f
.name
]
166 item
['path'] = f
.name
167 item
['part_path'] = f
.name
.replace(local_base_path
, '', 1)
168 item
['name'] = os
.path
.split(f
.name
)[1]
169 item
['is_dir'] = f
.isdir
170 item
['is_playlist'] = f
.isplay
171 item
['params'] = 'No'
174 item
['Title'] = f
.title
177 item
['Duration'] = f
.duration
179 if f
.isdir
or f
.isplay
or '://' in f
.name
:
180 self
.media_data_cache
[f
.name
] = item
183 if os
.path
.splitext(f
.name
)[1].lower() in TRANSCODE
:
184 # If the format is: (track #) Song name...
185 #artist, album, track = f.name.split(os.path.sep)[-3:]
186 #track = os.path.splitext(track)[0]
187 #if track[0].isdigit:
188 # track = ' '.join(track.split(' ')[1:])
190 #item['SongTitle'] = track
191 #item['AlbumTitle'] = album
192 #item['ArtistName'] = artist
193 fname
= unicode(f
.name
, 'utf-8')
195 fname
= fname
.encode('iso8859-1')
196 cmd
= [ffmpeg_path(), '-i', fname
]
197 ffmpeg
= subprocess
.Popen(cmd
, stderr
=subprocess
.PIPE
,
198 stdout
=subprocess
.PIPE
,
199 stdin
=subprocess
.PIPE
)
201 # wait 10 sec if ffmpeg is not back give up
202 for i
in xrange(200):
204 if not ffmpeg
.poll() == None:
207 if ffmpeg
.poll() != None:
208 output
= ffmpeg
.stderr
.read()
211 millisecs
= (int(d
.group(1)) * 3600 + \
212 int(d
.group(2)) * 60 + \
213 int(d
.group(3))) * 1000 + \
214 int(d
.group(4)) * 100
217 item
['Duration'] = millisecs
220 audioFile
= eyeD3
.Mp3AudioFile(unicode(f
.name
, 'utf-8'))
221 item
['Duration'] = audioFile
.getPlayTime() * 1000
223 tag
= audioFile
.getTag()
224 artist
= tag
.getArtist()
225 title
= tag
.getTitle()
226 if artist
== 'Various Artists' and '/' in title
:
227 artist
, title
= title
.split('/')
228 item
['ArtistName'] = artist
.strip()
229 item
['SongTitle'] = title
.strip()
230 item
['AlbumTitle'] = tag
.getAlbum()
231 item
['AlbumYear'] = tag
.getYear()
232 item
['MusicGenre'] = tag
.getGenre().getName()
233 except Exception, msg
:
236 if 'Duration' in item
:
237 item
['params'] = 'Yes'
239 self
.media_data_cache
[f
.name
] = item
242 subcname
= query
['Container'][0]
243 cname
= subcname
.split('/')[0]
244 local_base_path
= self
.get_local_base_path(handler
, query
)
246 if not handler
.server
.containers
.has_key(cname
) or \
247 not self
.get_local_path(handler
, query
):
248 handler
.send_response(404)
249 handler
.end_headers()
252 if os
.path
.splitext(subcname
)[1].lower() in PLAYLISTS
:
253 t
= Template(playlist_template
, filter=EncodeUnicode
)
254 t
.files
, t
.total
, t
.start
= self
.get_playlist(handler
, query
)
256 t
= Template(folder_template
, filter=EncodeUnicode
)
257 t
.files
, t
.total
, t
.start
= self
.get_files(handler
, query
,
259 t
.files
= map(media_data
, t
.files
)
266 handler
.send_response(200)
267 handler
.send_header('Content-Type', 'text/xml')
268 handler
.send_header('Content-Length', len(page
))
269 handler
.send_header('Connection', 'close')
270 handler
.end_headers()
271 handler
.wfile
.write(page
)
273 def parse_playlist(self
, list_name
, recurse
):
275 ext
= os
.path
.splitext(list_name
)[1].lower()
278 url
= list_name
.index('http://')
279 list_name
= list_name
[url
:]
280 list_file
= urllib
.urlopen(list_name
)
282 list_file
= open(unicode(list_name
, 'utf-8'))
283 local_path
= os
.path
.sep
.join(list_name
.split(os
.path
.sep
)[:-1])
285 if ext
in ('.m3u', '.pls'):
286 charset
= 'iso-8859-1'
290 if ext
in ('.wpl', '.asx', '.wax', '.wvx', '.b4s'):
292 for line
in list_file
:
293 line
= unicode(line
, charset
).encode('utf-8')
301 playlist
.append(FileData(s
.group(1), False))
304 names
, titles
, lengths
= {}, {}, {}
305 for line
in list_file
:
306 line
= unicode(line
, charset
).encode('utf-8')
309 names
[s
.group(1)] = s
.group(2)
313 titles
[s
.group(1)] = s
.group(2)
317 lengths
[s
.group(1)] = int(s
.group(2))
320 f
= FileData(names
[key
], False)
322 f
.title
= titles
[key
]
324 f
.duration
= lengths
[key
]
327 else: # ext == '.m3u' or '.m3u8' or '.ram'
328 duration
, title
= 0, ''
330 for line
in list_file
:
331 line
= unicode(line
.strip(), charset
).encode('utf-8')
333 if line
.startswith('#EXTINF:'):
335 duration
, title
= line
[8:].split(',')
336 duration
= int(duration
)
340 elif not line
.startswith('#'):
341 f
= FileData(line
, False)
342 f
.title
= title
.strip()
343 f
.duration
= duration
345 duration
, title
= 0, ''
349 # Expand relative paths
350 for i
in xrange(len(playlist
)):
351 if not '://' in playlist
[i
].name
:
352 name
= playlist
[i
].name
353 if not os
.path
.isabs(name
):
354 name
= os
.path
.join(local_path
, name
)
355 playlist
[i
].name
= os
.path
.normpath(name
)
361 newlist
.extend(self
.parse_playlist(i
.name
, recurse
))
369 def get_files(self
, handler
, query
, filterFunction
=None):
372 def __init__(self
, files
):
378 def build_recursive_list(path
, recurse
=True):
380 path
= unicode(path
, 'utf-8')
382 for f
in os
.listdir(path
):
383 if f
.startswith('.'):
385 f
= os
.path
.join(path
, f
)
386 isdir
= os
.path
.isdir(f
)
387 f
= f
.encode('utf-8')
388 if recurse
and isdir
:
389 files
.extend(build_recursive_list(f
))
391 fd
= FileData(f
, isdir
)
392 if recurse
and fd
.isplay
:
393 files
.extend(self
.parse_playlist(f
, recurse
))
394 elif isdir
or filterFunction(f
, file_type
):
401 if x
.isdir
== y
.isdir
:
402 if x
.isplay
== y
.isplay
:
403 return name_sort(x
, y
)
405 return y
.isplay
- x
.isplay
407 return y
.isdir
- x
.isdir
410 return cmp(x
.name
, y
.name
)
412 subcname
= query
['Container'][0]
413 cname
= subcname
.split('/')[0]
414 path
= self
.get_local_path(handler
, query
)
416 file_type
= query
.get('Filter', [''])[0]
418 recurse
= query
.get('Recurse', ['No'])[0] == 'Yes'
421 if recurse
and path
in self
.recurse_cache
:
422 if self
.recurse_cache
.mtime(path
) + 3600 >= time
.time():
423 filelist
= self
.recurse_cache
[path
]
424 elif not recurse
and path
in self
.dir_cache
:
425 if self
.dir_cache
.mtime(path
) >= os
.stat(path
)[8]:
426 filelist
= self
.dir_cache
[path
]
429 filelist
= SortList(build_recursive_list(path
, recurse
))
432 self
.recurse_cache
[path
] = filelist
434 self
.dir_cache
[path
] = filelist
439 sortby
= query
.get('SortOrder', ['Normal'])[0]
440 if 'Random' in sortby
:
441 if 'RandomSeed' in query
:
442 seed
= query
['RandomSeed'][0]
444 if 'RandomStart' in query
:
445 start
= query
['RandomStart'][0]
448 if filelist
.unsorted
or filelist
.sortby
!= sortby
:
449 if 'Random' in sortby
:
450 self
.random_lock
.acquire()
453 random
.shuffle(filelist
.files
)
454 self
.random_lock
.release()
456 local_base_path
= self
.get_local_base_path(handler
, query
)
457 start
= unquote(start
)
458 start
= start
.replace(os
.path
.sep
+ cname
,
460 filenames
= [x
.name
for x
in filelist
.files
]
462 index
= filenames
.index(start
)
463 i
= filelist
.files
.pop(index
)
464 filelist
.files
.insert(0, i
)
466 print 'Start not found:', start
468 filelist
.files
.sort(dir_sort
)
470 filelist
.sortby
= sortby
471 filelist
.unsorted
= False
473 files
= filelist
.files
[:]
476 files
, total
, start
= self
.item_count(handler
, query
, cname
, files
,
478 filelist
.last_start
= start
479 return files
, total
, start
481 def get_playlist(self
, handler
, query
):
482 subcname
= query
['Container'][0]
483 cname
= subcname
.split('/')[0]
486 url
= subcname
.index('http://')
487 list_name
= subcname
[url
:]
489 list_name
= self
.get_local_path(handler
, query
)
491 recurse
= query
.get('Recurse', ['No'])[0] == 'Yes'
492 playlist
= self
.parse_playlist(list_name
, recurse
)
495 if 'Random' in query
.get('SortOrder', ['Normal'])[0]:
496 seed
= query
.get('RandomSeed', [''])[0]
497 start
= query
.get('RandomStart', [''])[0]
499 self
.random_lock
.acquire()
502 random
.shuffle(playlist
)
503 self
.random_lock
.release()
505 local_base_path
= self
.get_local_base_path(handler
, query
)
506 start
= unquote(start
)
507 start
= start
.replace(os
.path
.sep
+ cname
,
509 filenames
= [x
.name
for x
in playlist
]
511 index
= filenames
.index(start
)
512 i
= playlist
.pop(index
)
513 playlist
.insert(0, i
)
515 print 'Start not found:', start
518 return self
.item_count(handler
, query
, cname
, playlist
)