5 from .common 
import InfoExtractor
 
   6 from .subtitles 
import SubtitlesInfoExtractor
 
  11     get_element_by_attribute
, 
  17 class DailymotionBaseInfoExtractor(InfoExtractor
): 
  19     def _build_request(url
): 
  20         """Build a request with the family filter disabled""" 
  21         request 
= compat_urllib_request
.Request(url
) 
  22         request
.add_header('Cookie', 'family_filter=off') 
  25 class DailymotionIE(DailymotionBaseInfoExtractor
, SubtitlesInfoExtractor
): 
  26     """Information Extractor for Dailymotion""" 
  28     _VALID_URL 
= r
'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)' 
  29     IE_NAME 
= u
'dailymotion' 
  31         u
'url': u
'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech', 
  32         u
'file': u
'x33vw9.mp4', 
  33         u
'md5': u
'392c4b85a60a90dc4792da41ce3144eb', 
  35             u
"uploader": u
"Amphora Alex and Van .",  
  36             u
"title": u
"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\"" 
  40     def _real_extract(self
, url
): 
  41         # Extract id and simplified title from URL 
  42         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  44         video_id 
= mobj
.group(1).split('_')[0].split('?')[0] 
  46         video_extension 
= 'mp4' 
  47         url 
= 'http://www.dailymotion.com/video/%s' % video_id
 
  49         # Retrieve video webpage to extract further information 
  50         request 
= self
._build
_request
(url
) 
  51         webpage 
= self
._download
_webpage
(request
, video_id
) 
  53         # Extract URL, uploader and title from webpage 
  54         self
.report_extraction(video_id
) 
  56         video_uploader 
= self
._search
_regex
([r
'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>', 
  57                                              # Looking for official user 
  58                                              r
'<(?:span|a) .*?rel="author".*?>([^<]+?)</'], 
  59                                             webpage
, 'video uploader') 
  61         video_upload_date 
= None 
  62         mobj 
= re
.search(r
'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage
) 
  64             video_upload_date 
= mobj
.group(3) + mobj
.group(2) + mobj
.group(1) 
  66         embed_url 
= 'http://www.dailymotion.com/embed/video/%s' % video_id
 
  67         embed_page 
= self
._download
_webpage
(embed_url
, video_id
, 
  68                                             u
'Downloading embed page') 
  69         info 
= self
._search
_regex
(r
'var info = ({.*?}),$', embed_page
, 
  70             'video info', flags
=re
.MULTILINE
) 
  71         info 
= json
.loads(info
) 
  72         if info
.get('error') is not None: 
  73             msg 
= 'Couldn\'t get video, Dailymotion says: %s' % info
['error']['title'] 
  74             raise ExtractorError(msg
, expected
=True) 
  76         # TODO: support choosing qualities 
  78         for key 
in ['stream_h264_hd1080_url','stream_h264_hd_url', 
  79                     'stream_h264_hq_url','stream_h264_url', 
  80                     'stream_h264_ld_url']: 
  81             if info
.get(key
):#key in info and info[key]: 
  83                 self
.to_screen(u
'Using %s' % key
) 
  86             raise ExtractorError(u
'Unable to extract video URL') 
  87         video_url 
= info
[max_quality
] 
  90         video_subtitles 
= self
.extract_subtitles(video_id
) 
  91         if self
._downloader
.params
.get('listsubtitles', False): 
  92             self
._list
_available
_subtitles
(video_id
) 
  98             'uploader': video_uploader
, 
  99             'upload_date':  video_upload_date
, 
 100             'title':    self
._og
_search
_title
(webpage
), 
 101             'ext':      video_extension
, 
 102             'subtitles':    video_subtitles
, 
 103             'thumbnail': info
['thumbnail_url'] 
 106     def _get_available_subtitles(self
, video_id
): 
 108             sub_list 
= self
._download
_webpage
( 
 109                 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id
, 
 110                 video_id
, note
=False) 
 111         except ExtractorError 
as err
: 
 112             self
._downloader
.report_warning(u
'unable to download video subtitles: %s' % compat_str(err
)) 
 114         info 
= json
.loads(sub_list
) 
 115         if (info
['total'] > 0): 
 116             sub_lang_list 
= dict((l
['language'], l
['url']) for l 
in info
['list']) 
 118         self
._downloader
.report_warning(u
'video doesn\'t have subtitles') 
 122 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor
): 
 123     IE_NAME 
= u
'dailymotion:playlist' 
 124     _VALID_URL 
= r
'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/' 
 125     _MORE_PAGES_INDICATOR 
= r
'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>' 
 126     _PAGE_TEMPLATE 
= 'https://www.dailymotion.com/playlist/%s/%s' 
 128     def _extract_entries(self
, id): 
 130         for pagenum 
in itertools
.count(1): 
 131             request 
= self
._build
_request
(self
._PAGE
_TEMPLATE 
% (id, pagenum
)) 
 132             webpage 
= self
._download
_webpage
(request
, 
 133                                              id, u
'Downloading page %s' % pagenum
) 
 135             playlist_el 
= get_element_by_attribute(u
'class', u
'video_list', webpage
) 
 136             video_ids
.extend(re
.findall(r
'data-id="(.+?)" data-ext-id', playlist_el
)) 
 138             if re
.search(self
._MORE
_PAGES
_INDICATOR
, webpage
, re
.DOTALL
) is None: 
 140         return [self
.url_result('http://www.dailymotion.com/video/%s' % video_id
, 'Dailymotion') 
 141                    for video_id 
in video_ids
] 
 143     def _real_extract(self
, url
): 
 144         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 145         playlist_id 
= mobj
.group('id') 
 146         webpage 
= self
._download
_webpage
(url
, playlist_id
) 
 148         return {'_type': 'playlist', 
 150                 'title': get_element_by_id(u
'playlist_name', webpage
), 
 151                 'entries': self
._extract
_entries
(playlist_id
), 
 155 class DailymotionUserIE(DailymotionPlaylistIE
): 
 156     IE_NAME 
= u
'dailymotion:user' 
 157     _VALID_URL 
= r
'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)' 
 158     _MORE_PAGES_INDICATOR 
= r
'<div class="next">.*?<a.*?href="/user/.+?".*?>.*?</a>.*?</div>' 
 159     _PAGE_TEMPLATE 
= 'http://www.dailymotion.com/user/%s/%s' 
 161     def _real_extract(self
, url
): 
 162         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 163         user 
= mobj
.group('user') 
 164         webpage 
= self
._download
_webpage
(url
, user
) 
 165         full_user 
= self
._html
_search
_regex
( 
 166             r
'<a class="label" href="/%s".*?>(.*?)</' % re
.escape(user
), 
 167             webpage
, u
'user', flags
=re
.DOTALL
) 
 173             'entries': self
._extract
_entries
(user
),