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