Skip to content

Instantly share code, notes, and snippets.

@steve-thousand
Forked from einstein95/hqq.py
Created November 7, 2020 01:23
Show Gist options
  • Save steve-thousand/7b27ca75a7e97874af22a67c07101d6a to your computer and use it in GitHub Desktop.
Save steve-thousand/7b27ca75a7e97874af22a67c07101d6a to your computer and use it in GitHub Desktop.
Downloads most hqq.tv streams
import json
import re
import urllib.request
from base64 import b64decode
from subprocess import call
from sys import argv
UA = 'Mozilla/6.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.5) Gecko/2008092417 Firefox/3.0.3'
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Content-Type': 'text/html; charset=utf-8'}
def request(url, headers={}):
# print(('request: %s' % url))
req = urllib.request.Request(url, headers=headers)
req.add_header('User-Agent', UA)
try:
response = urllib.request.urlopen(req)
data = response.read().decode('utf-8')
response.close()
except urllib.error.HTTPError as error:
data=error.read().decode('utf-8')
# print(('len(data) %s' % len(data)))
return data
def post(url, data, headers={}):
postdata = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url, postdata, headers)
req.add_header('User-Agent', UA)
try:
response = urllib.request.urlopen(req)
data = response.read().decode('utf-8')
response.close()
except urllib.error.HTTPError as error:
data=error.read().decode('utf-8')
# print(('len(data) %s' % len(data)))
return data
def request_noagent(url, headers={}):
# print(('request: %s' % url))
req = urllib.request.Request(url, headers=headers)
req.add_header('User-Agent', ' ')
try:
response = urllib.request.urlopen(req)
data = response.read().decode('utf-8')
response.close()
except urllib.error.HTTPError as error:
data=error.read().decode('utf-8')
# print(('len(data) %s' % len(data)))
return data
def _decode(data):
b64dec = b64decode(re.search(r'base64([^\"]+)', data).group(1))
enc = re.search(r"\'([^']+)\'", b64dec.decode('utf-8')).group(1)
escape = re.search(r"var _escape=\'([^\']+)", b64decode(enc[::-1]).decode('utf-8')).group(1)
escape = escape.replace('%', '\\').encode('utf-8').decode('unicode-escape')
data = re.findall(r'<input name="([^"]+?)" [^>]+? value="([^"]*)">', escape)
return dict(data)
def _decode2(data):
s = ''
for n in re.findall(r'(...)', b64decode(data).decode('ascii')):
s += chr(int(n) - 156)
return(s)
url = argv[1]
if not url.startswith('http'):
url = 'http://' + url
page = request(url)
vid = re.search(r'(?:hqq|netu)\.tv/player/embed_player\.php\?vid=([0-9A-Za-z]+)', page)
if not vid:
vid = re.search(r'(?:hqq|netu)\.tv/watch_video\.php\?v=([0-9A-Za-z]+)', url)
if not vid:
vid = re.search(r'(hqq|netu)\.tv/player/hash\.php\?hash=\d+', url)
if vid:
vid = re.search(r'var vid = \'([^\']+)\'', urllib.parse.unquote(request(url)))
vid = vid.group(1)
player_url = 'http://hqq.tv/player/embed_player.php?vid={}&autoplay=no'.format(vid)
data = request(player_url, headers)
post_data = _decode(data)
data = post(player_url, post_data, headers)
post_data = _decode(data)
data = urllib.parse.unquote(request("http://hqq.tv/sec/player/embed_player.php?" + urllib.parse.urlencode(post_data), headers))
linkvarname, servervarname = re.findall(r'link_1: ([^,]+), server_1: ([^,]+)', data)[0]
vid_server = re.search(r'var {} = "([^"]+?)"'.format(servervarname), data)
vid_link = re.search(r'var {} = "([^"]+?)"'.format(linkvarname), data)
at = re.search(r'var at = "([^"]+?)"', data)
if vid_server and vid_link and at:
# print(at)
# at = '0'*32
get_data = {'server_1': vid_server.group(1), 'link_1': re.sub(r'\?socket=?$', '.mp4.m3u8', vid_link.group(1)), 'at': at.group(1), 'adb': '1/', 'b' : '1', 'vid' : vid}
data = request("http://hqq.tv/player/get_md5.php?" + urllib.parse.urlencode(get_data), {**headers, **{'X-Requested-With':'XMLHttpRequest'}})
jsondata = json.loads(data)
decodedm3u = urllib.parse.unquote('%' + '%'.join(re.findall(r'.(..)', jsondata['html5_file'][1:])))
# print(decodedm3u)
filename = decodedm3u.split('/')[-1][:-9]
call('ffmpeg -user_agent "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us)" -i "{}" -c copy {}.mkv'.format(decodedm3u, filename), shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment