From d90adf704cdb4575bd5ecaf592b2f92fe78d9373 Mon Sep 17 00:00:00 2001 From: Jason Michalski Date: Sun, 24 Dec 2006 03:36:53 +0000 Subject: [PATCH] pyTivo - Added common crap to the base plugin --- plugins/music/music.py => plugin.py | 83 ++----------- plugins/music/music.py | 238 +++++++++++------------------------- plugins/video/video.py | 178 ++++++++------------------- 3 files changed, 135 insertions(+), 364 deletions(-) copy plugins/music/music.py => plugin.py (56%) rewrite plugins/music/music.py (60%) rewrite plugins/video/video.py (62%) diff --git a/plugins/music/music.py b/plugin.py similarity index 56% copy from plugins/music/music.py copy to plugin.py index 33bca7f..b728b31 100644 --- a/plugins/music/music.py +++ b/plugin.py @@ -1,82 +1,24 @@ -import transcode, os, socket, re, shutil -from Cheetah.Template import Template -from plugin import Plugin -from urllib import unquote_plus, quote, unquote +import os, shutil, re +from urllib import unquote, unquote_plus from urlparse import urlparse -from xml.sax.saxutils import escape -import eyeD3 -SCRIPTDIR = os.path.dirname(__file__) +def GetPlugin(name): + module_name = '.'.join(['plugins', name, name]) + module = __import__(module_name, fromlist=name) + plugin = getattr(module, name)() + return plugin -class music(Plugin): - - def __init__(self): - - self.content_type = 'x-container/tivo-music' +class Plugin: + + content_type = '' def SendFile(self, handler, container, name): - o = urlparse("http://fake.host" + handler.path) path = unquote_plus(o.path) handler.send_response(200) handler.end_headers() f = file(container['path'] + path[len(name)+1:], 'rb') shutil.copyfileobj(f, handler.wfile) - - def QueryContainer(self, handler, query): - - subcname = query['Container'][0] - cname = subcname.split('/')[0] - - if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query): - handler.send_response(404) - handler.end_headers() - return - - path = self.get_local_path(handler, query) - def isdir(file): - return os.path.isdir(os.path.join(path, file)) - - def media_data(file): - dict = {} - dict['path'] = file - - file = os.path.join(path, file) - - if isdir(file) or not eyeD3.isMp3File(file): - return dict - - audioFile = eyeD3.Mp3AudioFile(file) - dict['Duration'] = audioFile.getPlayTime() * 1000 - dict['SourceBitRate'] = audioFile.getBitRate()[1] - dict['SourceSampleRate'] = audioFile.getSampleFreq() - - tag = audioFile.getTag() - dict['ArtistName'] = str(tag.getArtist()) - dict['AlbumTitle'] = str(tag.getAlbum()) - dict['SongTitle'] = str(tag.getTitle()) - dict['AlbumYear'] = tag.getYear() - - try: - dict['MusicGenre'] = tag.getGenre().getName() - except: - pass - - return dict - - handler.send_response(200) - handler.end_headers() - t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) - t.name = subcname - print '----' - print len(self.get_files(handler, query)[0]) - print len(map(media_data, self.get_files(handler, query)[0])) - t.files, t.total, t.start = self.get_files(handler, query) - t.files = map(media_data, t.files) - t.isdir = isdir - t.quote = quote - t.escape = escape - handler.wfile.write(t) def get_local_path(self, handler, query): @@ -90,11 +32,13 @@ class music(Plugin): path = os.path.join(path, folder) return path - def get_files(self, handler, query): + def get_files(self, handler, query, filterFunction=None): subcname = query['Container'][0] path = self.get_local_path(handler, query) files = os.listdir(path) + if filterFunction: + files = filter(filterFunction, files) totalFiles = len(files) def dir_sort(x, y): @@ -163,4 +107,3 @@ class music(Plugin): else: files = files[max(index + count, 0):index] return files, totalFiles, index + count - diff --git a/plugins/music/music.py b/plugins/music/music.py dissimilarity index 60% index 33bca7f..8d6db6e 100644 --- a/plugins/music/music.py +++ b/plugins/music/music.py @@ -1,166 +1,72 @@ -import transcode, os, socket, re, shutil -from Cheetah.Template import Template -from plugin import Plugin -from urllib import unquote_plus, quote, unquote -from urlparse import urlparse -from xml.sax.saxutils import escape -import eyeD3 - -SCRIPTDIR = os.path.dirname(__file__) - -class music(Plugin): - - def __init__(self): - - self.content_type = 'x-container/tivo-music' - - def SendFile(self, handler, container, name): - - o = urlparse("http://fake.host" + handler.path) - path = unquote_plus(o.path) - handler.send_response(200) - handler.end_headers() - f = file(container['path'] + path[len(name)+1:], 'rb') - shutil.copyfileobj(f, handler.wfile) - - def QueryContainer(self, handler, query): - - subcname = query['Container'][0] - cname = subcname.split('/')[0] - - if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query): - handler.send_response(404) - handler.end_headers() - return - - path = self.get_local_path(handler, query) - def isdir(file): - return os.path.isdir(os.path.join(path, file)) - - def media_data(file): - dict = {} - dict['path'] = file - - file = os.path.join(path, file) - - if isdir(file) or not eyeD3.isMp3File(file): - return dict - - audioFile = eyeD3.Mp3AudioFile(file) - dict['Duration'] = audioFile.getPlayTime() * 1000 - dict['SourceBitRate'] = audioFile.getBitRate()[1] - dict['SourceSampleRate'] = audioFile.getSampleFreq() - - tag = audioFile.getTag() - dict['ArtistName'] = str(tag.getArtist()) - dict['AlbumTitle'] = str(tag.getAlbum()) - dict['SongTitle'] = str(tag.getTitle()) - dict['AlbumYear'] = tag.getYear() - - try: - dict['MusicGenre'] = tag.getGenre().getName() - except: - pass - - return dict - - handler.send_response(200) - handler.end_headers() - t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) - t.name = subcname - print '----' - print len(self.get_files(handler, query)[0]) - print len(map(media_data, self.get_files(handler, query)[0])) - t.files, t.total, t.start = self.get_files(handler, query) - t.files = map(media_data, t.files) - t.isdir = isdir - t.quote = quote - t.escape = escape - handler.wfile.write(t) - - def get_local_path(self, handler, query): - - subcname = query['Container'][0] - container = handler.server.containers[subcname.split('/')[0]] - - path = container['path'] - for folder in subcname.split('/')[1:]: - if folder == '..': - return False - path = os.path.join(path, folder) - return path - - def get_files(self, handler, query): - subcname = query['Container'][0] - path = self.get_local_path(handler, query) - - files = os.listdir(path) - totalFiles = len(files) - - def dir_sort(x, y): - xdir = os.path.isdir(os.path.join(path, x)) - ydir = os.path.isdir(os.path.join(path, y)) - - if xdir and ydir: - return name_sort(x, y) - elif xdir: - return -1 - elif ydir: - return 1 - else: - return name_sort(x, y) - - def name_sort(x, y): - numbername = re.compile(r'(\d*)(.*)') - m = numbername.match(x) - xNumber = m.group(1) - xStr = m.group(2) - m = numbername.match(y) - yNumber = m.group(1) - yStr = m.group(2) - - if xNumber and yNumber: - xNumber, yNumber = int(xNumber), int(yNumber) - if xNumber == yNumber: - return cmp(xStr, yStr) - else: - return cmp(xNumber, yNumber) - elif xNumber: - return -1 - elif yNumber: - return 1 - else: - return cmp(xStr, yStr) - - files.sort(dir_sort) - - index = 0 - count = 10 - if query.has_key('ItemCount'): - count = int(query['ItemCount'] [0]) - - if query.has_key('AnchorItem'): - anchor = unquote(query['AnchorItem'][0]) - for i in range(len(files)): - if os.path.isdir(os.path.join(path,files[i])): - file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] - else: - file_url = '/' + subcname + '/' + files[i] - if file_url == anchor: - if count > 0: - index = i + 1 - elif count < 0: - index = i - 1 - else: - index = i - break - if query.has_key('AnchorOffset'): - index = index + int(query['AnchorOffset'][0]) - - if index < index + count: - files = files[max(index, 0):index + count ] - return files, totalFiles, index - else: - files = files[max(index + count, 0):index] - return files, totalFiles, index + count - +import transcode, os, socket, re +from Cheetah.Template import Template +from plugin import Plugin +from urllib import unquote_plus, quote, unquote +from xml.sax.saxutils import escape +import eyeD3 + +SCRIPTDIR = os.path.dirname(__file__) + +class music(Plugin): + + def __init__(self): + + self.content_type = 'x-container/tivo-music' + + def QueryContainer(self, handler, query): + + subcname = query['Container'][0] + cname = subcname.split('/')[0] + + if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query): + handler.send_response(404) + handler.end_headers() + return + + path = self.get_local_path(handler, query) + def isdir(file): + return os.path.isdir(os.path.join(path, file)) + + def media_data(file): + dict = {} + dict['path'] = file + + file = os.path.join(path, file) + + if isdir(file) or not eyeD3.isMp3File(file): + return dict + + audioFile = eyeD3.Mp3AudioFile(file) + dict['Duration'] = audioFile.getPlayTime() * 1000 + dict['SourceBitRate'] = audioFile.getBitRate()[1] + dict['SourceSampleRate'] = audioFile.getSampleFreq() + + tag = audioFile.getTag() + dict['ArtistName'] = str(tag.getArtist()) + dict['AlbumTitle'] = str(tag.getAlbum()) + dict['SongTitle'] = str(tag.getTitle()) + dict['AlbumYear'] = tag.getYear() + + try: + dict['MusicGenre'] = tag.getGenre().getName() + except: + pass + + return dict + + handler.send_response(200) + handler.end_headers() + t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) + t.name = subcname + print '----' + print len(self.get_files(handler, query)[0]) + print len(map(media_data, self.get_files(handler, query)[0])) + t.files, t.total, t.start = self.get_files(handler, query) + t.files = map(media_data, t.files) + t.isdir = isdir + t.quote = quote + t.escape = escape + handler.wfile.write(t) + + + diff --git a/plugins/video/video.py b/plugins/video/video.py dissimilarity index 62% index 8a2ce83..16b50ee 100644 --- a/plugins/video/video.py +++ b/plugins/video/video.py @@ -1,128 +1,50 @@ -import transcode, os, socket, re -from Cheetah.Template import Template -from plugin import Plugin -from urllib import unquote_plus, quote, unquote -from urlparse import urlparse -from xml.sax.saxutils import escape - -SCRIPTDIR = os.path.dirname(__file__) - -class video(Plugin): - - content_type = 'x-container/tivo-videos' - - def SendFile(self, handler, container, name): - - #cheep hack - if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-': - handler.send_response(404) - handler.end_headers() - return - - o = urlparse("http://fake.host" + handler.path) - path = unquote_plus(o.path) - handler.send_response(200) - handler.end_headers() - transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile) - - def QueryContainer(self, handler, query): - - subcname = query['Container'][0] - cname = subcname.split('/')[0] - - if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query): - handler.send_response(404) - handler.end_headers() - return - - path = self.get_local_path(handler, query) - def isdir(file): - return os.path.isdir(os.path.join(path, file)) - - handler.send_response(200) - handler.end_headers() - t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) - t.name = subcname - t.files, t.total, t.start = self.get_files(handler, query) - t.isdir = isdir - t.quote = quote - t.escape = escape - handler.wfile.write(t) - - def get_local_path(self, handler, query): - - subcname = query['Container'][0] - container = handler.server.containers[subcname.split('/')[0]] - - path = container['path'] - for folder in subcname.split('/')[1:]: - if folder == '..': - return False - path = os.path.join(path, folder) - return path - - def get_files(self, handler, query): - subcname = query['Container'][0] - path = self.get_local_path(handler, query) - - files = os.listdir(path) - files = filter(lambda f: os.path.isdir(os.path.join(path, f)) or transcode.suported_format(os.path.join(path,f)), files) - totalFiles = len(files) - - def dir_sort(x, y): - xdir = os.path.isdir(os.path.join(path, x)) - ydir = os.path.isdir(os.path.join(path, y)) - - if xdir and ydir: - return name_sort(x, y) - elif xdir: - return -1 - elif ydir: - return 1 - else: - return name_sort(x, y) - - def name_sort(x, y): - numbername = re.compile(r'(\d*)(.*)') - m = numbername.match(x) - xNumber = m.group(1) - xStr = m.group(2) - m = numbername.match(y) - yNumber = m.group(1) - yStr = m.group(2) - - if xNumber and yNumber: - xNumber, yNumber = int(xNumber), int(yNumber) - if xNumber == yNumber: - return cmp(xStr, yStr) - else: - return cmp(xNumber, yNumber) - elif xNumber: - return -1 - elif yNumber: - return 1 - else: - return cmp(xStr, yStr) - - files.sort(dir_sort) - - index = 0 - if query.has_key('ItemCount'): - count = int(query['ItemCount'] [0]) - - if query.has_key('AnchorItem'): - anchor = unquote(query['AnchorItem'][0]) - for i in range(len(files)): - - if os.path.isdir(os.path.join(path,files[i])): - file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] - else: - file_url = '/' + subcname + '/' + files[i] - if file_url == anchor: - index = i + 1 - break - if query.has_key('AnchorOffset'): - index = index + int(query['AnchorOffset'][0]) - files = files[index:index + count] - - return files, totalFiles, index +import transcode, os, socket, re +from Cheetah.Template import Template +from plugin import Plugin +from urllib import unquote_plus, quote, unquote +from urlparse import urlparse +from xml.sax.saxutils import escape + +SCRIPTDIR = os.path.dirname(__file__) + +class video(Plugin): + + content_type = 'x-container/tivo-videos' + + def SendFile(self, handler, container, name): + + #cheep hack + if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-': + handler.send_response(404) + handler.end_headers() + return + + o = urlparse("http://fake.host" + handler.path) + path = unquote_plus(o.path) + handler.send_response(200) + handler.end_headers() + transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile) + + def QueryContainer(self, handler, query): + + subcname = query['Container'][0] + cname = subcname.split('/')[0] + + if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query): + handler.send_response(404) + handler.end_headers() + return + + path = self.get_local_path(handler, query) + def isdir(file): + return os.path.isdir(os.path.join(path, file)) + + handler.send_response(200) + handler.end_headers() + t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) + t.name = subcname + t.files, t.total, t.start = self.get_files(handler, query, lambda f: os.path.isdir(os.path.join(path, f)) or transcode.suported_format(os.path.join(path,f)) ) + t.isdir = isdir + t.quote = quote + t.escape = escape + handler.wfile.write(t) -- 2.11.4.GIT