def __init__(self, mode): # Create List for display showList = [plugin.getstr(571)] self.mode = mode # Load in Skin Codes from Database jsonData = storageDB.SkinCodes() if self.skin in jsonData: # Fetch viewmodes for selected mode self.viewModes = self.filterCodes(jsonData[self.skin], mode) # Append each key of viewModes to show list for i in sorted(self.viewModes.keys()): showList.append(i) # Fetch Current Mode if set and Show to user under Custom Mode ID self.currentMode = currentMode = plugin.getSetting("%s.%s.view" % (self.skin, mode)) if currentMode: showList.append("%s (%s)" % (plugin.getstr(636), currentMode)) else: showList.append(plugin.getstr(636)) # Display List self.display(showList)
def display(self, showList): # Bold the already selected view mode orgList = showList[:] if self.currentMode and len(showList) > 2: # Convert current viewmode to an interger currentMode = int(self.currentMode) for key, value in self.viewModes.iteritems(): # Check if current mode is found in viewModes if currentMode == value: # When found find its position in the list for count, i in enumerate(showList): # Check for required key if key == i: # Wen found, Bold and Indent the value showList[count] = "[B]-%s[/B]" % showList[count] break break # Display List to User ret = plugin.dialogSelect(plugin.getAddonData(self.skin, "name"), showList) if ret >= 0: # Take action depending on response response = orgList[ret] if response.startswith(plugin.getstr(636)): self.askForViewID() elif response == plugin.getstr(571): plugin.setSetting("%s.%s.view" % (self.skin, self.mode), "") else: plugin.setSetting("%s.%s.view" % (self.skin, self.mode), str(self.viewModes[str(response)]))
def handle_response(self, response): # Check if Response need to be decoded, else return raw response headers = response.info() contentCharset = headers.getparam("charset") or headers.getparam("encoding") contentEncoding = headers.get("Content-Encoding") contentType = headers.gettype() # If content is compressed then decompress and decode into unicode try: if contentEncoding and "gzip" in contentEncoding: rawdata = zlib.decompress(response.read(), 16+zlib.MAX_WBITS) elif contentEncoding and "deflate" in contentEncoding: rawdata = zlib.decompress(response.read()) else: rawdata = response.read() except zlib.error as e: raise plugin.URLError(plugin.getstr(32804), str(e)) else: # Convert content to unicode and back to utf-8 to fix any issues if contentType == "text/html": # if no charset was specified in the headers then attempt to fetch from content itself if contentCharset: plugin.debug("Character encoding: %s" % contentCharset) else: try: # Attempting to read charset from html body contentCharset = re.findall('<meta\s+http-equiv="Content-Type"\s+content=".*?charset=(\S+?)"\s+/>', rawdata)[0] plugin.debug("Character encoding: %s" % contentCharset) except: # Set Charset to default to UTF-8 plugin.debug("Response encoding not specified, Defaulting to UTF-8") contentCharset = "utf-8" # Convert html to unicode try: unicodeData = unicode(rawdata, contentCharset.lower()) except: # Attempt to decode using iso-8859-1 (latin-1) plugin.debug("Specified encoding failed, reverting to iso-8859-1 (latin-1)") try: unicodeData = unicode(rawdata, "iso-8859-1") except UnicodeDecodeError as e: raise plugin.URLError(plugin.getstr(32805), str(e)) # Unescape the content if requested if self.stripEntity: rawdata = self.unescape(unicodeData).encode("utf-8") else: rawdata = unicodeData.encode("utf-8") finally: # Close Http Resource Request response.close() # Return Data Wraped in an addinfourl Object addInfo = withaddinfourl(StringIO.StringIO(rawdata), headers, response.url, response.code) addInfo.msg = response.msg return addInfo
def htmlparser(self, htmlSource): ''' Parses the HTML Source into the Custom Parsers that Finds all Embeded Urls and Returns the List of Urls ''' try: # Try Parse HTML Using HTMLParser customParser = MainParser() customParser.parse(htmlSource) return list(customParser.sourceUrls) except: plugin.debug("HTML Parser Failed: Falling Back to Regex") try: # Try Parse HTML Using Regex customParser = RegexParser() customParser.parse(htmlSource) return list(customParser.sourceUrls) except: plugin.debug("Regex Parser Failed: Falling Beautiful Soup") try: # Try Parse HTML Using Beautiful Soup customParser = SoupParser() customParser.parse(htmlSource) return list(customParser.sourceUrls) except: plugin.debug("Beautiful Soup Parser Failed: All Out Total Failure") # Raise ParserError when all three parsers have failded raise plugin.ParserError(plugin.getstr(32826), "HTML Parsing Failed")
def wrapper(self, oarg): arg = oarg if arg[:4] == u"http": arg = self.strip(*urlparse.urlsplit(arg), returnID=True) if not arg: raise plugin.videoResolver(plugin.getstr(32821), u"Unable to Strip out videoID ==> %s ==> %s" % (pluginName, oarg)) else: # Call Function and return response plugin.debug("VideoResolver, Selected Source ==> %s ==> %s" % (pluginName, arg)) return function(self, arg)
def decode(self, arg): # Create Url String and Download HTML Page url = u"http://veehd.com/video/%s" % arg handle = urlhandler.HttpHandler() handle.add_response_handler() with handle.open(url) as resp: htmlSource = resp.read().decode("utf-8") # Fetch Video Info Page Url and Download HTML Page try: url = [u"http://veehd.com%s" % url for url in re.findall('attr\(\{src\s:\s"(/vpi\S+?)"\}\)',htmlSource) if u"do=d" in url][0] except IndexError as e: raise plugin.videoResolver(plugin.getstr(33077), str(e)) else: with handle.open(url) as resp: htmlSource = resp.read().decode("utf-8") # Search for Video Url try: return re.findall('<a\sstyle="color\:#31A0FF;"\shref="(http://.+?)">', htmlSource)[0] except IndexError as e: raise plugin.videoResolver(plugin.getstr(33077), "Was unable to Find Veehd Video Url or Decode It")
def store_in_cache(cachePath, response): ''' Saves Response and Headers to Cache ''' # Check if Cache Location is Valid cacheLocal = os.path.dirname(cachePath) if not os.path.exists(cacheLocal): os.makedirs(cacheLocal) # Save Headers to Cache outputFile = open(cachePath % u"headers", "wb") headers = response.info() if "Content-Encoding" in headers: del headers["Content-Encoding"] headers["X-Local-Cache"] = "HIT" headers["X-Location"] = response.url try: outputFile.write(str(headers)) except (IOError, OSError) as e: raise plugin.CacheError(plugin.getstr(32803), str(e)) finally: outputFile.close() # Save Response to Cache outputFile = open(cachePath % u"body", "wb") try: outputFile.write(response.read()) except (IOError, OSError) as e: raise plugin.CacheError(plugin.getstr(32803), str(e)) finally: outputFile.close() # Close Response Connection response.close()
def decode(self, arg): # Create Url String and Download HTML Page url = u"https://player.vimeo.com/video/%s" % arg handle = urlhandler.HttpHandler() handle.add_response_handler() with handle.open(url) as resp: htmlSource = resp.read() # Fetch Video Info Page Url and Download HTML Page try: jsonData = re.findall('"h264":({.+?}),"hls"', htmlSource)[0] except IndexError as e: raise plugin.videoResolver(plugin.getstr(33077), str(e)) # Convert String to json Object import fastjson as json try: urlData = sorted([(value["height"], value["url"]) for value in json.loads(jsonData).values()], key=lambda x: x[0], reverse=True) except (ValueError, KeyError) as e: raise plugin.videoResolver(plugin.getstr(33077), str(e)) # Fetch Video Quality and return video try: quality = int(plugin.getSetting("quality").replace(u"p",u"")) except ValueError: quality = 720 for res, url in urlData: if res <= quality: return url
def parse(self, arg, maxAge=57600): ''' Takes in a url to a online resource or the HTML to that online resource and Passes it into the Custom Parsers to Find Embeded Sources ''' # Check if arg is a URL Then Fetch HTML Source if arg.startswith(u"http://") or arg.startswith(u"https://"): sourceObj = urlhandler.urlopen(arg, maxAge) htmlSource = sourceObj.read().decode("utf8") sourceObj.close() else: htmlSource = arg # Feed the HTML into the HTMLParser and acquire the source urls sourceUrls = self.htmlparser(htmlSource) if not sourceUrls: raise plugin.ParserError(plugin.getstr(32827), "No Video Sources ware found") else: self.setUrls(sourceUrls)
def http_response(self, request, response): headers = response.info() loginPage = self.loginPage if loginPage and (headers.get("location") == loginPage or headers.get("uri") == loginPage): # Check if Logon has already happend within current session if self.logonAtempted is True: raise plugin.URLError(plugin.getstr(32806), "Logon Already Atempted, Stopping infinite loop") else: self.logonAtempted = True # Login to site and create session cookie plugin.debug("Sending Login Data") urllogin(**self.loginData) # Resend Request for Data with new session cookie request = self.http_request(request) return self.parent.open(request) else: self.cookiejar.extract_cookies(response, request) self.save_cookies() return response
def decode(self, arg): # Check if arg is the embeded ID or VideoID if arg.isdigit(): url = "http://blip.tv/rss/flash/%s" % arg else: # Fetch Redirected Url with the numeric id url = u"http://blip.tv/play/%s" % arg reUrl = urlhandler.redirect(url) # Filter out VideoCode try: url = plugin.parse_qs(reUrl[reUrl.find("?")+1:])["file"] except (ValueError, KeyError) as e: raise plugin.videoResolver(plugin.getstr(33077), str(e)) # Fetch XMLSource handle = urlhandler.HttpHandler() handle.add_response_handler() sourceObj = handle.open(url) import xml.etree.ElementTree as ElementTree media = u"http://search.yahoo.com/mrss/" tree = ElementTree.parse(sourceObj) sourceObj.close() # Fetch list of Media Content Found filtered = ((int(node.get(u"height",0)), node.attrib) for node in tree.getiterator(u"{%s}content" % media) if not node.get(u"type") == u"text/plain") filtered = sorted(filtered, key=lambda x: x[0], reverse=True) filtered = sorted(filtered, key=lambda x: x[1]["isDefault"] == "true", reverse=True) # Fetch Video Quality and return video try: quality = int(plugin.getSetting("quality").replace(u"p",u"")) except ValueError: quality = 720 for res, content in filtered: if res <= quality: return {"url":content[u"url"], "type":content[u"type"]} # Fallback to hightest available quality videoInfo = filtered[0][1] return {"url":videoInfo[u"url"], "type":videoInfo[u"type"]}
def askForViewID(self): # Display Numeric Dialog ret = plugin.dialogNumeric(0, plugin.getstr(611), self.currentMode) if ret: plugin.setSetting("%s.%s.view" % (self.skin, self.mode), str(ret))
def readFile(filename): ''' Return content of file and auto close file ''' try: with open(filename, "rb") as fileObject: return fileObject.read() except (IOError, OSError) as e: raise plugin.CacheError(plugin.getstr(32803), str(e))