from .common import InfoExtractor
-from urllib.request import Request
-from urllib.parse import urlencode
import json
-import random as rnd
+import random
+import re
+
+from ..compat import (
+ compat_parse_qs,
+ compat_str,
+)
+from ..utils import (
+ js_to_json,
+ strip_jsonp,
+ urlencode_postdata,
+)
+
class WeiboIE(InfoExtractor):
- _VALID_URL = r'https?://weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)'
+ _VALID_URL = r'https?://(?:www\.)?weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)'
_TEST = {
- 'url': 'https://weibo.com/6275294458/Fp6RGfbff?from=page_1005056275294458_profile&wvr=6&mod=weibotime&type=comment',
- 'md5': 'TODO: md5 sum of the first 10241 bytes of the video file (use --test)',
- 'info_dict': {
- 'id': '42',
- 'ext': 'mp4',
- 'title': 'Video title goes here',
- 'thumbnail': r're:^https?://.*\.jpg$',
- # TODO more properties, either as:
- # * A value
- # * MD5 checksum; start the string with md5:
- # * A regular expression; start the string with re:
- # * Any Python type (for example int or float)
- }
- }
+ 'url': 'https://weibo.com/6275294458/Fp6RGfbff?type=comment',
+ 'info_dict': {
+ 'id': 'Fp6RGfbff',
+ 'ext': 'mp4',
+ 'title': 'You should have servants to massage you,... 来自Hosico_猫 - 微博',
+ }
+ }
def _real_extract(self, 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
- 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)
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 = {
- 'Accept-Encoding': 'gzip, deflate, br',
- 'Accept': '*/*',
- 'Referer': visitor_url,
- }
-
- r_genvisitor = Request(
- 'https://passport.weibo.com/visitor/genvisitor',
- data = data,
- headers = headers,
- method = 'POST'
- )
- webpage,urlh = self._download_webpage_handle(r_genvisitor, video_id, note="gen visitor")
- print("webpage", webpage)
-
- p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
- i1 = p.find('{')
- i2 = p.rfind('}')
- j = p[i1:i2+1] # get JSON object
- d = json.loads(j)
- tid = d["data"]["tid"]
- cnfd = "%03d" % d["data"]["confidence"]
-
- param = urlencode({
- 'a': 'incarnate',
- 't': tid,
- 'w': 2,
- 'c': cnfd,
- 'cb': 'cross_domain',
- 'from': 'weibo',
- '_rand': rnd.random()
+ if 'passport.weibo.com' in visitor_url:
+ # first visit
+ visitor_data = self._download_json(
+ 'https://passport.weibo.com/visitor/genvisitor', video_id,
+ note='Generating first-visit data',
+ transform_source=strip_jsonp,
+ headers={'Referer': visitor_url},
+ data=urlencode_postdata({
+ 'cb': 'gen_callback',
+ 'fp': json.dumps({
+ 'os': '2',
+ 'browser': 'Gecko57,0,0,0',
+ 'fonts': 'undefined',
+ 'screenInfo': '1440*900*24',
+ 'plugins': '',
+ }),
+ }))
+
+ tid = visitor_data['data']['tid']
+ cnfd = '%03d' % visitor_data['data']['confidence']
+
+ self._download_webpage(
+ 'https://passport.weibo.com/visitor/visitor', video_id,
+ note='Running first-visit callback',
+ query={
+ 'a': 'incarnate',
+ 't': tid,
+ 'w': 2,
+ 'c': cnfd,
+ 'cb': 'cross_domain',
+ 'from': 'weibo',
+ '_rand': random.random(),
+ })
+
+ webpage = self._download_webpage(
+ url, video_id, note='Revisiting webpage')
+
+ title = self._html_search_regex(
+ r'<title>(.+?)</title>', webpage, 'title')
+
+ video_formats = compat_parse_qs(self._search_regex(
+ r'video-sources=\\\"(.+?)\"', webpage, 'video_sources'))
+
+ formats = []
+ supported_resolutions = (480, 720)
+ for res in supported_resolutions:
+ vid_urls = video_formats.get(compat_str(res))
+ if not vid_urls or not isinstance(vid_urls, list):
+ continue
+
+ vid_url = vid_urls[0]
+ formats.append({
+ 'url': vid_url,
+ 'height': res,
})
- gencallback_url = "https://passport.weibo.com/visitor/visitor?" + param
- webpage,urlh = self._download_webpage_handle(gencallback_url, video_id, note="gen callback")
- print("webpage", webpage)
- webpage,urlh = self._download_webpage_handle(url, video_id, headers=headers, note="retry to visit the page")
- print("webpage", webpage)
+ self._sort_formats(formats)
- # TODO more code goes here, for example ...
- title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
+ uploader = self._og_search_property(
+ 'nick-name', webpage, 'uploader', default=None)
- video_sources = self._search_regex(r'video-sources=(.+?)', webpage, 'video_sources')
- print("video_sources:", video_sources)
return {
- 'id': video_id,
- 'title': title,
- 'description': self._og_search_description(webpage),
- 'uploader': self._search_regex(r'<div[^>]+id="uploader"[^>]*>([^<]+)<', webpage, 'uploader', fatal=False),
- # TODO more properties (see youtube_dl/extractor/common.py)
- }
+ 'id': video_id,
+ 'title': title,
+ 'uploader': uploader,
+ 'formats': formats
+ }
+
+
+class WeiboMobileIE(InfoExtractor):
+ _VALID_URL = r'https?://m\.weibo\.cn/status/(?P<id>[0-9]+)(\?.+)?'
+ _TEST = {
+ 'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0',
+ 'info_dict': {
+ 'id': '4189191225395228',
+ 'ext': 'mp4',
+ 'title': '午睡当然是要甜甜蜜蜜的啦',
+ 'uploader': '柴犬柴犬'
+ }
+ }
+
+ def _real_extract(self, url):
+ video_id = self._match_id(url)
+ # to get Referer url for genvisitor
+ webpage = self._download_webpage(url, video_id, note='visit the page')
+
+ weibo_info = self._parse_json(self._search_regex(
+ r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\]\s*\|\|\s*{};',
+ webpage, 'js_code', flags=re.DOTALL),
+ video_id, transform_source=js_to_json)
+
+ status_data = weibo_info.get('status', {})
+ page_info = status_data.get('page_info')
+ title = status_data['status_title']
+ uploader = status_data.get('user', {}).get('screen_name')
+
+ return {
+ 'id': video_id,
+ 'title': title,
+ 'uploader': uploader,
+ 'url': page_info['media_info']['stream_url']
+ }