Merge branch 'fix-letv' of https://github.com/jackyzy823/you-get into jackyzy823-fix-letv

This commit is contained in:
Mort Yao 2015-02-09 14:11:25 +08:00
commit f1f682c246

View File

@ -5,16 +5,17 @@ __all__ = ['letv_download', 'letvcloud_download', 'letvcloud_download_by_vu']
import json
import random
import xml.etree.ElementTree as ET
import base64, hashlib, urllib
import base64, hashlib, urllib, time, re
from ..common import *
#@DEPRECATED
def get_timestamp():
tn = random.random()
url = 'http://api.letv.com/time?tn={}'.format(tn)
result = get_content(url)
return json.loads(result)['stime']
#@DEPRECATED
def get_key(t):
for s in range(0, 8):
e = 1 & t
@ -23,46 +24,78 @@ def get_key(t):
t += e
return t ^ 185025305
def video_info(vid):
tn = get_timestamp()
key = get_key(tn)
#old api reserve for future use or for example
# url = 'http://api.letv.com/mms/out/video/play?id={}&platid=1&splatid=101&format=1&tkey={}&domain=www.letv.com'.format(vid, key)
# print(url)
# r = get_content(url, decoded=False)
# print(r)
# xml_obj = ET.fromstring(r)
# info = json.loads(xml_obj.find("playurl").text)
# title = info.get('title')
# urls = info.get('dispatch')
# for k in urls.keys():
# url = urls[k][0]
# break
# url += '&termid=1&format=0&hwtype=un&ostype=Windows7&tag=letv&sign=letv&expect=1&pay=0&rateid={}'.format(k)
# return url, title
def calcTimeKey(t):
ror = lambda val, r_bits, : ((val & (2**32-1)) >> r_bits%32) | (val << (32-(r_bits%32)) & (2**32-1))
return ror(ror(t,773625421%13)^773625421,773625421%17)
url="http://api.letv.com/mms/out/common/geturl?platid=3&splatid=301&playid=0&vtype=9,13,21,28&version=2.0&tss=no&vid={}&domain=www.letv.com&tkey={}".format(vid,key)
def decode(data):
version = data[0:5]
if version.lower() == b'vc_01':
#get real m3u8
loc2 = data[5:]
length = len(loc2)
loc4 = [0]*(2*length)
for i in range(length):
loc4[2*i] = loc2[i] >> 4
loc4[2*i+1]= loc2[i] & 15;
loc6 = loc4[len(loc4)-11:]+loc4[:len(loc4)-11]
loc7 = [0]*length
for i in range(length):
loc7[i] = (loc6[2 * i] << 4) +loc6[2*i+1]
return ''.join([chr(i) for i in loc7])
else:
# directly return
return data
def video_info(vid,**kwargs):
url = 'http://api.letv.com/mms/out/video/playJson?id={}&platid=1&splatid=101&format=1&tkey={}&domain=www.letv.com'.format(vid,calcTimeKey(int(time.time())))
r = get_content(url, decoded=False)
info=json.loads(str(r,"utf-8"))
size=0
for i in info["data"][0]["infos"]: #0 means only one file not truncated.need to upgrade
if int(i["gsize"])>size:
size=int(i["gsize"])
url=i["mainUrl"]
url+="&ctv=pc&m3v=1&termid=1&format=1&hwtype=un&ostype=Linux&tag=letv&sign=letv&expect=3&tn={}&pay=0&iscpn=f9051&rateid=1300".format(random.random())
# url += '&termid=1&format=0&hwtype=un&ostype=Windows7&tag=letv&sign=letv&expect=1&pay=0&rateid=1000' #{}'.format(k)
stream_id = None
support_stream_id = info["playurl"]["dispatch"].keys()
if "stream_id" in kwargs and kwargs["stream_id"].lower() in support_stream_id:
stream_id = kwargs["stream_id"]
else:
print("Current Video Supports:")
for i in support_stream_id:
print("\t--format",i,"<URL>")
if "1080p" in support_stream_id:
stream_id = '1080p'
elif "720p" in support_stream_id:
stream_id = '720p'
else:
stream_id =sorted(support_stream_id,key= lambda i: int(i[1:]))[-1]
url =info["playurl"]["domain"][0]+info["playurl"]["dispatch"][stream_id][0]
ext = info["playurl"]["dispatch"][stream_id][1].split('.')[-1]
url+="&ctv=pc&m3v=1&termid=1&format=1&hwtype=un&ostype=Linux&tag=letv&sign=letv&expect=3&tn={}&pay=0&iscpn=f9051&rateid={}".format(random.random(),stream_id)
r2=get_content(url,decoded=False)
info2=json.loads(str(r2,"utf-8"))
return info2["location"]
def letv_download_by_vid(vid,title, output_dir='.', merge=True, info_only=False):
url= video_info(vid)
_, _, size = url_info(url)
ext = 'flv'
# hold on ! more things to do
# to decode m3u8 (encoded)
m3u8 = get_content(info2["location"],decoded=False)
m3u8_list = decode(m3u8)
urls = re.findall(r'^[^#][^\r]*',m3u8_list,re.MULTILINE)
return ext,urls
def letv_download_by_vid(vid,title, output_dir='.', merge=True, info_only=False,**kwargs):
ext , urls = video_info(vid,**kwargs)
size = 0
for i in urls:
_, _, tmp = url_info(i)
size += tmp
print_info(site_info, title, ext, size)
if not info_only:
download_urls([url], title, ext, size, output_dir=output_dir, merge=merge)
download_urls(urls, title, ext, size, output_dir=output_dir, merge=merge)
def letvcloud_download_by_vu(vu, title=None, output_dir='.', merge=True, info_only=False):
str2Hash = 'cfflashformatjsonran0.7214574650861323uu2d8c027396ver2.1vu' + vu + 'bie^#@(%27eib58'
@ -90,7 +123,7 @@ def letvcloud_download(url, output_dir='.', merge=True, info_only=False):
title = "LETV-%s" % vu
letvcloud_download_by_vu(vu, title=title, output_dir=output_dir, merge=merge, info_only=info_only)
def letv_download(url, output_dir='.', merge=True, info_only=False):
def letv_download(url, output_dir='.', merge=True, info_only=False ,**kwargs):
if re.match(r'http://yuntv.letv.com/', url):
letvcloud_download(url, output_dir=output_dir, merge=merge, info_only=info_only)
else:
@ -101,7 +134,7 @@ def letv_download(url, output_dir='.', merge=True, info_only=False):
else:
vid = match1(html, r'vid="(\d+)"')
title = match1(html,r'name="irTitle" content="(.*?)"')
letv_download_by_vid(vid, title=title, output_dir=output_dir, merge=merge, info_only=info_only)
letv_download_by_vid(vid, title=title, output_dir=output_dir, merge=merge, info_only=info_only,**kwargs)
site_info = "LeTV.com"
download = letv_download