- data = urlencode({
- "cb": "gen_callback",
- "fp": '{"os":"2","browser":"Gecko57,0,0,0","fonts":"undefined","screenInfo":"1440*900*24","plugins":""}',
- }).encode()
- headers = {
- 'Accept-Encoding': 'gzip, deflate, br',
- 'Accept': '*/*',
- 'Referer': visitor_url,
- }
-
- r_genvisitor = Request(
- 'https://passport.weibo.com/visitor/genvisitor',
- data=data,
- headers=headers,
- method='POST'
- )
- webpage, urlh = self._download_webpage_handle(r_genvisitor, video_id, note="gen visitor")
-
- p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
- i1 = p.find('{')
- i2 = p.rfind('}')
- j = p[i1:i2 + 1] # get JSON object
- d = json.loads(j)
- tid = d["data"]["tid"]
- cnfd = "%03d" % d["data"]["confidence"]
-
- param = urlencode({
- 'a': 'incarnate',
- 't': tid,
- 'w': 2,
- 'c': cnfd,
- 'cb': 'cross_domain',
- 'from': 'weibo',
- '_rand': rnd.random()
- })
- gencallback_url = "https://passport.weibo.com/visitor/visitor?" + param
- webpage, urlh = self._download_webpage_handle(gencallback_url, video_id, note="gen callback")
-
- webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers, note="retry to visit the page")
-
- # TODO more code goes here, for example ...
- title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
-
- video_sources_text = self._search_regex("video-sources=\\\\\"(.+?)\"", webpage, 'video_sources')
-
- video_formats = parse.parse_qs(video_sources_text)
+ if 'passport.weibo.com' in visitor_url:
+ # first visit
+ visitor_data = self._download_json(
+ 'https://passport.weibo.com/visitor/genvisitor', video_id,
+ note='Generating first-visit data',
+ transform_source=strip_jsonp,
+ headers={'Referer': visitor_url},
+ data=urlencode_postdata({
+ 'cb': 'gen_callback',
+ 'fp': json.dumps({
+ 'os': '2',
+ 'browser': 'Gecko57,0,0,0',
+ 'fonts': 'undefined',
+ 'screenInfo': '1440*900*24',
+ 'plugins': '',
+ }),
+ }))
+
+ tid = visitor_data['data']['tid']
+ cnfd = '%03d' % visitor_data['data']['confidence']
+
+ self._download_webpage(
+ 'https://passport.weibo.com/visitor/visitor', video_id,
+ note='Running first-visit callback',
+ query={
+ 'a': 'incarnate',
+ 't': tid,
+ 'w': 2,
+ 'c': cnfd,
+ 'cb': 'cross_domain',
+ 'from': 'weibo',
+ '_rand': random.random(),
+ })
+
+ webpage = self._download_webpage(
+ url, video_id, note='Revisiting webpage')
+
+ title = self._html_search_regex(
+ r'<title>(.+?)</title>', webpage, 'title')
+
+ video_formats = compat_parse_qs(self._search_regex(
+ r'video-sources=\\\"(.+?)\"', webpage, 'video_sources'))