Merge branch 'bleacherreport' of github.com:remitamine/youtube-dl into remitamine...
[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 )
10
11
12 class GoogleDriveIE(InfoExtractor):
13     _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})'
14     _TEST = {
15         'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
16         'md5': '881f7700aec4f538571fa1e0eed4a7b6',
17         'info_dict': {
18             'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
19             'ext': 'mp4',
20             'title': 'Big Buck Bunny.mp4',
21             'duration': 46,
22         }
23     }
24     _FORMATS_EXT = {
25         '5': 'flv',
26         '6': 'flv',
27         '13': '3gp',
28         '17': '3gp',
29         '18': 'mp4',
30         '22': 'mp4',
31         '34': 'flv',
32         '35': 'flv',
33         '36': '3gp',
34         '37': 'mp4',
35         '38': 'mp4',
36         '43': 'webm',
37         '44': 'webm',
38         '45': 'webm',
39         '46': 'webm',
40         '59': 'mp4',
41     }
42
43     @staticmethod
44     def _extract_url(webpage):
45         mobj = re.search(
46             r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28})',
47             webpage)
48         if mobj:
49             return 'https://drive.google.com/file/d/%s' % mobj.group('id')
50
51     def _real_extract(self, url):
52         video_id = self._match_id(url)
53         webpage = self._download_webpage(
54             'http://docs.google.com/file/d/%s' % video_id, video_id, encoding='unicode_escape')
55
56         reason = self._search_regex(r'"reason"\s*,\s*"([^"]+)', webpage, 'reason', default=None)
57         if reason:
58             raise ExtractorError(reason)
59
60         title = self._search_regex(r'"title"\s*,\s*"([^"]+)', webpage, 'title')
61         duration = int_or_none(self._search_regex(
62             r'"length_seconds"\s*,\s*"([^"]+)', webpage, 'length seconds', default=None))
63         fmt_stream_map = self._search_regex(
64             r'"fmt_stream_map"\s*,\s*"([^"]+)', webpage, 'fmt stream map').split(',')
65         fmt_list = self._search_regex(r'"fmt_list"\s*,\s*"([^"]+)', webpage, 'fmt_list').split(',')
66
67         formats = []
68         for fmt, fmt_stream in zip(fmt_list, fmt_stream_map):
69             fmt_id, fmt_url = fmt_stream.split('|')
70             resolution = fmt.split('/')[1]
71             width, height = resolution.split('x')
72             formats.append({
73                 'url': fmt_url,
74                 'format_id': fmt_id,
75                 'resolution': resolution,
76                 'width': int_or_none(width),
77                 'height': int_or_none(height),
78                 'ext': self._FORMATS_EXT[fmt_id],
79             })
80         self._sort_formats(formats)
81
82         return {
83             'id': video_id,
84             'title': title,
85             'thumbnail': self._og_search_thumbnail(webpage),
86             'duration': duration,
87             'formats': formats,
88         }