handler.send_header('Content-Type', 'text/html; charset=UTF-8') handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR,'templates', 'npl.tmpl')) t.folder = folder t.status = status t.tivo_mak = tivo_mak t.togo_path = togo_path t.tivos = handler.tivos t.tivoIP = tivoIP t.container = cname t.data = data t.unquote = unquote t.len = len t.TotalItems = int(TotalItems) t.ItemStart = int(ItemStart) t.ItemCount = int(ItemCount) t.FirstAnchor = quote(FirstAnchor) t.shows_per_page = shows_per_page t.redirect = quote(unquote_plus(handler.path).split('/')[1]) handler.wfile.write(unicode(t).encode('utf-8')) def get_tivo_file(self, url, mak, tivoIP, outfile): #global status cj = cookielib.LWPCookieJar() r=urllib2.Request(url) auth_handler = urllib2.HTTPDigestAuthHandler() auth_handler.add_password('TiVo DVR', tivoIP, 'tivo', mak) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), auth_handler) urllib2.install_opener(opener)
handler.send_header('Content-Type', 'text/html; charset=UTF-8') handler.end_headers() t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'npl.tmpl')) t.folder = folder t.status = status t.tivo_mak = tivo_mak t.togo_path = togo_path t.tivos = handler.tivos t.tivoIP = tivoIP t.container = cname t.data = data t.unquote = unquote t.len = len t.TotalItems = int(TotalItems) t.ItemStart = int(ItemStart) t.ItemCount = int(ItemCount) t.FirstAnchor = quote(FirstAnchor) t.shows_per_page = shows_per_page t.redirect = quote(unquote_plus(handler.path).split('/')[1]) handler.wfile.write(unicode(t).encode('utf-8')) def get_tivo_file(self, url, mak, tivoIP, outfile): #global status cj = cookielib.LWPCookieJar() r = urllib2.Request(url) auth_handler = urllib2.HTTPDigestAuthHandler() auth_handler.add_password('TiVo DVR', tivoIP, 'tivo', mak) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), auth_handler) urllib2.install_opener(opener)
def NPL(handler, query): """ ToGo.NPL returns an html page displaying the now playing list (NPL) from a particular TiVo device. The query may specify: - TiVo: the IPv4 address of the TiVo whose NPL is to be retrieved - ItemCount: the number of shows/folders to put on the page (default: 50, max: 50) - AnchorItem: the url identifying the 1st item in the retrieved list (default 1st item in folder) - AnchorOffset: the offset from the AnchorItem to start the retrieval from (default 0) - SortOrder: - Recurse: """ def getint(thing): try: result = int(thing) except: # pylint: disable=bare-except result = 0 return result shows_per_page = 50 # Change this to alter the number of shows returned (max is 50) if 'ItemCount' in query: shows_per_page = int(query['ItemCount'][0]) if shows_per_page > 50: shows_per_page = 50 folder = '' FirstAnchor = '' has_tivodecode = bool(config.get_bin('tivodecode')) has_tivolibre = bool(config.get_bin('tivolibre')) if 'TiVo' in query: tivoIP = query['TiVo'][0] try: tsn = config.tivos_by_ip(tivoIP) attrs = config.tivos[tsn] tivo_name = attrs.get('name', tivoIP) tivo_mak = config.get_tsn('tivo_mak', tsn) except config.Error as e: logger.error('NPL: %s', e) t = Template(ERROR_TEMPLATE) t.e = e t.additional_info = 'Your browser may have cached an old page' handler.send_html(str(t)) return protocol = attrs.get('protocol', 'https') ip_port = '%s:%d' % (tivoIP, attrs.get('port', 443)) path = attrs.get('path', DEFPATH) baseurl = '%s://%s%s' % (protocol, ip_port, path) theurl = baseurl if 'Folder' in query: folder = query['Folder'][0] theurl = urljoin(theurl, folder) theurl += '&ItemCount=%d' % shows_per_page if 'AnchorItem' in query: theurl += '&AnchorItem=' + quote(query['AnchorItem'][0]) if 'AnchorOffset' in query: theurl += '&AnchorOffset=' + query['AnchorOffset'][0] if 'SortOrder' in query: theurl += '&SortOrder=' + query['SortOrder'][0] if 'Recurse' in query: theurl += '&Recurse=' + query['Recurse'][0] if (theurl not in tivo_cache or (time.time() - tivo_cache[theurl]['thepage_time']) >= 60): # if page is not cached or old then retrieve it auth_handler.add_password('TiVo DVR', ip_port, 'tivo', tivo_mak) logger.debug('NPL: (1) add password for TiVo DVR netloc: %s', ip_port) try: logger.debug("NPL.theurl: %s", theurl) with tivo_open(theurl) as page: tivo_cache[theurl] = {'thepage': minidom.parse(page), 'thepage_time': time.time()} except IOError as e: handler.redir(UNABLE % (tivoIP, html.escape(str(e))), 10) return xmldoc = tivo_cache[theurl]['thepage'] items = xmldoc.getElementsByTagName('Item') TotalItems = tag_data(xmldoc, 'TiVoContainer/Details/TotalItems') ItemStart = tag_data(xmldoc, 'TiVoContainer/ItemStart') ItemCount = tag_data(xmldoc, 'TiVoContainer/ItemCount') title = tag_data(xmldoc, 'TiVoContainer/Details/Title') if items: FirstAnchor = tag_data(items[0], 'Links/Content/Url') data = [] for item in items: entry = {} for tag in ('CopyProtected', 'ContentType'): value = tag_data(item, 'Details/' + tag) if value: entry[tag] = value if entry['ContentType'].startswith('x-tivo-container'): entry['Url'] = tag_data(item, 'Links/Content/Url') entry['Title'] = tag_data(item, 'Details/Title') entry['TotalItems'] = tag_data(item, 'Details/TotalItems') lc = tag_data(item, 'Details/LastCaptureDate') if not lc: lc = tag_data(item, 'Details/LastChangeDate') entry['LastChangeDate'] = time.strftime('%b %d, %Y', time.localtime(int(lc, 16))) else: keys = {'Icon': 'Links/CustomIcon/Url', 'Url': 'Links/Content/Url', 'Details': 'Links/TiVoVideoDetails/Url', 'SourceSize': 'Details/SourceSize', 'Duration': 'Details/Duration', 'CaptureDate': 'Details/CaptureDate'} for key in keys: value = tag_data(item, keys[key]) if value: entry[key] = value if 'SourceSize' in entry: rawsize = entry['SourceSize'] entry['SourceSize'] = metadata.human_size(rawsize) if 'Duration' in entry: dur = getint(entry['Duration']) // 1000 entry['Duration'] = ('%d:%02d:%02d' % (dur // 3600, (dur % 3600) // 60, dur % 60)) if 'CaptureDate' in entry: entry['CaptureDate'] = time.strftime('%b %d, %Y', time.localtime(int(entry['CaptureDate'], 16))) dnld_url = entry['Url'] # the tivo download url seems to always be absolute, so is this necessary? # I'm commenting it out -mjl 7/23/2017 #dnld_url = urljoin(baseurl, dnld_url) if not dnld_url in showinfo: showinfo[dnld_url] = ShowInfo() showinfo[dnld_url].from_tivo_container_item(item) entry.update(showinfo[dnld_url].get_old_basicmeta()) data.append(entry) else: data = [] tivoIP = '' TotalItems = 0 ItemStart = 0 ItemCount = 0 title = '' tsn = '' tivo_name = '' t = Template(NPL_TEMPLATE) t.quote = quote t.folder = folder t.urlstatus = ToGo.get_urlstatus(tivoIP) t.has_tivodecode = has_tivodecode t.has_tivolibre = has_tivolibre t.togo_mpegts = config.is_ts_capable(tsn) t.tname = tivo_name t.tivoIP = tivoIP t.container = handler.cname t.data = data t.len = len t.TotalItems = getint(TotalItems) t.ItemStart = getint(ItemStart) t.ItemCount = getint(ItemCount) t.FirstAnchor = quote(FirstAnchor) t.shows_per_page = shows_per_page t.title = title handler.send_html(str(t), refresh='300')
t = Template(CONTAINER_TEMPLATE, filter=EncodeUnicode) t.escape = escape t.quote = quote t.folder = folder t.status = status if tivoIP in queue: t.queue = queue[tivoIP] t.has_tivodecode = has_tivodecode t.tname = tivo_name t.tivoIP = tivoIP t.container = handler.cname t.data = data t.len = len t.TotalItems = getint(TotalItems) t.ItemStart = getint(ItemStart) t.ItemCount = getint(ItemCount) t.FirstAnchor = quote(FirstAnchor) t.shows_per_page = shows_per_page t.title = title handler.send_html(str(t), refresh='300') def get_tivo_file(self, tivoIP, url, mak, togo_path): # global status status[url].update({'running': True, 'queued': False}) parse_url = urlparse.urlparse(url) name = unquote(parse_url[2])[10:].split('.') id = unquote(parse_url[4]).split('id=')[1] name.insert(-1, ' - ' + id + '.') if status[url]['decode']:
t = Template(NPL_TEMPLATE, filter=EncodeUnicode) t.quote = quote t.folder = folder t.status = status if tivoIP in queue: t.queue = queue[tivoIP] t.has_tivodecode = has_tivodecode t.togo_mpegts = config.is_ts_capable(tsn) t.tname = tivo_name t.tivoIP = tivoIP t.container = handler.cname t.data = data t.len = len t.TotalItems = getint(TotalItems) t.ItemStart = getint(ItemStart) t.ItemCount = getint(ItemCount) t.FirstAnchor = quote(FirstAnchor) t.shows_per_page = shows_per_page t.title = title handler.send_html(str(t), refresh='300') def get_tivo_file(self, tivoIP, url, mak, togo_path): # global status status[url].update({'running': True, 'queued': False}) parse_url = urlparse.urlparse(url) if status[url]['save']: meta = basic_meta[url] name = unicode(unquote(parse_url[2]),