Skip to content

plugins.mitele: fix and refactor plugin #4760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
plugins.mitele: fix and refactor plugin
  • Loading branch information
bastimeyer committed Aug 20, 2022
commit 3b0c5c367c4ffdc67803496454c2db7f71aff5bc
128 changes: 68 additions & 60 deletions src/streamlink/plugins/mitele.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,83 +21,91 @@
r"https?://(?:www\.)?mitele\.es/directo/(?P<channel>[\w-]+)"
))
class Mitele(Plugin):
caronte_url = "https://caronte.mediaset.es/delivery/channel/mmc/{channel}/mtweb"
gbx_url = "https://mab.mediaset.es/1.0.0/get?oid=mtmw&eid=%2Fapi%2Fmtmw%2Fv2%2Fgbx%2Fmtweb%2Flive%2Fmmc%2F{channel}"
URL_CARONTE = "https://caronte.mediaset.es/delivery/channel/mmc/{channel}/mtweb"
URL_GBX = "https://mab.mediaset.es/1.0.0/get"

error_schema = validate.Schema({"code": int})
caronte_schema = validate.Schema(validate.parse_json(), validate.any(
{
"cerbero": validate.url(),
"bbx": str,
"dls": [{
"lid": validate.all(int, validate.transform(str)),
"format": validate.any("hls", "dash", "smooth"),
"stream": validate.url(),
validate.optional("assetKey"): str,
"drm": bool,
}],
},
error_schema,
))
gbx_schema = validate.Schema(
validate.parse_json(),
{"gbx": str},
validate.get("gbx")
)
cerbero_schema = validate.Schema(
validate.parse_json(),
validate.any(
validate.all(
{"tokens": {str: {"cdn": str}}},
validate.get("tokens")
),
error_schema,
)
)
token_errors = {
4038: "User has no privileges"
TOKEN_ERRORS = {
4038: "User has no privileges",
}

def _get_streams(self):
channel = self.match.group("channel")

pdata = self.session.http.get(self.caronte_url.format(channel=channel),
acceptable_status=(200, 403, 404),
schema=self.caronte_schema)
gbx = self.session.http.get(self.gbx_url.format(channel=channel),
schema=self.gbx_schema)

pdata = self.session.http.get(
self.URL_CARONTE.format(channel=channel),
acceptable_status=(200, 403, 404),
schema=validate.Schema(
validate.parse_json(),
validate.any(
{"code": int},
{
"cerbero": validate.url(),
"bbx": str,
"dls": validate.all(
[{
"drm": bool,
"format": str,
"stream": validate.url(),
"lid": validate.all(int, validate.transform(str)),
validate.optional("assetKey"): str,
}],
validate.filter(lambda obj: obj["format"] == "hls")
),
},
),
),
)
if "code" in pdata:
log.error("error getting pdata: {}".format(pdata["code"]))
log.error(f"Error getting pdata: {pdata['code']}")
return

tokens = self.session.http.post(pdata["cerbero"],
acceptable_status=(200, 403, 404),
json={"bbx": pdata["bbx"], "gbx": gbx},
headers={"origin": "https://www.mitele.es"},
schema=self.cerbero_schema)
gbx = self.session.http.get(
self.URL_GBX,
params={
"oid": "mtmw",
"eid": f"/api/mtmw/v2/gbx/mtweb/live/mmc/{channel}",
},
schema=validate.Schema(
validate.parse_json(),
{"gbx": str},
validate.get("gbx"),
),
)

tokens = self.session.http.post(
pdata["cerbero"],
acceptable_status=(200, 403, 404),
json={
"bbx": pdata["bbx"],
"gbx": gbx,
},
headers={"origin": "https://www.mitele.es"},
schema=validate.Schema(
validate.parse_json(),
validate.any(
{"code": int},
validate.all(
{"tokens": {str: {"cdn": str}}},
validate.get("tokens")
),
),
),
)
if "code" in tokens:
log.error("Could not get stream tokens: {} ({})".format(tokens["code"],
self.token_errors.get(tokens["code"], "unknown error")))
tokenerrors = self.TOKEN_ERRORS.get(tokens["code"], "unknown error")
log.error(f"Could not get stream tokens: {tokens['code']} ({tokenerrors})")
return

list_urls = []
urls = set()
for stream in pdata["dls"]:
if stream["drm"]:
log.warning("Stream may be protected by DRM")
else:
sformat = stream.get("format")
log.debug("Stream: {} ({})".format(stream["stream"], sformat or "n/a"))
cdn_token = tokens.get(stream["lid"], {}).get("cdn", "")
qsd = parse_qsd(cdn_token)
if sformat == "hls":
list_urls.append(update_qsd(stream["stream"], qsd))

if not list_urls:
return
continue
cdn_token = tokens.get(stream["lid"], {}).get("cdn", "")
qsd = parse_qsd(cdn_token)
urls.add(update_qsd(stream["stream"], qsd, quote_via=lambda string, *_, **__: string))

for url in list(set(list_urls)):
for url in urls:
yield from HLSStream.parse_variant_playlist(self.session, url, name_fmt="{pixels}_{bitrate}").items()


Expand Down