[giantbomb] Extend _VALID_URL (#25222)
[youtube-dl] / youtube_dl / extractor / giantbomb.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     int_or_none,
10     qualities,
11     unescapeHTML,
12 )
13
14
15 class GiantBombIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?giantbomb\.com/(?:videos|shows)/(?P<display_id>[^/]+)/(?P<id>\d+-\d+)'
17     _TESTS = [{
18         'url': 'http://www.giantbomb.com/videos/quick-look-destiny-the-dark-below/2300-9782/',
19         'md5': '132f5a803e7e0ab0e274d84bda1e77ae',
20         'info_dict': {
21             'id': '2300-9782',
22             'display_id': 'quick-look-destiny-the-dark-below',
23             'ext': 'mp4',
24             'title': 'Quick Look: Destiny: The Dark Below',
25             'description': 'md5:0aa3aaf2772a41b91d44c63f30dfad24',
26             'duration': 2399,
27             'thumbnail': r're:^https?://.*\.jpg$',
28         }
29     }, {
30         'url': 'https://www.giantbomb.com/shows/ben-stranding/2970-20212',
31         'only_matching': True,
32     }]
33
34     def _real_extract(self, url):
35         mobj = re.match(self._VALID_URL, url)
36         video_id = mobj.group('id')
37         display_id = mobj.group('display_id')
38
39         webpage = self._download_webpage(url, display_id)
40
41         title = self._og_search_title(webpage)
42         description = self._og_search_description(webpage)
43         thumbnail = self._og_search_thumbnail(webpage)
44
45         video = json.loads(unescapeHTML(self._search_regex(
46             r'data-video="([^"]+)"', webpage, 'data-video')))
47
48         duration = int_or_none(video.get('lengthSeconds'))
49
50         quality = qualities([
51             'f4m_low', 'progressive_low', 'f4m_high',
52             'progressive_high', 'f4m_hd', 'progressive_hd'])
53
54         formats = []
55         for format_id, video_url in video['videoStreams'].items():
56             if format_id == 'f4m_stream':
57                 continue
58             ext = determine_ext(video_url)
59             if ext == 'f4m':
60                 f4m_formats = self._extract_f4m_formats(video_url + '?hdcore=3.3.1', display_id)
61                 if f4m_formats:
62                     f4m_formats[0]['quality'] = quality(format_id)
63                     formats.extend(f4m_formats)
64             elif ext == 'm3u8':
65                 formats.extend(self._extract_m3u8_formats(
66                     video_url, display_id, ext='mp4', entry_protocol='m3u8_native',
67                     m3u8_id='hls', fatal=False))
68             else:
69                 formats.append({
70                     'url': video_url,
71                     'format_id': format_id,
72                     'quality': quality(format_id),
73                 })
74
75         if not formats:
76             youtube_id = video.get('youtubeID')
77             if youtube_id:
78                 return self.url_result(youtube_id, 'Youtube')
79
80         self._sort_formats(formats)
81
82         return {
83             'id': video_id,
84             'display_id': display_id,
85             'title': title,
86             'description': description,
87             'thumbnail': thumbnail,
88             'duration': duration,
89             'formats': formats,
90         }