sprhawk 2017-12-26 22:20:43 +08:00
parent 2593651224
commit 42a1012c77

View File

@ -4,13 +4,12 @@
from .common import InfoExtractor from .common import InfoExtractor
import json import json
import random as rnd import random
import re import re
from ..compat import ( from ..compat import (
compat_urllib_parse_urlencode as urlencode, compat_urllib_parse_urlencode,
compat_urllib_request as request, compat_urlparse,
compat_urlparse as parse,
) )
from ..utils import ( from ..utils import (
js_to_json, js_to_json,
@ -30,34 +29,28 @@ class WeiboIE(InfoExtractor):
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) video_id = self._match_id(url)
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'Upgrade-Insecure-Requests': '1',
}
# to get Referer url for genvisitor # to get Referer url for genvisitor
webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="first visit the page") webpage, urlh = self._download_webpage_handle(url, video_id, note="first visit the page")
visitor_url = urlh.geturl() visitor_url = urlh.geturl()
data = urlencode({
"cb": "gen_callback",
"fp": '{"os":"2","browser":"Gecko57,0,0,0","fonts":"undefined","screenInfo":"1440*900*24","plugins":""}',
}).encode()
headers = { headers = {
'Accept-Encoding': 'gzip, deflate, br', 'Referer': visitor_url
'Accept': '*/*',
'Referer': visitor_url,
} }
r_genvisitor = request.Request( fp = {
'https://passport.weibo.com/visitor/genvisitor', "os": "2",
data=data, "browser": "Gecko57,0,0,0",
headers=headers, "fonts": "undefined",
) "screenInfo": "1440*900*24",
webpage, urlh = self._download_webpage_handle(r_genvisitor, video_id, note="gen visitor") "plugins": ""
}
data = compat_urllib_parse_urlencode({
"cb": "gen_callback",
"fp": json.dumps(fp),
}).encode()
genvisitor_url = 'https://passport.weibo.com/visitor/genvisitor'
webpage, _ = self._download_webpage_handle(genvisitor_url, video_id, data=data, headers=headers, note="gen visitor")
p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)" p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
i1 = p.find('{') i1 = p.find('{')
@ -67,29 +60,28 @@ def _real_extract(self, url):
tid = d["data"]["tid"] tid = d["data"]["tid"]
cnfd = "%03d" % d["data"]["confidence"] cnfd = "%03d" % d["data"]["confidence"]
param = urlencode({ query = {
'a': 'incarnate', 'a': 'incarnate',
't': tid, 't': tid,
'w': 2, 'w': 2,
'c': cnfd, 'c': cnfd,
'cb': 'cross_domain', 'cb': 'cross_domain',
'from': 'weibo', 'from': 'weibo',
'_rand': rnd.random() '_rand': random.random()
}) }
gencallback_url = "https://passport.weibo.com/visitor/visitor?" + param gencallback_url = "https://passport.weibo.com/visitor/visitor"
webpage, urlh = self._download_webpage_handle(gencallback_url, video_id, note="gen callback") self._download_webpage_handle(gencallback_url, video_id, note="gen callback", query=query)
webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="retry to visit the page") webpage, _ = self._download_webpage_handle(url, video_id, note="retry to visit the page")
# TODO more code goes here, for example ...
title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title') title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
video_sources_text = self._search_regex(r'video-sources=\\\"(.+?)\"', webpage, 'video_sources') video_sources_text = self._search_regex(r'video-sources=\\\"(.+?)\"', webpage, 'video_sources')
video_formats = parse.parse_qs(video_sources_text) video_formats = compat_urlparse.parse_qs(video_sources_text)
formats = [] formats = []
supported_resolutions = ['720', '480'] supported_resolutions = ('720', '480')
for res in supported_resolutions: for res in supported_resolutions:
f = video_formats.get(res) f = video_formats.get(res)
if isinstance(f, list): if isinstance(f, list):
@ -107,12 +99,11 @@ def _real_extract(self, url):
'title': title, 'title': title,
'uploader': uploader, 'uploader': uploader,
'formats': formats 'formats': formats
# TODO more properties (see youtube_dl/extractor/common.py)
} }
class WeiboMobileIE(InfoExtractor): class WeiboMobileIE(InfoExtractor):
_VALID_URL = r'https?://m.weibo.cn/status/(?P<id>[0-9]+)(\?.+)?' _VALID_URL = r'https?://m\.weibo\.cn/status/(?P<id>[0-9]+)(\?.+)?'
_TEST = { _TEST = {
'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0', 'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0',
'info_dict': { 'info_dict': {
@ -125,29 +116,17 @@ class WeiboMobileIE(InfoExtractor):
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) video_id = self._match_id(url)
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
'Upgrade-Insecure-Requests': '1',
}
# to get Referer url for genvisitor # to get Referer url for genvisitor
webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="visit the page") webpage, _ = self._download_webpage_handle(url, video_id, note="visit the page")
js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags=re.DOTALL) js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags=re.DOTALL)
weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json) weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json)
page_info = weibo_info['status']['page_info'] page_info = weibo_info['status']['page_info']
title = weibo_info['status']['status_title'] title = weibo_info.get('status').get('status_title')
format = { uploader = weibo_info.get('status').get('user').get('screen_name')
'url': page_info['media_info']['stream_url'],
'format': 'mp4',
}
formats = [format]
uploader = weibo_info['status']['user']['screen_name']
return { return {
'id': video_id, 'id': video_id,
'title': title, 'title': title,
'uploader': uploader, 'uploader': uploader,
'formats': formats 'url': page_info['media_info']['stream_url']
# TODO more properties (see youtube_dl/extractor/common.py)
} }