Skip to content

Instantly share code, notes, and snippets.

@nightcoffee
Last active June 2, 2019 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nightcoffee/dad38232a532b36a61b6 to your computer and use it in GitHub Desktop.
Save nightcoffee/dad38232a532b36a61b6 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import urllib
import requests
import json
import execjs
VIDEO_ID = 'lZnqmZ4ZTP8'
START_TOKEN = 'ytplayer.config = '
END_TOKEN = ';ytplayer.load'
def convert_to_dict(data):
result = {}
list = data.split('&')
for item in list:
pair = item.split('=')
key = urllib.unquote_plus(pair[0])
value = urllib.unquote_plus(pair[1])
result[key] = value
return result
def parse_js(data):
start = data.find("""a.splice(0,b)""")
start = data.rfind(";var", 0 , start)
end = data.find("""return a.join("")};""", start)
js = data[start:end+len("""return a.join("")};""")]
start = js.find("""function """)
end = js.find("""(a)""" , start)
name = js[start+len("""function """):end]
return name, js
if __name__ == '__main__':
print "Using javascript interpreter:", execjs.get().name
headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36',
'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch'}
response = requests.get("https://www.youtube.com/watch?v=%s" % VIDEO_ID, headers=headers)
data = response.content
start = data.find(START_TOKEN)
end = data.find(END_TOKEN)
data = data[start+len(START_TOKEN):end]
data = json.loads(data)
js = 'https:' + data['assets']['js']
response = requests.get(js)
jscontent = response.content
func, jscontent = parse_js(jscontent)
ctxt = execjs.compile(jscontent)
data = data['args']
print "Title:", data['title']
list = data['adaptive_fmts'].split(',')
parsed = []
for item in list:
info = convert_to_dict(item)
if info['url'].find("&signature=") == -1:
info['url'] = info['url'] +"&signature=" + ctxt.call(func, info['s'])
parsed.append(info)
for item in parsed:
if item['type'].startswith("video"):
print "%-35s|%-10s%2sfps|%-10s|%s" % (item['type'], item['size'], item['fps'], "%0.2fMiB" % (float(item['clen'])/1024/1024), item['url'])
else:
print "%-35s|%-10s%5s|%-10s|%s" % (item['type'], "" , "" , "%0.2fMiB" % (float(item['clen'])/1024/1024), item['url'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment