def video_file_filter(self, full_path, type=None): if os.path.isdir(full_path): return True if extensions: return os.path.splitext(full_path)[1].lower() in extensions else: return transcode.supported_format(full_path)
def video_file_filter(self, full_path, type=None): if os.path.isdir(unicode(full_path, 'utf-8')): return True if use_extensions: return os.path.splitext(full_path)[1].lower() in EXTENSIONS else: return transcode.supported_format(full_path)
def push_one_file(self, f): file_info = VideoDetails() file_info['valid'] = transcode.supported_format(f['path']) temp_share = config.get_server('temp_share', '') temp_share_path = '' if temp_share: for name, data in config.getShares(): if temp_share == name: temp_share_path = data.get('path') mime = 'video/mpeg' if config.isHDtivo(f['tsn']): for m in ['video/mp4', 'video/bif']: if transcode.tivo_compatible(f['path'], f['tsn'], m)[0]: mime = m break if (mime == 'video/mpeg' and transcode.mp4_remuxable(f['path'], f['tsn'])): new_path = transcode.mp4_remux(f['path'], f['name'], f['tsn'], temp_share_path) if new_path: mime = 'video/mp4' f['name'] = new_path if temp_share_path: ip = config.get_ip() port = config.getPort() container = quote(temp_share) + '/' f['url'] = 'http://%s:%s/%s' % (ip, port, container) if file_info['valid']: file_info.update(self.metadata_full(f['path'], f['tsn'], mime)) url = f['url'] + quote(f['name']) title = file_info['seriesTitle'] if not title: title = file_info['title'] source = file_info['seriesId'] if not source: source = title subtitle = file_info['episodeTitle'] try: m = mind.getMind(f['tsn']) m.pushVideo( tsn = f['tsn'], url = url, description = file_info['description'], duration = file_info['duration'] / 1000, size = file_info['size'], title = title, subtitle = subtitle, source = source, mime = mime, tvrating = file_info['tvRating']) except Exception, msg: logger.error(msg)
def get_details_xml(self, tsn, file_path): if (tsn, file_path) in self.tvbus_cache: details = self.tvbus_cache[(tsn, file_path)] else: file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) if file_info['valid']: file_info.update(self.metadata_full(file_path, tsn)) t = Template(TVBUS_TEMPLATE, filter=EncodeUnicode) t.video = file_info t.escape = escape details = str(t) self.tvbus_cache[(tsn, file_path)] = details return details
def push_one_file(self, f): file_info = VideoDetails() file_info["valid"] = transcode.supported_format(f["path"]) mime = "video/mpeg" if config.isHDtivo(f["tsn"]): for m in ["video/mp4", "video/bif"]: if transcode.tivo_compatible(f["path"], f["tsn"], m)[0]: mime = m break if mime == "video/mpeg" and transcode.mp4_remuxable(f["path"], f["tsn"]): new_path = transcode.mp4_remux(f["path"], f["name"], f["tsn"]) if new_path: mime = "video/mp4" f["name"] = new_path if file_info["valid"]: file_info.update(self.metadata_full(f["path"], f["tsn"], mime)) url = f["url"] + quote(f["name"]) title = file_info["seriesTitle"] if not title: title = file_info["title"] source = file_info["seriesId"] if not source: source = title subtitle = file_info["episodeTitle"] try: m = mind.getMind(f["tsn"]) m.pushVideo( tsn=f["tsn"], url=url, description=file_info["description"], duration=file_info["duration"] / 1000, size=file_info["size"], title=title, subtitle=subtitle, source=source, mime=mime, tvrating=file_info["tvRating"], ) except Exception, msg: logger.error(msg)
def push_one_file(self, f): file_info = VideoDetails() file_info['valid'] = transcode.supported_format(f['path']) mime = 'video/mpeg' if config.isHDtivo(f['tsn']): for m in ['video/mp4', 'video/bif']: if transcode.tivo_compatible(f['path'], f['tsn'], m)[0]: mime = m break if (mime == 'video/mpeg' and transcode.mp4_remuxable(f['path'], f['tsn'])): new_path = transcode.mp4_remux(f['path'], f['name'], f['tsn']) if new_path: mime = 'video/mp4' f['name'] = new_path if file_info['valid']: file_info.update(self.metadata_full(f['path'], f['tsn'], mime)) url = f['url'] + quote(f['name']) title = file_info['seriesTitle'] if not title: title = file_info['title'] source = file_info['seriesId'] if not source: source = title subtitle = file_info['episodeTitle'] try: m = mind.getMind(f['tsn']) m.pushVideo( tsn = f['tsn'], url = url, description = file_info['description'], duration = file_info['duration'] / 1000, size = file_info['size'], title = title, subtitle = subtitle, source = source, mime = mime, tvrating = file_info['tvRating']) except Exception, msg: logger.error(msg)
def TVBusQuery(self, handler, query): tsn = handler.headers.getheader('tsn', '') file = query['File'][0] path = self.get_local_path(handler, query) file_path = path + file file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) if file_info['valid']: file_info.update(self.__metadata_full(file_path, tsn)) handler.send_response(200) handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl')) t.video = file_info t.escape = escape handler.wfile.write(t)
def Push(self, handler, query): file = unquote(query['File'][0]) tsn = query['tsn'][0] path = self.get_local_path(handler, query) file_path = path + file file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) if file_info['valid']: file_info.update(self.__metadata_full(file_path, tsn)) import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('tivo.com',123)) ip = s.getsockname()[0] container = quote(query['Container'][0].split('/')[0]) port = config.getPort() url = 'http://%s:%s/%s%s' % (ip, port, container, quote(file)) print 'tsn', tsn print 'url', url print query username = config.getTivoUsername() password = config.getTivoPassword() if not username or not password: raise Exception("tivo_username and tivo_password required") try: m = mind.Mind(username, password, True) m.pushVideo( tsn = tsn, url = url, description = file_info['description'], duration = file_info['duration'] / 1000, size = file_info['size'], title = file_info['title'], subtitle = file_info['name']) except Exception, e: import traceback handler.send_response(500) handler.end_headers() handler.wfile.write('%s\n\n%s' % (e, traceback.format_exc() )) raise
def TVBusQuery(self, handler, query): tsn = handler.headers.getheader('tsn', '') file = query['File'][0] path = self.get_local_path(handler, query) file_path = path + file file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) if file_info['valid']: file_info.update(self.__metadata_full(file_path, tsn)) handler.send_response(200) handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'TvBus.tmpl')) t.video = file_info t.escape = escape handler.wfile.write(t)
def Push(self, handler, query): file = unquote(query['File'][0]) tsn = query['tsn'][0] path = self.get_local_path(handler, query) file_path = path + file file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) if file_info['valid']: file_info.update(self.__metadata_full(file_path, tsn)) import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('tivo.com', 123)) ip = s.getsockname()[0] container = quote(query['Container'][0].split('/')[0]) port = config.getPort() url = 'http://%s:%s/%s%s' % (ip, port, container, quote(file)) print 'tsn', tsn print 'url', url print query username = config.getTivoUsername() password = config.getTivoPassword() if not username or not password: raise Exception("tivo_username and tivo_password required") try: m = mind.Mind(username, password, True) m.pushVideo(tsn=tsn, url=url, description=file_info['description'], duration=file_info['duration'] / 1000, size=file_info['size'], title=file_info['title'], subtitle=file_info['name']) except Exception, e: import traceback handler.send_response(500) handler.end_headers() handler.wfile.write('%s\n\n%s' % (e, traceback.format_exc())) raise
def __total_items(self, full_path): count = 0 try: for file in os.listdir(full_path): if file.startswith('.'): continue file = os.path.join(full_path, file) if os.path.isdir(file): count += 1 elif extensions: if os.path.splitext(file)[1].lower() in extensions: count += 1 elif file in transcode.info_cache: if transcode.supported_format(file): count += 1 except: pass return count
def get_details_xml(self, tsn, file_path): if (tsn, file_path) in self.tvbus_cache: details = self.tvbus_cache[(tsn, file_path)] else: file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) if file_info['valid']: file_info.update(self.metadata_full(file_path, tsn)) t = Template(TVBUS_TEMPLATE, filter=EncodeUnicode) t.video = file_info t.escape = escape t.get_tv = metadata.get_tv t.get_mpaa = metadata.get_mpaa t.get_stars = metadata.get_stars details = str(t) self.tvbus_cache[(tsn, file_path)] = details return details
def __total_items(self, full_path): count = 0 try: full_path = unicode(full_path, 'utf-8') for f in os.listdir(full_path): if f.startswith('.'): continue f = os.path.join(full_path, f) f2 = f.encode('utf-8') if os.path.isdir(f): count += 1 elif use_extensions: if os.path.splitext(f2)[1].lower() in EXTENSIONS: count += 1 elif f2 in transcode.info_cache: if transcode.supported_format(f2): count += 1 except: pass return count
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] if not self.get_local_path(handler, query): handler.send_error(404) return container = handler.container precache = container.get('precache', 'False').lower() == 'true' force_alpha = container.get('force_alpha', 'False').lower() == 'true' use_html = query.get('Format', [''])[0].lower() == 'text/html' files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha) videos = [] local_base_path = self.get_local_base_path(handler, query) for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning('Bad file time on ' + unicode(f.name, 'utf-8')) mtime = int(time.time()) ltime = time.localtime(mtime) video['captureDate'] = hex(mtime) video['textDate'] = time.strftime('%b %d, %Y', ltime) video['name'] = os.path.basename(f.name) video['path'] = f.name video['part_path'] = f.name.replace(local_base_path, '', 1) if not video['part_path'].startswith(os.path.sep): video['part_path'] = os.path.sep + video['part_path'] video['title'] = os.path.basename(f.name) video['is_dir'] = f.isdir if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(f.name) else: if precache or len( files) == 1 or f.name in transcode.info_cache: video['valid'] = transcode.supported_format(f.name) if video['valid']: video.update(self.metadata_full(f.name, tsn)) if len(files) == 1: video['captureDate'] = hex(isogm(video['time'])) else: video['valid'] = True video.update(metadata.basic(f.name)) if config.hasTStivo(tsn): video['mime'] = 'video/x-tivo-mpeg-ts' else: video['mime'] = 'video/x-tivo-mpeg' video['textSize'] = ('%.3f GB' % (float(f.size) / (1024**3))) videos.append(video) if use_html: t = Template(HTML_CONTAINER_TEMPLATE, filter=EncodeUnicode) else: t = Template(XML_CONTAINER_TEMPLATE, filter=EncodeUnicode) t.container = handler.cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos t.tivo_names = config.tivo_names if use_html: handler.send_html(str(t)) else: handler.send_xml(str(t))
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] if not self.get_local_path(handler, query): handler.send_error(404) return container = handler.container force_alpha = container.getboolean('force_alpha') ar = container.get('allow_recurse', 'auto').lower() if ar == 'auto': allow_recurse = not tsn or tsn[0] < '7' else: allow_recurse = ar in ('1', 'yes', 'true', 'on') use_html = query.get('Format', [''])[0].lower() == 'text/html' files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha, allow_recurse) videos = [] local_base_path = self.get_local_base_path(handler, query) resort = False for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning('Bad file time on ' + unicode(f.name, 'utf-8')) mtime = time.time() ltime = time.localtime(mtime) video['captureDate'] = hex(int(mtime)) video['textDate'] = time.strftime('%b %d, %Y', ltime) video['name'] = os.path.basename(f.name) video['path'] = f.name video['part_path'] = f.name.replace(local_base_path, '', 1) if not video['part_path'].startswith(os.path.sep): video['part_path'] = os.path.sep + video['part_path'] video['title'] = os.path.basename(f.name) video['is_dir'] = f.isdir if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(f.name) else: if len(files) == 1 or f.name in transcode.info_cache: video['valid'] = transcode.supported_format(f.name) if video['valid']: video.update(self.metadata_full(f.name, tsn, mtime=mtime)) if len(files) == 1: video['captureDate'] = hex(isogm(video['time'])) else: video['valid'] = True video.update(metadata.basic(f.name, mtime)) if 'time' in video and video['time'] != '': if video['time'].lower() == 'oad': video['time'] = video['originalAirDate'] resort = True try: video['captureDate'] = hex(isogm(video['time'])) video['textDate'] = time.strftime('%b %d, %Y', time.localtime(isogm(video['time']))) resort = True except: logger.warning('Bad time format: "' + video['time'] + '", using current time') if self.use_ts(tsn, f.name): video['mime'] = 'video/x-tivo-mpeg-ts' else: video['mime'] = 'video/x-tivo-mpeg' video['textSize'] = metadata.human_size(f.size) videos.append(video) if use_html: t = Template(HTML_CONTAINER_TEMPLATE, filter=EncodeUnicode) else: t = Template(XML_CONTAINER_TEMPLATE, filter=EncodeUnicode) sortby = query.get('SortOrder', ['Normal'])[0].lower() t.sortby = sortby if use_html and resort: if sortby == 'capturedate': logger.info('re-sorting by captureDate, reverse=True') videos.sort(key=itemgetter('captureDate'), reverse=True) elif sortby == '!capturedate': logger.info('re-sorting by captureDate, reverse=False') videos.sort(key=itemgetter('captureDate'), reverse=False) t.container = handler.cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos if use_html: handler.send_html(str(t)) else: handler.send_xml(str(t))
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] if not self.get_local_path(handler, query): handler.send_error(404) return container = handler.container force_alpha = container.getboolean('force_alpha') ar = container.get('allow_recurse', 'auto').lower() if ar == 'auto': allow_recurse = not tsn or tsn[0] < '7' else: allow_recurse = ar in ('1', 'yes', 'true', 'on') files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha, allow_recurse) videos = [] local_base_path = self.get_local_base_path(handler, query) for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning('Bad file time on ' + unicode(f.name, 'utf-8')) mtime = time.time() ltime = time.localtime(mtime) video['captureDate'] = hex(int(mtime)) video['textDate'] = time.strftime('%b %d, %Y', ltime) video['name'] = os.path.basename(f.name) video['path'] = f.name video['part_path'] = f.name.replace(local_base_path, '', 1) if not video['part_path'].startswith(os.path.sep): video['part_path'] = os.path.sep + video['part_path'] video['title'] = os.path.basename(f.name) video['is_dir'] = f.isdir if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(f.name) else: if len(files) == 1 or f.name in transcode.info_cache: video['valid'] = transcode.supported_format(f.name) if video['valid']: video.update( self.metadata_full(f.name, tsn, mtime=mtime)) if len(files) == 1: video['captureDate'] = hex(isogm(video['time'])) else: video['valid'] = True video.update(metadata.basic(f.name, mtime)) if self.use_ts(tsn, f.name): video['mime'] = 'video/x-tivo-mpeg-ts' else: video['mime'] = 'video/x-tivo-mpeg' video['textSize'] = metadata.human_size(f.size) videos.append(video) t = Template(XML_CONTAINER_TEMPLATE, filter=EncodeUnicode) t.container = handler.cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos handler.send_xml(str(t))
def pre_cache(self, full_path): if Video.video_file_filter(self, full_path): transcode.supported_format(full_path)
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] # If you are running 8.3 software you want to enable hack83 # in the config file if config.getHack83(): print '=' * 73 query, hackPath = self.hack(handler, query, subcname) hackPath = '/'.join(hackPath) print 'Tivo said:', subcname, '|| Hack said:', hackPath debug_write(__name__, fn_attr(), ['Tivo said: ', subcname, ' || Hack said: ', hackPath]) subcname = hackPath if not query: debug_write(__name__, fn_attr(), ['sending 302 redirect page']) handler.send_response(302) handler.send_header( 'Location ', 'http://' + handler.headers.getheader('host') + '/TiVoConnect?Command=QueryContainer&' + 'AnchorItem=Hack8.3&Container=' + hackPath) handler.end_headers() return # End hack mess 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 container = handler.server.containers[cname] precache = container.get('precache', 'False').lower() == 'true' files, total, start = self.get_files(handler, query, self.video_file_filter) videos = [] local_base_path = self.get_local_base_path(handler, query) for file in files: mtime = datetime.fromtimestamp(os.stat(file).st_mtime) video = VideoDetails() video['captureDate'] = hex(int(time.mktime(mtime.timetuple()))) video['name'] = os.path.split(file)[1] video['path'] = file video['part_path'] = file.replace(local_base_path, '', 1) video['title'] = os.path.split(file)[1] video['is_dir'] = self.__isdir(file) if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(file) else: if precache or len(files) == 1 or file in transcode.info_cache: video['valid'] = transcode.supported_format(file) if video['valid']: video.update(self.__metadata_full(file, tsn)) else: video['valid'] = True video.update(self.__metadata_basic(file)) videos.append(video) handler.send_response(200) handler.end_headers() t = Template( file=os.path.join(SCRIPTDIR, 'templates', 'container.tmpl')) t.container = cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = handler.tivos handler.wfile.write(t)
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] useragent = handler.headers.getheader('User-Agent', '') if not self.get_local_path(handler, query): handler.send_error(404) return container = handler.container force_alpha = container.getboolean('force_alpha') use_html = query.get('Format', [''])[0].lower() == 'text/html' files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha) videos = [] local_base_path = self.get_local_base_path(handler, query) for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning('Bad file time on ' + unicode(f.name, 'utf-8')) mtime = int(time.time()) ltime = time.localtime(mtime) video['captureDate'] = hex(mtime) video['textDate'] = time.strftime('%b %d, %Y', ltime) video['name'] = os.path.basename(f.name) video['path'] = f.name video['part_path'] = f.name.replace(local_base_path, '', 1) if not video['part_path'].startswith(os.path.sep): video['part_path'] = os.path.sep + video['part_path'] video['title'] = os.path.basename(f.name) video['is_dir'] = f.isdir if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(f.name) else: if len(files) == 1 or f.name in transcode.info_cache: video['valid'] = transcode.supported_format(f.name) if video['valid']: video.update(self.metadata_full(f.name, tsn)) if len(files) == 1: video['captureDate'] = hex(isogm(video['time'])) else: video['valid'] = True video.update(metadata.basic(f.name)) if self.use_ts(tsn, f.name): video['mime'] = 'video/x-tivo-mpeg-ts' else: video['mime'] = 'video/x-tivo-mpeg' video['textSize'] = metadata.human_size(f.size) videos.append(video) logger.debug('mobileagent: %d useragent: %s' % (useragent.lower().find('mobile'), useragent.lower())) use_mobile = useragent.lower().find('mobile') > 0 if use_html: if use_mobile: t = Template(HTML_CONTAINER_TEMPLATE_MOBILE, filter=EncodeUnicode) else: t = Template(HTML_CONTAINER_TEMPLATE, filter=EncodeUnicode) else: t = Template(XML_CONTAINER_TEMPLATE, filter=EncodeUnicode) t.container = handler.cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos t.tivo_names = config.tivo_names if use_html: handler.send_html(str(t)) else: handler.send_xml(str(t))
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] # If you are running 8.3 software you want to enable hack83 # in the config file if config.getHack83(): print '=' * 73 query, hackPath = self.hack(handler, query, subcname) hackPath = '/'.join(hackPath) print 'Tivo said:', subcname, '|| Hack said:', hackPath debug_write(__name__, fn_attr(), ['Tivo said: ', subcname, ' || Hack said: ', hackPath]) subcname = hackPath if not query: debug_write(__name__, fn_attr(), ['sending 302 redirect page']) handler.send_response(302) handler.send_header('Location ', 'http://' + handler.headers.getheader('host') + '/TiVoConnect?Command=QueryContainer&' + 'AnchorItem=Hack8.3&Container=' + hackPath) handler.end_headers() return # End hack mess 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 container = handler.server.containers[cname] precache = container.get('precache', 'False').lower() == 'true' files, total, start = self.get_files(handler, query, self.video_file_filter) videos = [] local_base_path = self.get_local_base_path(handler, query) for file in files: mtime = datetime.fromtimestamp(os.stat(file).st_mtime) video = VideoDetails() video['captureDate'] = hex(int(time.mktime(mtime.timetuple()))) video['name'] = os.path.split(file)[1] video['path'] = file video['part_path'] = file.replace(local_base_path, '', 1) video['title'] = os.path.split(file)[1] video['is_dir'] = self.__isdir(file) if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(file) else: if precache or len(files) == 1 or file in transcode.info_cache: video['valid'] = transcode.supported_format(file) if video['valid']: video.update(self.__metadata_full(file, tsn)) else: video['valid'] = True video.update(self.__metadata_basic(file)) videos.append(video) handler.send_response(200) handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) t.container = cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = handler.tivos handler.wfile.write(t)
def QueryContainer(self, handler, query): tsn = handler.headers.getheader("tsn", "") subcname = query["Container"][0] if not self.get_local_path(handler, query): handler.send_error(404) return container = handler.container force_alpha = container.get("force_alpha", "False").lower() == "true" use_html = query.get("Format", [""])[0].lower() == "text/html" files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha) videos = [] local_base_path = self.get_local_base_path(handler, query) for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning("Bad file time on " + unicode(f.name, "utf-8")) mtime = int(time.time()) ltime = time.localtime(mtime) video["captureDate"] = hex(mtime) video["textDate"] = time.strftime("%b %d, %Y", ltime) video["name"] = os.path.basename(f.name) video["path"] = f.name video["part_path"] = f.name.replace(local_base_path, "", 1) if not video["part_path"].startswith(os.path.sep): video["part_path"] = os.path.sep + video["part_path"] video["title"] = os.path.basename(f.name) video["is_dir"] = f.isdir if video["is_dir"]: video["small_path"] = subcname + "/" + video["name"] video["total_items"] = self.__total_items(f.name) else: if len(files) == 1 or f.name in transcode.info_cache: video["valid"] = transcode.supported_format(f.name) if video["valid"]: video.update(self.metadata_full(f.name, tsn)) if len(files) == 1: video["captureDate"] = hex(isogm(video["time"])) else: video["valid"] = True video.update(metadata.basic(f.name)) if self.use_ts(tsn, f.name): video["mime"] = "video/x-tivo-mpeg-ts" else: video["mime"] = "video/x-tivo-mpeg" video["textSize"] = "%.3f GB" % (float(f.size) / (1024 ** 3)) videos.append(video) if use_html: t = Template(HTML_CONTAINER_TEMPLATE, filter=EncodeUnicode) else: t = Template(XML_CONTAINER_TEMPLATE, filter=EncodeUnicode) t.container = handler.cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos t.tivo_names = config.tivo_names if use_html: handler.send_html(str(t)) else: handler.send_xml(str(t))
def Push(self, handler, query): tsn = query['tsn'][0] for key in config.tivo_names: if config.tivo_names[key] == tsn: tsn = key break tivo_name = config.tivo_names.get(tsn, tsn) container = quote(query['Container'][0].split('/')[0]) ip = config.get_ip() port = config.getPort() baseurl = 'http://%s:%s' % (ip, port) if config.getIsExternal(tsn): exturl = config.get_server('externalurl') if exturl: baseurl = exturl else: ip = self.readip() baseurl = 'http://%s:%s' % (ip, port) path = self.get_local_base_path(handler, query) files = query.get('File', []) for f in files: file_path = path + os.path.normpath(f) file_info = VideoDetails() file_info['valid'] = transcode.supported_format(file_path) mime = 'video/mpeg' if config.isHDtivo(tsn): for m in ['video/mp4', 'video/bif']: if transcode.tivo_compatible(file_path, tsn, m)[0]: mime = m break if file_info['valid']: file_info.update(self.metadata_full(file_path, tsn, mime)) url = baseurl + '/%s%s' % (container, quote(f)) title = file_info['seriesTitle'] if not title: title = file_info['title'] source = file_info['seriesId'] if not source: source = title subtitle = file_info['episodeTitle'] try: m = mind.getMind(tsn) m.pushVideo( tsn = tsn, url = url, description = file_info['description'], duration = file_info['duration'] / 1000, size = file_info['size'], title = title, subtitle = subtitle, source = source, mime = mime, tvrating = file_info['tvRating']) except Exception, e: handler.send_response(500) handler.end_headers() handler.wfile.write('%s\n\n%s' % (e, traceback.format_exc() )) raise logger.info('[%s] Queued "%s" for Push to %s' % (time.strftime('%d/%b/%Y %H:%M:%S'), unicode(file_path, 'utf-8'), tivo_name))
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] if not self.get_local_path(handler, query): handler.send_error(404) return container = handler.container force_alpha = container.getboolean('force_alpha') ar = container.get('allow_recurse', 'auto').lower() if ar == 'auto': allow_recurse = not tsn or tsn[0] < '7' else: allow_recurse = ar in ('1', 'yes', 'true', 'on') files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha, allow_recurse) videos = [] local_base_path = self.get_local_base_path(handler, query) for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning('Bad file time on ' + unicode(f.name, 'utf-8')) mtime = time.time() ltime = time.localtime(mtime) video['captureDate'] = hex(int(mtime)) video['textDate'] = time.strftime('%b %d, %Y', ltime) video['name'] = os.path.basename(f.name) video['path'] = f.name video['part_path'] = f.name.replace(local_base_path, '', 1) if not video['part_path'].startswith(os.path.sep): video['part_path'] = os.path.sep + video['part_path'] video['title'] = os.path.basename(f.name) video['is_dir'] = f.isdir if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(f.name) else: if len(files) == 1 or f.name in transcode.info_cache: video['valid'] = transcode.supported_format(f.name) if video['valid']: video.update(self.metadata_full(f.name, tsn, mtime=mtime)) if len(files) == 1: video['captureDate'] = hex(isogm(video['time'])) else: video['valid'] = True video.update(metadata.basic(f.name, mtime)) if self.use_ts(tsn, f.name): video['mime'] = 'video/x-tivo-mpeg-ts' else: video['mime'] = 'video/x-tivo-mpeg' video['textSize'] = metadata.human_size(f.size) videos.append(video) t = Template(XML_CONTAINER_TEMPLATE, filter=EncodeUnicode) t.container = handler.cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos handler.send_xml(str(t))
def push_one_file(self, f): file_info = VideoDetails() file_info['valid'] = transcode.supported_format(f['path']) temp_share = config.get_server('temp_share', '') temp_share_path = '' remux_path = os.path.dirname(f['path']) if temp_share: for name, data in config.getShares(): if temp_share == name: temp_share_path = data.get('path') remux_path = temp_share_path mime = 'video/mpeg' if config.isHDtivo(f['tsn']): for m in ['video/mp4', 'video/bif']: if transcode.tivo_compatible(f['path'], f['tsn'], m)[0]: mime = m break if (mime == 'video/mpeg' and transcode.mp4_remuxable(f['path'], f['tsn'])): if config.get_freeSpace(remux_path, f['path']): new_path = transcode.mp4_remux(f['path'], f['name'], f['tsn'], temp_share_path) if new_path: mime = 'video/mp4' f['name'] = new_path if temp_share_path: ip = config.get_ip() port = config.getPort() container = quote(temp_share) + '/' f['url'] = 'http://%s:%s/%s' % (ip, port, container) else: logger.warning('Not enough disk space to perform remux, ' + 'transcoding instead.') if file_info['valid']: file_info.update(self.metadata_full(f['path'], f['tsn'], mime)) url = f['url'] + quote(f['name']) title = file_info['seriesTitle'] if not title: title = file_info['title'] source = file_info['seriesId'] if not source: source = title subtitle = file_info['episodeTitle'] try: m = mind.getMind(f['tsn']) m.pushVideo( tsn = f['tsn'], url = url, description = file_info['description'], duration = file_info['duration'] / 1000, size = file_info['size'], title = title, subtitle = subtitle, source = source, mime = mime, tvrating = file_info['tvRating']) except ValueError, msg: if 'usernamePasswordError' in msg: if f['name'].endswith('.pyTivo-temp'): fname = os.path.join(remux_path, os.path.basename(f['name'])) fname = unicode(fname, 'utf-8') os.remove(fname) logger.debug(fname + ' has been removed')
def QueryContainer(self, handler, query): tsn = handler.headers.getheader('tsn', '') subcname = query['Container'][0] cname = subcname.split('/')[0] if (not cname in handler.server.containers or not self.get_local_path(handler, query)): handler.send_error(404) return container = handler.server.containers[cname] precache = container.get('precache', 'False').lower() == 'true' force_alpha = container.get('force_alpha', 'False').lower() == 'true' files, total, start = self.get_files(handler, query, self.video_file_filter, force_alpha) videos = [] local_base_path = self.get_local_base_path(handler, query) for f in files: video = VideoDetails() mtime = f.mdate try: ltime = time.localtime(mtime) except: logger.warning('Bad file time on ' + unicode(f.name, 'utf-8')) mtime = int(time.time()) ltime = time.localtime(mtime) video['captureDate'] = hex(mtime) video['textDate'] = time.strftime('%b %d, %Y', ltime) video['name'] = os.path.split(f.name)[1] video['path'] = f.name video['part_path'] = f.name.replace(local_base_path, '', 1) if not video['part_path'].startswith(os.path.sep): video['part_path'] = os.path.sep + video['part_path'] video['title'] = os.path.split(f.name)[1] video['is_dir'] = f.isdir if video['is_dir']: video['small_path'] = subcname + '/' + video['name'] video['total_items'] = self.__total_items(f.name) else: if precache or len(files) == 1 or f.name in transcode.info_cache: video['valid'] = transcode.supported_format(f.name) if video['valid']: video.update(self.metadata_full(f.name, tsn)) else: video['valid'] = True video.update(metadata.basic(f.name)) video['textSize'] = ( '%.3f GB' % (float(f.size) / (1024 ** 3)) ) videos.append(video) t = Template(CONTAINER_TEMPLATE, filter=EncodeUnicode) t.container = cname t.name = subcname t.total = total t.start = start t.videos = videos t.quote = quote t.escape = escape t.crc = zlib.crc32 t.guid = config.getGUID() t.tivos = config.tivos t.tivo_names = config.tivo_names handler.send_response(200) handler.send_header('Content-Type', 'text/xml') handler.send_header('Expires', '0') handler.end_headers() handler.wfile.write(t)