Added support for Discovery Issue #2227
[youtube-dl] / youtube_dl / extractor / discovery.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5 from .common import InfoExtractor
6
7
8 class DiscoveryIE(InfoExtractor):
9     _VALID_URL = r'http://dsc\.discovery\.com\/[a-zA-Z0-9\-]*/[a-zA-Z0-9\-]*/videos/(?P<id>[a-zA-Z0-9\-]*)(.htm)?'
10     _TEST = {
11         'url': 'http://dsc.discovery.com/tv-shows/mythbusters/videos/mission-impossible-outtakes.htm',
12         'file': 'mission-impossible-outtakes.mp4',
13         'md5': 'e12614f9ee303a6ccef415cb0793eba2',
14         'info_dict': {
15             'title': 'MythBusters: Mission Impossible Outtakes'
16         }
17     }
18
19     def _real_extract(self, url):
20         mobj = re.match(self._VALID_URL, url)
21         video_id = mobj.group('id')
22         webpage = self._download_webpage(url, video_id)
23         title = self._search_regex(
24             r'(?<=\"name\": ")(?P<title>.*?)(?=\"\,)', webpage, r'Filename')
25         duration = int(self._search_regex(
26             r'(?<=\"duration\"\: )(?P<duration>.*?)(?=,)', webpage, r'Duration'))
27         formats_raw = self._search_regex(
28             r'(?<=\"mp4\":)(.*?)(}])', webpage, r'formats') + '}]'
29         formats_json = json.loads(formats_raw)
30         formats = []
31         for f in formats_json:
32             formats.append(
33                 {'url': f['src'], r'ext': r'mp4', 'tbr': int(f['bitrate'][:-1])})
34
35         return {
36             'id': video_id,
37             'duration': duration,
38             'title': title,
39             'formats': formats
40         }