added weibo mobile site support
[youtube-dl] / youtube_dl / extractor / weibo.py
index 195508e994577e11d0c8990fed596eefb8d934f6..eda0fa63ddf43bbb6036c6efc63afb8dbc704f4b 100644 (file)
@@ -5,24 +5,24 @@ from .common import InfoExtractor
 
 from urllib.request import Request
 from urllib.parse import urlencode
+from urllib import parse
 import json
 import random as rnd
+from os import path
+import re
+
+from ..utils import (
+    js_to_json,
+)
 
 class WeiboIE(InfoExtractor):
     _VALID_URL = r'https?://weibo\.com/[0-9]+/(?P<id>[a-zA-Z0-9]+)'
     _TEST = {
-            'url': 'https://weibo.com/6275294458/Fp6RGfbff?from=page_1005056275294458_profile&wvr=6&mod=weibotime&type=comment',
-            'md5': 'TODO: md5 sum of the first 10241 bytes of the video file (use --test)',
+            'url': 'https://weibo.com/6275294458/Fp6RGfbff?type=comment',
             'info_dict': {
-                'id': '42',
+                'id': 'Fp6RGfbff',
                 'ext': 'mp4',
-                'title': 'Video title goes here',
-                'thumbnail': r're:^https?://.*\.jpg$',
-                # TODO more properties, either as:
-                # * A value
-                # * MD5 checksum; start the string with md5:
-                # * A regular expression; start the string with re:
-                # * Any Python type (for example int or float)
+                'title': 'You should have servants to massage you,... 来自Hosico_猫 - 微博',
                 }
             }
 
@@ -57,7 +57,6 @@ class WeiboIE(InfoExtractor):
             method = 'POST'
             )
         webpage,urlh = self._download_webpage_handle(r_genvisitor, video_id, note="gen visitor")
-        print("webpage", webpage)
 
         p = webpage.split("&&")[1] # split "gen_callback && gen_callback(...)"
         i1 = p.find('{')
@@ -78,20 +77,75 @@ class WeiboIE(InfoExtractor):
             })
         gencallback_url = "https://passport.weibo.com/visitor/visitor?" + param
         webpage,urlh = self._download_webpage_handle(gencallback_url, video_id, note="gen callback")
-        print("webpage", webpage)
 
         webpage,urlh = self._download_webpage_handle(url, video_id, headers=headers, note="retry to visit the page")
-        print("webpage", webpage)
 
         # TODO more code goes here, for example ...
         title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
 
-        video_sources = self._search_regex(r'video-sources=(.+?)', webpage, 'video_sources')
-        print("video_sources:", video_sources)
+        video_sources_text = self._search_regex("video-sources=\\\\\"(.+?)\"", webpage, 'video_sources')
+        
+        video_formats = parse.parse_qs(video_sources_text)
+
+        formats = []
+        supported_resolutions = ['720', '480']
+        for res in supported_resolutions:
+            f = video_formats.get(res)
+            if isinstance(f, list):
+                if len(f) > 0:
+                    vid_url = f[0]
+                    formats.append({
+                        'url': vid_url,
+                        'format': 'mp4',
+                        'height': int(res),
+                        })
+        self._sort_formats(formats)
+        uploader = self._og_search_property('nick-name', webpage, 'uploader', default = None)
+        return {
+                'id': video_id,
+                'title': title,
+                'uploader': uploader,
+                'formats': formats
+                # TODO more properties (see youtube_dl/extractor/common.py)
+                }
+
+class WeiboMobileIE(InfoExtractor):
+    _VALID_URL = r'https?://m.weibo.cn/status/(?P<id>[0-9]+)(\?.+)?'
+    _TEST = {
+            'url': 'https://m.weibo.cn/status/4189191225395228?wm=3333_2001&sourcetype=weixin&featurecode=newtitle&from=singlemessage&isappinstalled=0',
+            'info_dict': {
+                'id': '4189191225395228',
+                'ext': 'mp4',
+                'title': '午睡当然是要甜甜蜜蜜的啦',
+                'uploader': '柴犬柴犬'
+                }
+            }
+
+    def _real_extract(self, url):
+        video_id = self._match_id(url)
+        headers = {
+            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
+            'Accept-Encoding': 'gzip, deflate, br',
+            'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
+            'Upgrade-Insecure-Requests': '1',
+        }
+        # to get Referer url for genvisitor
+        webpage,urlh = self._download_webpage_handle(url, video_id, headers=headers, note="visit the page")
+        js_code = self._search_regex(r'var\s+\$render_data\s*=\s*\[({.*})\]\[0\] \|\| {};', webpage, 'js_code', flags = re.DOTALL)
+        weibo_info = self._parse_json(js_code, video_id, transform_source=js_to_json)
+        page_info = weibo_info['status']['page_info']
+        title = weibo_info['status']['status_title']
+        format = {
+            'url': page_info['media_info']['stream_url'],
+            'format': 'mp4', 
+                  }
+        formats = [format]
+        uploader = weibo_info['status']['user']['screen_name']
+
         return {
                 'id': video_id,
                 'title': title,
-                'description': self._og_search_description(webpage),
-                'uploader': self._search_regex(r'<div[^>]+id="uploader"[^>]*>([^<]+)<', webpage, 'uploader', fatal=False),
+                'uploader': uploader,
+                'formats': formats
                 # TODO more properties (see youtube_dl/extractor/common.py)
                 }