def _schema_consent(data): schema_consent = validate.Schema( validate.parse_html(), validate.any( validate.xml_find(".//form[@action='https://consent.youtube.com/s']"), validate.all( validate.xml_xpath(".//form[@action='https://consent.youtube.com/save']"), validate.filter(lambda elem: elem.xpath(".//input[@type='hidden'][@name='set_ytc'][@value='true']")), validate.get(0), ) ), validate.union(( validate.get("action"), validate.xml_xpath(".//input[@type='hidden']"), )), ) return schema_consent.validate(data)
def test_failure_schema(self): with pytest.raises(validate.ValidationError) as cm: validate.validate(validate.xml_xpath("."), "not-an-element") assert_validationerror( cm.value, """ ValidationError(Callable): iselement('not-an-element') is not true """)
def test_xml_xpath(self): root = Element("root") foo = Element("foo") bar = Element("bar") baz = Element("baz") root.append(foo) root.append(bar) foo.append(baz) assert validate(xml_xpath("./descendant-or-self::node()"), root) == [root, foo, baz, bar], "Returns correct node set" assert validate(xml_xpath("./child::qux"), root) is None, "Returns None when node set is empty" assert validate( xml_xpath("name(.)"), root) == "root", "Returns function values instead of node sets" self.assertRaises(ValueError, validate, xml_xpath("."), "not an Element")
def get_channels(self): data = self.session.http.get( self.url, schema=validate.Schema( validate.parse_html(), validate.xml_xpath( ".//*[contains(@class,'channel-list')]//a[@data-id][@data-code]" ), [ validate.union_get("data-id", "data-code"), ], )) return {k: v for k, v in data}
def _get_streams(self): root = self.session.http.get(self.url, schema=validate.Schema( validate.parse_html())) for needle, errormsg in ( ( "This service is not available in your Country", "The content is not available in your region", ), ( "Silahkan login Menggunakan akun MyIndihome dan berlangganan minipack", "The content is not available without a subscription", ), ): if validate.Schema( validate.xml_xpath( """.//script[contains(text(), '"{0}"')]""".format( needle))).validate(root): log.error(errormsg) return url = validate.Schema( validate.any( validate.all( validate.xml_xpath_string(""" .//script[contains(text(), 'laylist.m3u8') or contains(text(), 'manifest.mpd')][1]/text() """), validate.text, validate.transform( re.compile( r"""(?P<q>['"])(?P<url>https://.*?/(?:[Pp]laylist\.m3u8|manifest\.mpd).+?)(?P=q)""" ).search), validate.any( None, validate.all(validate.get("url"), validate.url())), ), validate.all( validate.xml_xpath_string( ".//video[@id='video-player']/source/@src"), validate.any(None, validate.url()), ), )).validate(root) if url and ".m3u8" in url: return HLSStream.parse_variant_playlist(self.session, url) elif url and ".mpd" in url: return DASHStream.parse_manifest(self.session, url)
def test_other(self, element): assert validate.validate(validate.xml_xpath("local-name(.)"), element) == "root"
def test_empty(self, element): assert validate.validate(validate.xml_xpath("invalid"), element) is None
def test_simple(self, element): assert validate.validate(validate.xml_xpath("*"), element) == [ element[0], element[1], element[2] ] assert validate.validate(validate.xml_xpath("*/text()"), element) == ["FOO", "BAR", "BAZ"]