]>
Commit | Line | Data |
---|---|---|
29ac31af | 1 | from .common import InfoExtractor |
2 | ||
29ac31af | 3 | import json |
42a1012c | 4 | import random |
447a5a71 | 5 | import re |
6 | ||
d2be5bb5 | 7 | from ..compat import ( |
5eca00a2 YCH |
8 | compat_parse_qs, |
9 | compat_str, | |
d2be5bb5 | 10 | ) |
447a5a71 | 11 | from ..utils import ( |
12 | js_to_json, | |
6a41a12d | 13 | strip_jsonp, |
5c97ec5f | 14 | urlencode_postdata, |
447a5a71 | 15 | ) |
29ac31af | 16 | |
95104372 | 17 | |
29ac31af | 18 | class WeiboIE(InfoExtractor): |
93bb6b1b | 19 | _VALID_URL = r'https?://(?:www\.)?weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)' |
29ac31af | 20 | _TEST = { |
95104372 | 21 | 'url': 'https://weibo.com/6275294458/Fp6RGfbff?type=comment', |
22 | 'info_dict': { | |
23 | 'id': 'Fp6RGfbff', | |
24 | 'ext': 'mp4', | |
25 | 'title': 'You should have servants to massage you,... 来自Hosico_猫 - 微博', | |
26 | } | |
27 | } | |
29ac31af | 28 | |
29 | def _real_extract(self, url): | |
30 | video_id = self._match_id(url) | |
29ac31af | 31 | # to get Referer url for genvisitor |
5eca00a2 | 32 | webpage, urlh = self._download_webpage_handle(url, video_id) |
29ac31af | 33 | |
34 | visitor_url = urlh.geturl() | |
35 | ||
5eca00a2 YCH |
36 | if 'passport.weibo.com' in visitor_url: |
37 | # first visit | |
38 | visitor_data = self._download_json( | |
39 | 'https://passport.weibo.com/visitor/genvisitor', video_id, | |
40 | note='Generating first-visit data', | |
41 | transform_source=strip_jsonp, | |
42 | headers={'Referer': visitor_url}, | |
43 | data=urlencode_postdata({ | |
44 | 'cb': 'gen_callback', | |
45 | 'fp': json.dumps({ | |
46 | 'os': '2', | |
47 | 'browser': 'Gecko57,0,0,0', | |
48 | 'fonts': 'undefined', | |
49 | 'screenInfo': '1440*900*24', | |
50 | 'plugins': '', | |
51 | }), | |
52 | })) | |
53 | ||
54 | tid = visitor_data['data']['tid'] | |
55 | cnfd = '%03d' % visitor_data['data']['confidence'] | |
56 | ||
57 | self._download_webpage( | |
58 | 'https://passport.weibo.com/visitor/visitor', video_id, | |
59 | note='Running first-visit callback', | |
60 | query={ | |
61 | 'a': 'incarnate', | |
62 | 't': tid, | |
63 | 'w': 2, | |
64 | 'c': cnfd, | |
65 | 'cb': 'cross_domain', | |
66 | 'from': 'weibo', | |
67 | '_rand': random.random(), | |
68 | }) | |
69 | ||
70 | webpage = self._download_webpage( | |
71 | url, video_id, note='Revisiting webpage') | |
72 | ||
04f3fd2c | 73 | title = self._html_extract_title(webpage) |
5eca00a2 YCH |
74 | |
75 | video_formats = compat_parse_qs(self._search_regex( | |
76 | r'video-sources=\\\"(.+?)\"', webpage, 'video_sources')) | |
29ac31af | 77 | |
5eca00a2 YCH |
78 | formats = [] |
79 | supported_resolutions = (480, 720) | |
80 | for res in supported_resolutions: | |
81 | vid_urls = video_formats.get(compat_str(res)) | |
82 | if not vid_urls or not isinstance(vid_urls, list): | |
83 | continue | |
29ac31af | 84 | |
5eca00a2 YCH |
85 | vid_url = vid_urls[0] |
86 | formats.append({ | |
87 | 'url': vid_url, | |
88 | 'height': res, | |
89 | }) | |
29ac31af | 90 | |
5eca00a2 | 91 | self._sort_formats(formats) |
95104372 | 92 | |
5eca00a2 YCH |
93 | uploader = self._og_search_property( |
94 | 'nick-name', webpage, 'uploader', default=None) | |
3281af34 | 95 | |
29ac31af | 96 | return { |
95104372 | 97 | 'id': video_id, |
98 | 'title': title, | |
99 | 'uploader': uploader, | |
100 | 'formats': formats | |
95104372 | 101 | } |
102 | ||
447a5a71 | 103 | |
104 | class WeiboMobileIE(InfoExtractor): | |
42a1012c | 105 | _VALID_URL = r'https?://m\.weibo\.cn/status/(?P<id>[0-9]+)(\?.+)?' |
447a5a71 | 106 | _TEST = { |
95104372 | 107 | 'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0', |
108 | 'info_dict': { | |
109 | 'id': '4189191225395228', | |
110 | 'ext': 'mp4', | |
111 | 'title': '午睡当然是要甜甜蜜蜜的啦', | |
112 | 'uploader': '柴犬柴犬' | |
113 | } | |
114 | } | |
447a5a71 | 115 | |
116 | def _real_extract(self, url): | |
117 | video_id = self._match_id(url) | |
447a5a71 | 118 | # to get Referer url for genvisitor |
5eca00a2 YCH |
119 | webpage = self._download_webpage(url, video_id, note='visit the page') |
120 | ||
121 | weibo_info = self._parse_json(self._search_regex( | |
122 | r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\]\s*\|\|\s*{};', | |
123 | webpage, 'js_code', flags=re.DOTALL), | |
124 | video_id, transform_source=js_to_json) | |
125 | ||
126 | status_data = weibo_info.get('status', {}) | |
127 | page_info = status_data.get('page_info') | |
128 | title = status_data['status_title'] | |
129 | uploader = status_data.get('user', {}).get('screen_name') | |
447a5a71 | 130 | |
131 | return { | |
95104372 | 132 | 'id': video_id, |
133 | 'title': title, | |
134 | 'uploader': uploader, | |
42a1012c | 135 | 'url': page_info['media_info']['stream_url'] |
95104372 | 136 | } |