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