Created
February 22, 2017 15:38
-
-
Save itsuwari/c17beb01394e9116afcd7d9b4e99e2c8 to your computer and use it in GitHub Desktop.
This script will get play urls form PPTV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import requests | |
# Use same session | |
s = requests.Session() | |
from time import sleep | |
# DEPRECATED in favor of match1() | |
def r1(pattern, text): | |
m = re.search(pattern, text) | |
if m: | |
return m.group(1) | |
def generate_k(dtkey, dtsh, dtst, dtid, dtbh, dtiv): | |
import hashlib | |
from Crypto.Cipher import AES | |
from binascii import unhexlify, hexlify | |
app_key = "V8oo0Or1f047NaiMTxK123LMFuINTNeI" | |
decrypt = unhexlify(dtkey[0:32]) | |
key = dtsh+dtst+dtid+dtbh+dtiv+app_key | |
key = hashlib.sha256(key.encode()).digest() | |
crypto = AES.new(key, AES.MODE_ECB) | |
decrypted = hexlify(crypto.decrypt(decrypt)) | |
return str(decrypted)[2:-1] + dtkey[32:] | |
def pptv_download(url, video_id): | |
url = url[:url.find('.html')+5] | |
#assert re.match(r'http://v.pptv.com/show/(\w+)\.html$', url) | |
html = str(requests.get(url).text) | |
id = r1(r'webcfg\s*=\s*{"id":\s*(\d+)', html) | |
assert id | |
pptv_download_by_id(id, video_id) | |
def pptv_download_by_id(id, video_id=0): | |
param = 'version=5¶m=type=web.fpp&ahl_ver=1&userType=0&o=www.google.co.jp&ppi=9402c4338&pageUrl=http://v.pptv.com/show/Jm4zsZTm7y2QDnY.html?rcc_src=S1&type=web.fpp&sl=2&scver=2&appplt=flp&appid=pptv.flashplayer.vod&appver=3.4.2.11' | |
xml = requests.get('http://web-play.pptv.com/webplay3-0-%s.xml?%s' % (id, param)).text | |
# print(xml) | |
# &ahl_ver=1 get HD videos | |
# vt=3 means vod mode vt=5 means live mode | |
import xml.etree.ElementTree as ET | |
root = ET.fromstring(xml) | |
files = root.findall('channel')[0].find('file') | |
for file in files.findall('item'): | |
rid = file.get('rid') | |
dt = root.findall('dt')[len(root.findall('dt'))-1] | |
dtsh = host = str(dt.find('sh').text) | |
dtid = str(dt.find('id').text) | |
dtiv = str(dt.find('iv').text) | |
dtbh = str(dt.find('bh').text) | |
dtkey = str(dt.find('key').text) | |
dtst = st = str(dt.find('st').text) | |
k = generate_k(dtkey, dtsh, dtst, dtid, dtbh, dtiv) | |
# ft = int(r1(r'/><item rid="%s".{20,28}ft="([^"]+)".{12,260}</file>' % str(rid), xml)) - 1 | |
title = r1(r'nm="([^"]+)"', xml) | |
# ft="[1,2..]" decide video quality | |
urls = [] | |
total_size = 0 | |
i = 0 | |
for sgm in root.findall('dragdata')[-1].findall('sgm'): | |
total_size += int(sgm.get('fs')) | |
url = "http://{}/{}/0/9902/{}?fpp.ver=1.3.0.21&type=web.fpp&k={}".format(host, i, rid, k) | |
urls.append(url) | |
i += 1 | |
from lib.download import download_urls | |
download_urls(urls, video_id, pools=11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment