]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/weibo.py
fix according to "https://github.com/rg3/youtube-dl/pull/15079#discussion_r158688607"
[yt-dlp.git] / youtube_dl / extractor / weibo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 import json
7 import random
8 import re
9
10 from ..compat import (
11 compat_urllib_parse_urlencode,
12 compat_urlparse,
13 )
14 from ..utils import (
15 js_to_json,
16 )
17
18
19 class WeiboIE(InfoExtractor):
20 _VALID_URL = r'https?://weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)'
21 _TEST = {
22 'url': 'https://weibo.com/6275294458/Fp6RGfbff?type=comment',
23 'info_dict': {
24 'id': 'Fp6RGfbff',
25 'ext': 'mp4',
26 'title': 'You should have servants to massage you,... 来自Hosico_猫 - 微博',
27 }
28 }
29
30 def _real_extract(self, url):
31 video_id = self._match_id(url)
32 # to get Referer url for genvisitor
33 webpage, urlh = self._download_webpage_handle(url, video_id, note="first visit the page")
34
35 visitor_url = urlh.geturl()
36 headers = {
37 'Referer': visitor_url
38 }
39
40 fp = {
41 "os": "2",
42 "browser": "Gecko57,0,0,0",
43 "fonts": "undefined",
44 "screenInfo": "1440*900*24",
45 "plugins": ""
46 }
47 data = compat_urllib_parse_urlencode({
48 "cb": "gen_callback",
49 "fp": json.dumps(fp),
50 }).encode()
51
52 genvisitor_url = 'https://passport.weibo.com/visitor/genvisitor'
53 webpage, _ = self._download_webpage_handle(genvisitor_url, video_id, data=data, headers=headers, note="gen visitor")
54
55 p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
56 i1 = p.find('{')
57 i2 = p.rfind('}')
58 j = p[i1:i2 + 1] # get JSON object
59 d = json.loads(j)
60 tid = d["data"]["tid"]
61 cnfd = "%03d" % d["data"]["confidence"]
62
63 query = {
64 'a': 'incarnate',
65 't': tid,
66 'w': 2,
67 'c': cnfd,
68 'cb': 'cross_domain',
69 'from': 'weibo',
70 '_rand': random.random()
71 }
72 gencallback_url = "https://passport.weibo.com/visitor/visitor"
73 self._download_webpage_handle(gencallback_url, video_id, note="gen callback", query=query)
74
75 webpage, _ = self._download_webpage_handle(url, video_id, note="retry to visit the page")
76
77 title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
78
79 video_sources_text = self._search_regex(r'video-sources=\\\"(.+?)\"', webpage, 'video_sources')
80
81 video_formats = compat_urlparse.parse_qs(video_sources_text)
82
83 formats = []
84 supported_resolutions = ('720', '480')
85 for res in supported_resolutions:
86 f = video_formats.get(res)
87 if isinstance(f, list):
88 if len(f) > 0:
89 vid_url = f[0]
90 formats.append({
91 'url': vid_url,
92 'format': 'mp4',
93 'height': int(res),
94 })
95 self._sort_formats(formats)
96 uploader = self._og_search_property('nick-name', webpage, 'uploader', default=None)
97 return {
98 'id': video_id,
99 'title': title,
100 'uploader': uploader,
101 'formats': formats
102 }
103
104
105 class WeiboMobileIE(InfoExtractor):
106 _VALID_URL = r'https?://m\.weibo\.cn/status/(?P<id>[0-9]+)(\?.+)?'
107 _TEST = {
108 'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0',
109 'info_dict': {
110 'id': '4189191225395228',
111 'ext': 'mp4',
112 'title': '午睡当然是要甜甜蜜蜜的啦',
113 'uploader': '柴犬柴犬'
114 }
115 }
116
117 def _real_extract(self, url):
118 video_id = self._match_id(url)
119 # to get Referer url for genvisitor
120 webpage, _ = self._download_webpage_handle(url, video_id, note="visit the page")
121 js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags=re.DOTALL)
122 weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json)
123 page_info = weibo_info['status']['page_info']
124 title = weibo_info.get('status').get('status_title')
125 uploader = weibo_info.get('status').get('user').get('screen_name')
126
127 return {
128 'id': video_id,
129 'title': title,
130 'uploader': uploader,
131 'url': page_info['media_info']['stream_url']
132 }