Skip to content

Instantly share code, notes, and snippets.

@einstein95
Last active January 23, 2022 20:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save einstein95/7a58e86a0267c139fcf591054928f25e to your computer and use it in GitHub Desktop.
Save einstein95/7a58e86a0267c139fcf591054928f25e 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)
@iamjoezac
Copy link

how to run this file?

@abirkan
Copy link

abirkan commented Dec 26, 2017

how to run this file???

@bagapitos
Copy link

Logan, hello!
I tried to run your code and I get the following error on line 76:
Quote
File "hqq.py", line 76, in
vid = vid.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
Quote

I am trying to execute it on any "https://kinemania.tv" video by the format:
python hqq.py "https://kinemania.tv"

Is there something that I am missing or simply the code cannot find the video to parse?
Any help would be appreciated! :)

Thank you,
V.

@John61590
Copy link

Doesn't work for me.

I understand this script requires it to be http link. When I use https://hqq.tv/player/embed_player.php?vid=241260264273261222275264245204244207194271217271255&autoplay=none It does not work. crash at

Traceback (most recent call last):
File "hqq.py", line 90, in
post_data = _decode(data)
File "hqq.py", line 56, in _decode
b64dec = b64decode(re.search(r'base64([^\"]+)', data).group(1))
AttributeError: 'NoneType' object has no attribute 'group'

data = request(player_url, headers)
returns another HTML page. Not a Base64 for me.

@eberamp
Copy link

eberamp commented Jun 10, 2019

It might not work because:

A) I believe they updated their streaming protection
B) As far as I can tell this script uses regex to search the URL passed as an argument in the entire webpage source code so you might not get past the AttributeError since it didn't find it, which means the video url wasn't in plain view

What I have seen is that they use as of the time of this post an iframe and they break the stream into smaller peaces so even if you download something you only get 20 second videos and it might have something to do with the uploader preferences of not sharing outside of the their site, if you stop certain scripts you can see loading a player for the video its thumbnails but you won't be able to play it since you also need the scripts to start the streaming process and once you enable them again it probably checks these settings/preferences and you get a 404.

Copy link

ghost commented Feb 16, 2021

You can use Stream Recorder to download video from hqq.tv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment