Changeset d90adf704cdb4575bd5ecaf592b2f92fe78d9373
- Timestamp:
- 12/23/06 21:36:53 (2 years ago)
- git-parent:
- Files:
-
- plugin.py (modified) (2 diffs)
- plugins/music/music.py (modified) (3 diffs)
- plugins/video/video.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugin.py
r29ad0eb rd90adf7 1 import os, shutil, re 2 from urllib import unquote, unquote_plus 3 from urlparse import urlparse 4 1 5 def GetPlugin(name): 2 6 module_name = '.'.join(['plugins', name, name]) … … 9 13 content_type = '' 10 14 11 def SendFile(): 12 pass 15 def SendFile(self, handler, container, name): 16 o = urlparse("http://fake.host" + handler.path) 17 path = unquote_plus(o.path) 18 handler.send_response(200) 19 handler.end_headers() 20 f = file(container['path'] + path[len(name)+1:], 'rb') 21 shutil.copyfileobj(f, handler.wfile) 22 23 def get_local_path(self, handler, query): 24 25 subcname = query['Container'][0] 26 container = handler.server.containers[subcname.split('/')[0]] 27 28 path = container['path'] 29 for folder in subcname.split('/')[1:]: 30 if folder == '..': 31 return False 32 path = os.path.join(path, folder) 33 return path 34 35 def get_files(self, handler, query, filterFunction=None): 36 subcname = query['Container'][0] 37 path = self.get_local_path(handler, query) 38 39 files = os.listdir(path) 40 if filterFunction: 41 files = filter(filterFunction, files) 42 totalFiles = len(files) 43 44 def dir_sort(x, y): 45 xdir = os.path.isdir(os.path.join(path, x)) 46 ydir = os.path.isdir(os.path.join(path, y)) 47 48 if xdir and ydir: 49 return name_sort(x, y) 50 elif xdir: 51 return -1 52 elif ydir: 53 return 1 54 else: 55 return name_sort(x, y) 56 57 def name_sort(x, y): 58 numbername = re.compile(r'(\d*)(.*)') 59 m = numbername.match(x) 60 xNumber = m.group(1) 61 xStr = m.group(2) 62 m = numbername.match(y) 63 yNumber = m.group(1) 64 yStr = m.group(2) 65 66 if xNumber and yNumber: 67 xNumber, yNumber = int(xNumber), int(yNumber) 68 if xNumber == yNumber: 69 return cmp(xStr, yStr) 70 else: 71 return cmp(xNumber, yNumber) 72 elif xNumber: 73 return -1 74 elif yNumber: 75 return 1 76 else: 77 return cmp(xStr, yStr) 78 79 files.sort(dir_sort) 80 81 index = 0 82 count = 10 83 if query.has_key('ItemCount'): 84 count = int(query['ItemCount'] [0]) 85 86 if query.has_key('AnchorItem'): 87 anchor = unquote(query['AnchorItem'][0]) 88 for i in range(len(files)): 89 if os.path.isdir(os.path.join(path,files[i])): 90 file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] 91 else: 92 file_url = '/' + subcname + '/' + files[i] 93 if file_url == anchor: 94 if count > 0: 95 index = i + 1 96 elif count < 0: 97 index = i - 1 98 else: 99 index = i 100 break 101 if query.has_key('AnchorOffset'): 102 index = index + int(query['AnchorOffset'][0]) 103 104 if index < index + count: 105 files = files[max(index, 0):index + count ] 106 return files, totalFiles, index 107 else: 108 files = files[max(index + count, 0):index] 109 return files, totalFiles, index + count plugins/music/music.py
rc3a7ed6 rd90adf7 1 import transcode, os, socket, re , shutil1 import transcode, os, socket, re 2 2 from Cheetah.Template import Template 3 3 from plugin import Plugin 4 4 from urllib import unquote_plus, quote, unquote 5 from urlparse import urlparse6 5 from xml.sax.saxutils import escape 7 6 import eyeD3 … … 14 13 15 14 self.content_type = 'x-container/tivo-music' 16 17 def SendFile(self, handler, container, name):18 19 o = urlparse("http://fake.host" + handler.path)20 path = unquote_plus(o.path)21 handler.send_response(200)22 handler.end_headers()23 f = file(container['path'] + path[len(name)+1:], 'rb')24 shutil.copyfileobj(f, handler.wfile)25 15 26 16 def QueryContainer(self, handler, query): … … 79 69 handler.wfile.write(t) 80 70 81 def get_local_path(self, handler, query):82 71 83 subcname = query['Container'][0]84 container = handler.server.containers[subcname.split('/')[0]]85 86 path = container['path']87 for folder in subcname.split('/')[1:]:88 if folder == '..':89 return False90 path = os.path.join(path, folder)91 return path92 93 def get_files(self, handler, query):94 subcname = query['Container'][0]95 path = self.get_local_path(handler, query)96 97 files = os.listdir(path)98 totalFiles = len(files)99 100 def dir_sort(x, y):101 xdir = os.path.isdir(os.path.join(path, x))102 ydir = os.path.isdir(os.path.join(path, y))103 104 if xdir and ydir:105 return name_sort(x, y)106 elif xdir:107 return -1108 elif ydir:109 return 1110 else:111 return name_sort(x, y)112 113 def name_sort(x, y):114 numbername = re.compile(r'(\d*)(.*)')115 m = numbername.match(x)116 xNumber = m.group(1)117 xStr = m.group(2)118 m = numbername.match(y)119 yNumber = m.group(1)120 yStr = m.group(2)121 122 if xNumber and yNumber:123 xNumber, yNumber = int(xNumber), int(yNumber)124 if xNumber == yNumber:125 return cmp(xStr, yStr)126 else:127 return cmp(xNumber, yNumber)128 elif xNumber:129 return -1130 elif yNumber:131 return 1132 else:133 return cmp(xStr, yStr)134 135 files.sort(dir_sort)136 137 index = 0138 count = 10139 if query.has_key('ItemCount'):140 count = int(query['ItemCount'] [0])141 142 if query.has_key('AnchorItem'):143 anchor = unquote(query['AnchorItem'][0])144 for i in range(len(files)):145 if os.path.isdir(os.path.join(path,files[i])):146 file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i]147 else:148 file_url = '/' + subcname + '/' + files[i]149 if file_url == anchor:150 if count > 0:151 index = i + 1152 elif count < 0:153 index = i - 1154 else:155 index = i156 break157 if query.has_key('AnchorOffset'):158 index = index + int(query['AnchorOffset'][0])159 72 160 if index < index + count:161 files = files[max(index, 0):index + count ]162 return files, totalFiles, index163 else:164 files = files[max(index + count, 0):index]165 return files, totalFiles, index + count166 plugins/video/video.py
r29ad0eb rd90adf7 44 44 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) 45 45 t.name = subcname 46 t.files, t.total, t.start = self.get_files(handler, query )46 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)) ) 47 47 t.isdir = isdir 48 48 t.quote = quote 49 49 t.escape = escape 50 50 handler.wfile.write(t) 51 52 def get_local_path(self, handler, query):53 54 subcname = query['Container'][0]55 container = handler.server.containers[subcname.split('/')[0]]56 57 path = container['path']58 for folder in subcname.split('/')[1:]:59 if folder == '..':60 return False61 path = os.path.join(path, folder)62 return path63 64 def get_files(self, handler, query):65 subcname = query['Container'][0]66 path = self.get_local_path(handler, query)67 68 files = os.listdir(path)69 files = filter(lambda f: os.path.isdir(os.path.join(path, f)) or transcode.suported_format(os.path.join(path,f)), files)70 totalFiles = len(files)71 72 def dir_sort(x, y):73 xdir = os.path.isdir(os.path.join(path, x))74 ydir = os.path.isdir(os.path.join(path, y))75 76 if xdir and ydir:77 return name_sort(x, y)78 elif xdir:79 return -180 elif ydir:81 return 182 else:83 return name_sort(x, y)84 85 def name_sort(x, y):86 numbername = re.compile(r'(\d*)(.*)')87 m = numbername.match(x)88 xNumber = m.group(1)89 xStr = m.group(2)90 m = numbername.match(y)91 yNumber = m.group(1)92 yStr = m.group(2)93 94 if xNumber and yNumber:95 xNumber, yNumber = int(xNumber), int(yNumber)96 if xNumber == yNumber:97 return cmp(xStr, yStr)98 else:99 return cmp(xNumber, yNumber)100 elif xNumber:101 return -1102 elif yNumber:103 return 1104 else:105 return cmp(xStr, yStr)106 107 files.sort(dir_sort)108 109 index = 0110 if query.has_key('ItemCount'):111 count = int(query['ItemCount'] [0])112 113 if query.has_key('AnchorItem'):114 anchor = unquote(query['AnchorItem'][0])115 for i in range(len(files)):116 117 if os.path.isdir(os.path.join(path,files[i])):118 file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i]119 else:120 file_url = '/' + subcname + '/' + files[i]121 if file_url == anchor:122 index = i + 1123 break124 if query.has_key('AnchorOffset'):125 index = index + int(query['AnchorOffset'][0])126 files = files[index:index + count]127 128 return files, totalFiles, index
