]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/baidu.py
Imported Upstream version 2015.11.27.1
[youtubedl] / youtube_dl / extractor / baidu.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urlparse
8
9
10 class BaiduVideoIE(InfoExtractor):
11 IE_DESC = '百度视频'
12 _VALID_URL = r'http://v\.baidu\.com/(?P<type>[a-z]+)/(?P<id>\d+)\.htm'
13 _TESTS = [{
14 'url': 'http://v.baidu.com/comic/1069.htm?frp=bdbrand&q=%E4%B8%AD%E5%8D%8E%E5%B0%8F%E5%BD%93%E5%AE%B6',
15 'info_dict': {
16 'id': '1069',
17 'title': '中华小当家 TV版 (全52集)',
18 'description': 'md5:395a419e41215e531c857bb037bbaf80',
19 },
20 'playlist_count': 52,
21 }, {
22 'url': 'http://v.baidu.com/show/11595.htm?frp=bdbrand',
23 'info_dict': {
24 'id': '11595',
25 'title': 're:^奔跑吧兄弟',
26 'description': 'md5:1bf88bad6d850930f542d51547c089b8',
27 },
28 'playlist_mincount': 3,
29 }]
30
31 def _real_extract(self, url):
32 mobj = re.match(self._VALID_URL, url)
33 playlist_id = mobj.group('id')
34 category = category2 = mobj.group('type')
35 if category == 'show':
36 category2 = 'tvshow'
37
38 webpage = self._download_webpage(url, playlist_id)
39
40 playlist_title = self._html_search_regex(
41 r'title\s*:\s*(["\'])(?P<title>[^\']+)\1', webpage,
42 'playlist title', group='title')
43 playlist_description = self._html_search_regex(
44 r'<input[^>]+class="j-data-intro"[^>]+value="([^"]+)"/>', webpage,
45 playlist_id, 'playlist description')
46
47 site = self._html_search_regex(
48 r'filterSite\s*:\s*["\']([^"]*)["\']', webpage,
49 'primary provider site')
50 api_result = self._download_json(
51 'http://v.baidu.com/%s_intro/?dtype=%sPlayUrl&id=%s&site=%s' % (
52 category, category2, playlist_id, site),
53 playlist_id, 'Get playlist links')
54
55 entries = []
56 for episode in api_result[0]['episodes']:
57 episode_id = '%s_%s' % (playlist_id, episode['episode'])
58
59 redirect_page = self._download_webpage(
60 compat_urlparse.urljoin(url, episode['url']), episode_id,
61 note='Download Baidu redirect page')
62 real_url = self._html_search_regex(
63 r'location\.replace\("([^"]+)"\)', redirect_page, 'real URL')
64
65 entries.append(self.url_result(
66 real_url, video_title=episode['single_title']))
67
68 return self.playlist_result(
69 entries, playlist_id, playlist_title, playlist_description)