[googledrive] Fix formats' sorting (closes #13443)
[youtube-dl] / youtube_dl / extractor / googledrive.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8     int_or_none,
9     lowercase_escape,
10 )
11
12
13 class GoogleDriveIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:(?:docs|drive)\.google\.com/(?:uc\?.*?id=|file/d/)|video\.google\.com/get_player\?.*?docid=)(?P<id>[a-zA-Z0-9_-]{28,})'
15     _TESTS = [{
16         'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
17         'md5': 'd109872761f7e7ecf353fa108c0dbe1e',
18         'info_dict': {
19             'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
20             'ext': 'mp4',
21             'title': 'Big Buck Bunny.mp4',
22             'duration': 45,
23         }
24     }, {
25         # video id is longer than 28 characters
26         'url': 'https://drive.google.com/file/d/1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ/edit',
27         'only_matching': True,
28     }]
29     _FORMATS_EXT = {
30         '5': 'flv',
31         '6': 'flv',
32         '13': '3gp',
33         '17': '3gp',
34         '18': 'mp4',
35         '22': 'mp4',
36         '34': 'flv',
37         '35': 'flv',
38         '36': '3gp',
39         '37': 'mp4',
40         '38': 'mp4',
41         '43': 'webm',
42         '44': 'webm',
43         '45': 'webm',
44         '46': 'webm',
45         '59': 'mp4',
46     }
47
48     @staticmethod
49     def _extract_url(webpage):
50         mobj = re.search(
51             r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28,})',
52             webpage)
53         if mobj:
54             return 'https://drive.google.com/file/d/%s' % mobj.group('id')
55
56     def _real_extract(self, url):
57         video_id = self._match_id(url)
58         webpage = self._download_webpage(
59             'http://docs.google.com/file/d/%s' % video_id, video_id)
60
61         reason = self._search_regex(r'"reason"\s*,\s*"([^"]+)', webpage, 'reason', default=None)
62         if reason:
63             raise ExtractorError(reason)
64
65         title = self._search_regex(r'"title"\s*,\s*"([^"]+)', webpage, 'title')
66         duration = int_or_none(self._search_regex(
67             r'"length_seconds"\s*,\s*"([^"]+)', webpage, 'length seconds', default=None))
68         fmt_stream_map = self._search_regex(
69             r'"fmt_stream_map"\s*,\s*"([^"]+)', webpage, 'fmt stream map').split(',')
70         fmt_list = self._search_regex(r'"fmt_list"\s*,\s*"([^"]+)', webpage, 'fmt_list').split(',')
71
72         resolutions = {}
73         for fmt in fmt_list:
74             mobj = re.search(
75                 r'^(?P<format_id>\d+)/(?P<width>\d+)[xX](?P<height>\d+)', fmt)
76             if mobj:
77                 resolutions[mobj.group('format_id')] = (
78                     int(mobj.group('width')), int(mobj.group('height')))
79
80         formats = []
81         for fmt_stream in fmt_stream_map:
82             fmt_stream_split = fmt_stream.split('|')
83             if len(fmt_stream_split) < 2:
84                 continue
85             format_id, format_url = fmt_stream_split[:2]
86             f = {
87                 'url': lowercase_escape(format_url),
88                 'format_id': format_id,
89                 'ext': self._FORMATS_EXT[format_id],
90             }
91             resolution = resolutions.get(format_id)
92             if resolution:
93                 f.update({
94                     'width': resolution[0],
95                     'height': resolution[0],
96                 })
97             formats.append(f)
98         self._sort_formats(formats)
99
100         return {
101             'id': video_id,
102             'title': title,
103             'thumbnail': self._og_search_thumbnail(webpage, default=None),
104             'duration': duration,
105             'formats': formats,
106         }