[generic] If the url doesn't specify the protocol, then try to extract prepending...
[youtube-dl] / youtube_dl / extractor / dailymotion.py
1 import re
2 import json
3 import itertools
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urllib_request,
8     get_element_by_attribute,
9     get_element_by_id,
10
11     ExtractorError,
12 )
13
14 class DailymotionIE(InfoExtractor):
15     """Information Extractor for Dailymotion"""
16
17     _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)'
18     IE_NAME = u'dailymotion'
19     _TEST = {
20         u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
21         u'file': u'x33vw9.mp4',
22         u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
23         u'info_dict': {
24             u"uploader": u"Amphora Alex and Van .", 
25             u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
26         }
27     }
28
29     def _real_extract(self, url):
30         # Extract id and simplified title from URL
31         mobj = re.match(self._VALID_URL, url)
32
33         video_id = mobj.group(1).split('_')[0].split('?')[0]
34
35         video_extension = 'mp4'
36         url = 'http://www.dailymotion.com/video/%s' % video_id
37
38         # Retrieve video webpage to extract further information
39         request = compat_urllib_request.Request(url)
40         request.add_header('Cookie', 'family_filter=off')
41         webpage = self._download_webpage(request, video_id)
42
43         # Extract URL, uploader and title from webpage
44         self.report_extraction(video_id)
45
46         video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
47                                              # Looking for official user
48                                              r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
49                                             webpage, 'video uploader')
50
51         video_upload_date = None
52         mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
53         if mobj is not None:
54             video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
55
56         embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
57         embed_page = self._download_webpage(embed_url, video_id,
58                                             u'Downloading embed page')
59         info = self._search_regex(r'var info = ({.*?}),$', embed_page,
60             'video info', flags=re.MULTILINE)
61         info = json.loads(info)
62
63         # TODO: support choosing qualities
64
65         for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
66                     'stream_h264_hq_url','stream_h264_url',
67                     'stream_h264_ld_url']:
68             if info.get(key):#key in info and info[key]:
69                 max_quality = key
70                 self.to_screen(u'Using %s' % key)
71                 break
72         else:
73             raise ExtractorError(u'Unable to extract video URL')
74         video_url = info[max_quality]
75
76         return [{
77             'id':       video_id,
78             'url':      video_url,
79             'uploader': video_uploader,
80             'upload_date':  video_upload_date,
81             'title':    self._og_search_title(webpage),
82             'ext':      video_extension,
83             'thumbnail': info['thumbnail_url']
84         }]
85
86
87 class DailymotionPlaylistIE(InfoExtractor):
88     _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
89     _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
90
91     def _real_extract(self, url):
92         mobj = re.match(self._VALID_URL, url)
93         playlist_id =  mobj.group('id')
94         video_ids = []
95
96         for pagenum in itertools.count(1):
97             webpage = self._download_webpage('https://www.dailymotion.com/playlist/%s/%s' % (playlist_id, pagenum),
98                                              playlist_id, u'Downloading page %s' % pagenum)
99
100             playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
101             video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
102
103             if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
104                 break
105
106         entries = [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
107                    for video_id in video_ids]
108         return {'_type': 'playlist',
109                 'id': playlist_id,
110                 'title': get_element_by_id(u'playlist_name', webpage),
111                 'entries': entries,
112                 }