]> git.bitcoin.ninja Git - youtube-dl/commitdiff
[bilibili] add support for specefic page extraction
authorremitamine <remitamine@gmail.com>
Sat, 17 Oct 2015 22:12:58 +0000 (23:12 +0100)
committerremitamine <remitamine@gmail.com>
Sat, 17 Oct 2015 22:12:58 +0000 (23:12 +0100)
youtube_dl/extractor/bilibili.py

index 85156ce49cca1b99f9e534f48c270f5e34621339..e00efb6eb0a1e4671f900e1e9bb4b0da144609aa 100644 (file)
@@ -1,18 +1,20 @@
 # coding: utf-8
 from __future__ import unicode_literals
 
+import re
 import json
 import xml.etree.ElementTree as ET
 
 from .common import InfoExtractor
 from ..utils import (
     int_or_none,
+    unescapeHTML,
     ExtractorError,
 )
 
 
 class BiliBiliIE(InfoExtractor):
-    _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>[0-9]+)/'
+    _VALID_URL = r'http://www\.bilibili\.(?:tv|com)/video/av(?P<id>\d+)(?:/index_(?P<page_num>\d+).html)?'
 
     _TESTS = [{
         'url': 'http://www.bilibili.tv/video/av1074402/',
@@ -33,17 +35,31 @@ class BiliBiliIE(InfoExtractor):
         'info_dict': {
             'id': '1041170',
             'title': '【BD1080P】刀语【诸神&异域】',
+            'description': '这是个神奇的故事~每个人不留弹幕不给走哦~切利哦!~',
+            'uploader': '枫叶逝去',
+            'timestamp': 1396501299,
         },
-        'playlist_count': 12,
+        'playlist_count': 9,
     }]
 
-    def _extract_video_info(self, cid, view_data, page_num=1, num_pages=1):
-        title = view_data['title']
+    def _real_extract(self, url):
+        mobj = re.match(self._VALID_URL, url)
+        video_id = mobj.group('id')
+        page_num = mobj.group('page_num') or '1'
+
+        view_data = self._download_json(
+            'http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s&page=%s' % (video_id, page_num),
+            video_id)
+        if 'error' in view_data:
+            raise ExtractorError('%s said: %s' % (self.IE_NAME, view_data['error']), expected=True)
+
+        cid = view_data['cid']
+        title = unescapeHTML(view_data['title'])
 
         page = self._download_webpage(
             'http://interface.bilibili.com/v_cdn_play?appkey=8e9fc618fbd41e28&cid=%s' % cid,
             cid,
-            'Downloading page %d/%d' % (page_num, num_pages)
+            'Downloading page %s/%s' % (page_num, view_data['pages'])
         )
         try:
             err_info = json.loads(page)
@@ -76,7 +92,7 @@ class BiliBiliIE(InfoExtractor):
             })
 
         info = {
-            'id': cid,
+            'id': str(cid),
             'title': title,
             'description': view_data.get('description'),
             'thumbnail': view_data.get('pic'),
@@ -92,22 +108,7 @@ class BiliBiliIE(InfoExtractor):
         else:
             info.update({
                 '_type': 'multi_video',
+                'id': video_id,
                 'entries': entries,
             })
             return info
-
-    def _real_extract(self, url):
-        video_id = self._match_id(url)
-        view_data = self._download_json('http://api.bilibili.com/view?type=json&appkey=8e9fc618fbd41e28&id=%s' % video_id, video_id)
-
-        num_pages = int_or_none(view_data['pages'])
-        if num_pages > 1:
-            play_list_title = view_data['title']
-            page_list = self._download_json('http://www.bilibili.com/widget/getPageList?aid=%s' % video_id, video_id, 'Downloading page list metadata')
-            entries = []
-            for page in page_list:
-                view_data['title'] = page['pagename']
-                entries.append(self._extract_video_info(str(page['cid']), view_data, page['page'], num_pages))
-            return self.playlist_result(entries, video_id, play_list_title, view_data.get('description'))
-        else:
-            return self._extract_video_info(str(view_data['cid']), view_data)