Move DailyMotion into its own file
[youtube-dl] / youtube_dl / extractor / dailymotion.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     compat_urllib_request,
6     compat_urllib_parse,
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
18     def _real_extract(self, url):
19         # Extract id and simplified title from URL
20         mobj = re.match(self._VALID_URL, url)
21
22         video_id = mobj.group(1).split('_')[0].split('?')[0]
23
24         video_extension = 'mp4'
25
26         # Retrieve video webpage to extract further information
27         request = compat_urllib_request.Request(url)
28         request.add_header('Cookie', 'family_filter=off')
29         webpage = self._download_webpage(request, video_id)
30
31         # Extract URL, uploader and title from webpage
32         self.report_extraction(video_id)
33         mobj = re.search(r'\s*var flashvars = (.*)', webpage)
34         if mobj is None:
35             raise ExtractorError(u'Unable to extract media URL')
36         flashvars = compat_urllib_parse.unquote(mobj.group(1))
37
38         for key in ['hd1080URL', 'hd720URL', 'hqURL', 'sdURL', 'ldURL', 'video_url']:
39             if key in flashvars:
40                 max_quality = key
41                 self.to_screen(u'Using %s' % key)
42                 break
43         else:
44             raise ExtractorError(u'Unable to extract video URL')
45
46         mobj = re.search(r'"' + max_quality + r'":"(.+?)"', flashvars)
47         if mobj is None:
48             raise ExtractorError(u'Unable to extract video URL')
49
50         video_url = compat_urllib_parse.unquote(mobj.group(1)).replace('\\/', '/')
51
52         # TODO: support choosing qualities
53
54         mobj = re.search(r'<meta property="og:title" content="(?P<title>[^"]*)" />', webpage)
55         if mobj is None:
56             raise ExtractorError(u'Unable to extract title')
57         video_title = unescapeHTML(mobj.group('title'))
58
59         video_uploader = None
60         video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
61                                              # Looking for official user
62                                              r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
63                                             webpage, 'video uploader')
64
65         video_upload_date = None
66         mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
67         if mobj is not None:
68             video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
69
70         return [{
71             'id':       video_id,
72             'url':      video_url,
73             'uploader': video_uploader,
74             'upload_date':  video_upload_date,
75             'title':    video_title,
76             'ext':      video_extension,
77         }]