Skip to content

Instantly share code, notes, and snippets.

@NickCis
Created November 24, 2023 14:56
Show Gist options
  • Save NickCis/1e0b2d17f72e401f29b36c86bbee45cf to your computer and use it in GitHub Desktop.
Save NickCis/1e0b2d17f72e401f29b36c86bbee45cf to your computer and use it in GitHub Desktop.
Extraer link de iframe de https://www.mega.cl/senal-en-vivo/
SHELL := /usr/bin/bash
.PHONY: activate clean format run help
help: # Show help for each of the Makefile recipes.
@grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done
venv: requirements.txt # Crea carpeta venv
python3 -m venv venv
source ./venv/bin/activate && \
pip install --upgrade pip && \
pip install -r requirements.txt
activate: venv # Corre una terminal con venv activado
source ./venv/bin/activate && $${SHELL}
format: venv # Ejecuta autoformateo
source ./venv/bin/activate && \
pip install autopep8 && \
autopep8 -i -r mediastream-extractor.py
clean: # Borra venv
rm -rf ./venv
#! /usr/bin/python
## Ejecutar python mediastream-extractor.py 'https://www.mega.cl/senal-en-vivo/'
import re
import hjson
import argparse
import urllib.request
from urllib.parse import urlencode
# Es el que tiene yt-dlp
UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.70 Safari/537.36'
def fetch(url, headers={}):
req = urllib.request.Request(
url,
data=None,
headers={
'User-Agent': UserAgent,
**headers,
}
)
with urllib.request.urlopen(req) as res:
return res.read().decode('utf-8')
def main(url):
web = fetch(url)
# var video = {id: '53d2c1a32640614e62a0e000',type: 'live',c3: 'SENALENVIVO',dfp: '/mega.cl/senal-en-vivo/home/',autoplay: 'true',loop: 'false',mse: true,code: 'mga-577fbf8be7709',atentusTrack: true,volume: 0,player: '5e5597b9171de20f8f30667a',useToken: true,serverKey : '1053d2c1a32640614e62a0e00010$zJBj3.sgbBli22KGOxEJV.Xm2bl1CnfyKTO81Dhf01sjpfmKltReK1700755694', kc: true}
m = re.search('var video = ({[^}]*});', web)
if not m:
print("No se encontró variable de video")
return
video = hjson.loads(m.group(1))
# https://api.mega.cl/api/v1/mdstrm?id=53d2c1a32640614e62a0e000&ua=Mozilla%2F5.0+(X11%3B+Linux+x86_64)+AppleWebKit%2F537.36+(KHTML%2C+like+Gecko)+Chrome%2F119.0.0.0+Safari%2F537.36&type=live&process=access_token&key=1053d2c1a32640614e62a0e00010%24zJBj3.sgbBli22KGOxEJV.Xm2bl1CnfyKTO81Dhf01sjpfmKltReK1700755694
params = urlencode({
'id': video['id'],
'ua': UserAgent,
'type': video['type'],
'process': 'access_token',
'key': video['serverKey'],
})
accessTokenURL = f'https://api.mega.cl/api/v1/mdstrm?{params}'
token = fetch(accessTokenURL, {'Origin': 'https://www.mega.cl'})
if token == "false":
print("Error al obtener token")
return
tokenParsed = hjson.loads(token)
# https://mdstrm.com/live-stream/53d2c1a32640614e62a0e000?jsapi=true&loop=false&autoplay=true&volume=0&player=5e5597b9171de20f8f30667a&access_token=WmHnkdGUdYGmzCXpUSMk094ni65BWH9YRV1iin3oGbrZX5gTduFe7px0qyhf7JuHw6lVi9BCmhV&mse=true&ads[map]=&custom.dfp=%2Fmega.cl%2Fsenal-en-vivo%2Fhome%2F&custom.kv=seccion%253Dotros%2526nivel%253Dhome%2526nota%253D%2526id_nota%253D%2526tipo%253Dotro%2526sev_companion%253Dsev_companion&custom.desc_url=https%3A%2F%2Fwww.mega.cl%2Fsenal-en-vivo%2F
params = urlencode({
'jsapi': 'true',
'loop': video['loop'],
'autoplay': video['autoplay'],
'volume': '0',
'player': video['player'],
'access_token': tokenParsed['access_token'],
})
iframeURL = f"https://mdstrm.com/live-stream/{video['id']}?{params}"
print(iframeURL)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog='mediastream-extractor',
description='Extrae el m3u8 de una web que contiene un iframe de MediaStream',
)
parser.add_argument('url')
args = parser.parse_args()
main(args.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment