fix toutiao errors

This commit is contained in:
jason 2019-03-29 22:01:08 +08:00
parent 2e461820f8
commit 10cc42f1fb
2 changed files with 32 additions and 18 deletions

View File

@ -1,27 +1,36 @@
#!/usr/bin/env python #!/usr/bin/env python
import base64
import binascii import binascii
from ..common import *
import random import random
from json import loads from json import loads
from urllib.parse import urlparse
from ..common import *
try:
from base64 import decodebytes
except ImportError:
from base64 import decodestring
decodebytes = decodestring
__all__ = ['toutiao_download', ] __all__ = ['toutiao_download', ]
def random_with_n_digits(n):
return random.randint(10 ** (n - 1), (10 ** n) - 1)
def sign_video_url(vid): def sign_video_url(vid):
# some code from http://codecloud.net/110854.html r = str(random_with_n_digits(16))
r = str(random.random())[2:]
def right_shift(val, n): url = 'https://ib.365yg.com/video/urls/v/1/toutiao/mp4/{vid}'.format(vid=vid)
return val >> n if val >= 0 else (val + 0x100000000) >> n n = urlparse(url).path + '?r=' + r
b_n = bytes(n, encoding="utf-8")
url = 'http://i.snssdk.com/video/urls/v/1/toutiao/mp4/%s' % vid s = binascii.crc32(b_n)
n = url.replace("http://i.snssdk.com", "")+ '?r=' + r aid = 1364
c = binascii.crc32(n.encode("ascii")) ts = int(time.time() * 1000)
s = right_shift(c, 0) return url + '?r={r}&s={s}&aid={aid}&vfrom=xgplayer&callback=axiosJsonpCallback1&_={ts}'.format(r=r, s=s, aid=aid,
return url + '?r=%s&s=%s' % (r, s) ts=ts)
class ToutiaoVideoInfo(object): class ToutiaoVideoInfo(object):
@ -43,12 +52,12 @@ def get_file_by_vid(video_id):
vRet = [] vRet = []
url = sign_video_url(video_id) url = sign_video_url(video_id)
ret = get_content(url) ret = get_content(url)
ret = loads(ret) ret = loads(ret[20:-1])
vlist = ret.get('data').get('video_list') vlist = ret.get('data').get('video_list')
if len(vlist) > 0: if len(vlist) > 0:
vInfo = vlist.get(sorted(vlist.keys(), reverse=True)[0]) vInfo = vlist.get(sorted(vlist.keys(), reverse=True)[0])
vUrl = vInfo.get('main_url') vUrl = vInfo.get('main_url')
vUrl = base64.decodestring(vUrl.encode('ascii')).decode('ascii') vUrl = decodebytes(vUrl.encode('ascii')).decode('ascii')
videoInfo = ToutiaoVideoInfo() videoInfo = ToutiaoVideoInfo()
videoInfo.bitrate = vInfo.get('bitrate') videoInfo.bitrate = vInfo.get('bitrate')
videoInfo.definition = vInfo.get('definition') videoInfo.definition = vInfo.get('definition')
@ -63,8 +72,8 @@ def get_file_by_vid(video_id):
def toutiao_download(url, output_dir='.', merge=True, info_only=False, **kwargs): def toutiao_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
html = get_html(url, faker=True) html = get_html(url, faker=True)
video_id = match1(html, r"videoid\s*:\s*'([^']+)',\n") video_id = match1(html, r".*?videoId: '(?P<vid>.*)'")
title = match1(html, r"title: '([^']+)'.replace") title = match1(html, '.*?<title>(?P<title>.*?)</title>')
video_file_list = get_file_by_vid(video_id) # 调api获取视频源文件 video_file_list = get_file_by_vid(video_id) # 调api获取视频源文件
type, ext, size = url_info(video_file_list[0].url, faker=True) type, ext, size = url_info(video_file_list[0].url, faker=True)
print_info(site_info=site_info, title=title, type=type, size=size) print_info(site_info=site_info, title=title, type=type, size=size)

View File

@ -7,6 +7,7 @@ from you_get.extractors import (
magisto, magisto,
youtube, youtube,
bilibili, bilibili,
toutiao,
) )
@ -31,5 +32,9 @@ class YouGetTests(unittest.TestCase):
info_only=True info_only=True
) )
def test_toutiao(self):
toutiao.download('https://www.365yg.com/i6640053613567675662/#mid=1611922564114440', info_only=True)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()