Clarify that download rate is in bytes per second
[youtube-dl] / youtube_dl / extractor / dailymotion.py
1 import re
2 import json
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_request,
7
8     ExtractorError,
9 )
10
11 class DailymotionIE(InfoExtractor):
12     """Information Extractor for Dailymotion"""
13
14     _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
15     IE_NAME = u'dailymotion'
16     _TEST = {
17         u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
18         u'file': u'x33vw9.mp4',
19         u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
20         u'info_dict': {
21             u"uploader": u"Alex and Van .", 
22             u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
23         }
24     }
25
26     def _real_extract(self, url):
27         # Extract id and simplified title from URL
28         mobj = re.match(self._VALID_URL, url)
29
30         video_id = mobj.group(1).split('_')[0].split('?')[0]
31
32         video_extension = 'mp4'
33
34         # Retrieve video webpage to extract further information
35         request = compat_urllib_request.Request(url)
36         request.add_header('Cookie', 'family_filter=off')
37         webpage = self._download_webpage(request, video_id)
38
39         # Extract URL, uploader and title from webpage
40         self.report_extraction(video_id)
41
42         video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
43                                              # Looking for official user
44                                              r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
45                                             webpage, 'video uploader')
46
47         video_upload_date = None
48         mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
49         if mobj is not None:
50             video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
51
52         embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
53         embed_page = self._download_webpage(embed_url, video_id,
54                                             u'Downloading embed page')
55         info = self._search_regex(r'var info = ({.*?}),', embed_page, 'video info')
56         info = json.loads(info)
57
58         # TODO: support choosing qualities
59
60         for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
61                     'stream_h264_hq_url','stream_h264_url',
62                     'stream_h264_ld_url']:
63             if info.get(key):#key in info and info[key]:
64                 max_quality = key
65                 self.to_screen(u'Using %s' % key)
66                 break
67         else:
68             raise ExtractorError(u'Unable to extract video URL')
69         video_url = info[max_quality]
70
71         return [{
72             'id':       video_id,
73             'url':      video_url,
74             'uploader': video_uploader,
75             'upload_date':  video_upload_date,
76             'title':    self._og_search_title(webpage),
77             'ext':      video_extension,
78             'thumbnail': info['thumbnail_url']
79         }]