/
reuters.py
93 lines (80 loc) · 3.15 KB
/
reuters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
$description Global business, financial, national and international news.
$url reuters.com
$url reuters.tv
$type live, vod
"""
import logging
import re
from streamlink.plugin import Plugin, PluginError, pluginmatcher
from streamlink.plugin.api import validate
from streamlink.stream.hls import HLSStream
log = logging.getLogger(__name__)
@pluginmatcher(re.compile(
r"https?://([\w-]+\.)*reuters\.(com|tv)",
))
class Reuters(Plugin):
def _get_data(self):
root = self.session.http.get(self.url, schema=validate.Schema(
validate.parse_html(),
))
try:
log.debug("Trying to find source via meta tag")
schema = validate.Schema(
validate.xml_xpath_string(".//meta[@property='og:video'][1]/@content"),
validate.url(),
)
return schema.validate(root)
except PluginError:
pass
try:
log.debug("Trying to find source via next-head")
schema = validate.Schema(
validate.xml_findtext(".//script[@type='application/ld+json'][@class='next-head']"),
validate.parse_json(),
{"contentUrl": validate.url()},
validate.get("contentUrl"),
)
return schema.validate(root)
except PluginError:
pass
schema_fusion = validate.xml_findtext(".//script[@type='application/javascript'][@id='fusion-metadata']")
schema_video = validate.all(
{"source": {"hls": validate.url()}},
validate.get(("source", "hls")),
)
try:
log.debug("Trying to find source via fusion-metadata globalContent")
schema = validate.Schema(
schema_fusion,
validate.regex(re.compile(r"Fusion\s*\.\s*globalContent\s*=\s*(?P<json>{.+?})\s*;\s*Fusion\s*\.", re.DOTALL)),
validate.get("json"),
validate.parse_json(),
{"result": {"related_content": {"videos": list}}},
validate.get(("result", "related_content", "videos", 0)),
schema_video,
)
return schema.validate(root)
except PluginError:
pass
try:
log.debug("Trying to find source via fusion-metadata contentCache")
schema = validate.Schema(
schema_fusion,
validate.regex(re.compile(r"Fusion\s*\.\s*contentCache\s*=\s*(?P<json>{.+?})\s*;\s*Fusion\s*\.", re.DOTALL)),
validate.get("json"),
validate.parse_json(),
{"videohub-by-guid-v1": {str: {"data": {"result": {"videos": list}}}}},
validate.get("videohub-by-guid-v1"),
validate.transform(lambda obj: obj[next(iter((obj.keys())))]),
validate.get(("data", "result", "videos", 0)),
schema_video,
)
return schema.validate(root)
except PluginError:
pass
def _get_streams(self):
hls_url = self._get_data()
if hls_url:
return HLSStream.parse_variant_playlist(self.session, hls_url)
__plugin__ = Reuters