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