]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/weibo.py
replace urlencode.encode with urlencode_postdata
[yt-dlp.git] / youtube_dl / extractor / weibo.py
CommitLineData
29ac31af 1# coding: utf-8
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
5
29ac31af 6import json
42a1012c 7import random
447a5a71 8import re
9
d2be5bb5 10from ..compat import (
42a1012c 11 compat_urlparse,
d2be5bb5 12)
447a5a71 13from ..utils import (
14 js_to_json,
5c97ec5f 15 urlencode_postdata,
447a5a71 16)
29ac31af 17
95104372 18
29ac31af 19class WeiboIE(InfoExtractor):
20 _VALID_URL = r'https?://weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)'
21 _TEST = {
95104372 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 }
29ac31af 29
30 def _real_extract(self, url):
31 video_id = self._match_id(url)
29ac31af 32 # to get Referer url for genvisitor
42a1012c 33 webpage, urlh = self._download_webpage_handle(url, video_id, note="first visit the page")
29ac31af 34
35 visitor_url = urlh.geturl()
42a1012c 36 headers = {
37 'Referer': visitor_url
38 }
29ac31af 39
42a1012c 40 fp = {
41 "os": "2",
42 "browser": "Gecko57,0,0,0",
43 "fonts": "undefined",
44 "screenInfo": "1440*900*24",
45 "plugins": ""
46 }
5c97ec5f 47 data = urlencode_postdata({
29ac31af 48 "cb": "gen_callback",
42a1012c 49 "fp": json.dumps(fp),
5c97ec5f 50 })
29ac31af 51
42a1012c 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")
29ac31af 54
95104372 55 p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
29ac31af 56 i1 = p.find('{')
57 i2 = p.rfind('}')
95104372 58 j = p[i1:i2 + 1] # get JSON object
29ac31af 59 d = json.loads(j)
60 tid = d["data"]["tid"]
61 cnfd = "%03d" % d["data"]["confidence"]
62
42a1012c 63 query = {
29ac31af 64 'a': 'incarnate',
65 't': tid,
66 'w': 2,
67 'c': cnfd,
68 'cb': 'cross_domain',
69 'from': 'weibo',
42a1012c 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)
29ac31af 74
42a1012c 75 webpage, _ = self._download_webpage_handle(url, video_id, note="retry to visit the page")
29ac31af 76
29ac31af 77 title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
78
25936512 79 video_sources_text = self._search_regex(r'video-sources=\\\"(.+?)\"', webpage, 'video_sources')
95104372 80
42a1012c 81 video_formats = compat_urlparse.parse_qs(video_sources_text)
3281af34 82
83 formats = []
42a1012c 84 supported_resolutions = ('720', '480')
3281af34 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]
3281af34 90 formats.append({
0c699588 91 'url': vid_url,
92 'format': 'mp4',
93 'height': int(res),
95104372 94 })
3281af34 95 self._sort_formats(formats)
95104372 96 uploader = self._og_search_property('nick-name', webpage, 'uploader', default=None)
29ac31af 97 return {
95104372 98 'id': video_id,
99 'title': title,
100 'uploader': uploader,
101 'formats': formats
95104372 102 }
103
447a5a71 104
105class WeiboMobileIE(InfoExtractor):
42a1012c 106 _VALID_URL = r'https?://m\.weibo\.cn/status/(?P<id>[0-9]+)(\?.+)?'
447a5a71 107 _TEST = {
95104372 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 }
447a5a71 116
117 def _real_extract(self, url):
118 video_id = self._match_id(url)
447a5a71 119 # to get Referer url for genvisitor
42a1012c 120 webpage, _ = self._download_webpage_handle(url, video_id, note="visit the page")
95104372 121 js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags=re.DOTALL)
447a5a71 122 weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json)
123 page_info = weibo_info['status']['page_info']
42a1012c 124 title = weibo_info.get('status').get('status_title')
125 uploader = weibo_info.get('status').get('user').get('screen_name')
447a5a71 126
127 return {
95104372 128 'id': video_id,
129 'title': title,
130 'uploader': uploader,
42a1012c 131 'url': page_info['media_info']['stream_url']
95104372 132 }