5 from .common 
import InfoExtractor
 
   6 from .subtitles 
import SubtitlesInfoExtractor
 
  11     get_element_by_attribute
, 
  18 class DailymotionBaseInfoExtractor(InfoExtractor
): 
  20     def _build_request(url
): 
  21         """Build a request with the family filter disabled""" 
  22         request 
= compat_urllib_request
.Request(url
) 
  23         request
.add_header('Cookie', 'family_filter=off') 
  26 class DailymotionIE(DailymotionBaseInfoExtractor
, SubtitlesInfoExtractor
): 
  27     """Information Extractor for Dailymotion""" 
  29     _VALID_URL 
= r
'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)' 
  30     IE_NAME 
= u
'dailymotion' 
  33             u
'url': u
'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech', 
  34             u
'file': u
'x33vw9.mp4', 
  35             u
'md5': u
'392c4b85a60a90dc4792da41ce3144eb', 
  37                 u
"uploader": u
"Amphora Alex and Van .",  
  38                 u
"title": u
"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\"" 
  43             u
'url': u
'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi', 
  44             u
'file': u
'USUV71301934.mp4', 
  46                 u
'title': u
'Roar (Official)', 
  47                 u
'uploader': u
'Katy Perry', 
  48                 u
'upload_date': u
'20130905', 
  51                 u
'skip_download': True, 
  53             u
'skip': u
'VEVO is only available in some countries', 
  57     def _real_extract(self
, url
): 
  58         # Extract id and simplified title from URL 
  59         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  61         video_id 
= mobj
.group(1).split('_')[0].split('?')[0] 
  63         video_extension 
= 'mp4' 
  64         url 
= 'http://www.dailymotion.com/video/%s' % video_id
 
  66         # Retrieve video webpage to extract further information 
  67         request 
= self
._build
_request
(url
) 
  68         webpage 
= self
._download
_webpage
(request
, video_id
) 
  70         # Extract URL, uploader and title from webpage 
  71         self
.report_extraction(video_id
) 
  73         # It may just embed a vevo video: 
  75             r
'<link rel="video_src" href="[^"]*?vevo.com[^"]*?videoId=(?P<id>[\w]*)', 
  77         if m_vevo 
is not None: 
  78             vevo_id 
= m_vevo
.group('id') 
  79             self
.to_screen(u
'Vevo video detected: %s' % vevo_id
) 
  80             return self
.url_result(u
'vevo:%s' % vevo_id
, ie
='Vevo') 
  82         video_uploader 
= self
._search
_regex
([r
'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>', 
  83                                              # Looking for official user 
  84                                              r
'<(?:span|a) .*?rel="author".*?>([^<]+?)</'], 
  85                                             webpage
, 'video uploader') 
  87         video_upload_date 
= None 
  88         mobj 
= re
.search(r
'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage
) 
  90             video_upload_date 
= mobj
.group(3) + mobj
.group(2) + mobj
.group(1) 
  92         embed_url 
= 'http://www.dailymotion.com/embed/video/%s' % video_id
 
  93         embed_page 
= self
._download
_webpage
(embed_url
, video_id
, 
  94                                             u
'Downloading embed page') 
  95         info 
= self
._search
_regex
(r
'var info = ({.*?}),$', embed_page
, 
  96             'video info', flags
=re
.MULTILINE
) 
  97         info 
= json
.loads(info
) 
  98         if info
.get('error') is not None: 
  99             msg 
= 'Couldn\'t get video, Dailymotion says: %s' % info
['error']['title'] 
 100             raise ExtractorError(msg
, expected
=True) 
 102         # TODO: support choosing qualities 
 104         for key 
in ['stream_h264_hd1080_url','stream_h264_hd_url', 
 105                     'stream_h264_hq_url','stream_h264_url', 
 106                     'stream_h264_ld_url']: 
 107             if info
.get(key
):#key in info and info[key]: 
 109                 self
.to_screen(u
'Using %s' % key
) 
 112             raise ExtractorError(u
'Unable to extract video URL') 
 113         video_url 
= info
[max_quality
] 
 116         video_subtitles 
= self
.extract_subtitles(video_id
) 
 117         if self
._downloader
.params
.get('listsubtitles', False): 
 118             self
._list
_available
_subtitles
(video_id
) 
 124             'uploader': video_uploader
, 
 125             'upload_date':  video_upload_date
, 
 126             'title':    self
._og
_search
_title
(webpage
), 
 127             'ext':      video_extension
, 
 128             'subtitles':    video_subtitles
, 
 129             'thumbnail': info
['thumbnail_url'] 
 132     def _get_available_subtitles(self
, video_id
): 
 134             sub_list 
= self
._download
_webpage
( 
 135                 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id
, 
 136                 video_id
, note
=False) 
 137         except ExtractorError 
as err
: 
 138             self
._downloader
.report_warning(u
'unable to download video subtitles: %s' % compat_str(err
)) 
 140         info 
= json
.loads(sub_list
) 
 141         if (info
['total'] > 0): 
 142             sub_lang_list 
= dict((l
['language'], l
['url']) for l 
in info
['list']) 
 144         self
._downloader
.report_warning(u
'video doesn\'t have subtitles') 
 148 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor
): 
 149     IE_NAME 
= u
'dailymotion:playlist' 
 150     _VALID_URL 
= r
'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/' 
 151     _MORE_PAGES_INDICATOR 
= r
'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>' 
 152     _PAGE_TEMPLATE 
= 'https://www.dailymotion.com/playlist/%s/%s' 
 154     def _extract_entries(self
, id): 
 156         for pagenum 
in itertools
.count(1): 
 157             request 
= self
._build
_request
(self
._PAGE
_TEMPLATE 
% (id, pagenum
)) 
 158             webpage 
= self
._download
_webpage
(request
, 
 159                                              id, u
'Downloading page %s' % pagenum
) 
 161             playlist_el 
= get_element_by_attribute(u
'class', u
'video_list', webpage
) 
 162             video_ids
.extend(re
.findall(r
'data-id="(.+?)"', playlist_el
)) 
 164             if re
.search(self
._MORE
_PAGES
_INDICATOR
, webpage
, re
.DOTALL
) is None: 
 166         return [self
.url_result('http://www.dailymotion.com/video/%s' % video_id
, 'Dailymotion') 
 167                    for video_id 
in orderedSet(video_ids
)] 
 169     def _real_extract(self
, url
): 
 170         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 171         playlist_id 
= mobj
.group('id') 
 172         webpage 
= self
._download
_webpage
(url
, playlist_id
) 
 174         return {'_type': 'playlist', 
 176                 'title': get_element_by_id(u
'playlist_name', webpage
), 
 177                 'entries': self
._extract
_entries
(playlist_id
), 
 181 class DailymotionUserIE(DailymotionPlaylistIE
): 
 182     IE_NAME 
= u
'dailymotion:user' 
 183     _VALID_URL 
= r
'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)' 
 184     _MORE_PAGES_INDICATOR 
= r
'<div class="next">.*?<a.*?href="/user/.+?".*?>.*?</a>.*?</div>' 
 185     _PAGE_TEMPLATE 
= 'http://www.dailymotion.com/user/%s/%s' 
 187     def _real_extract(self
, url
): 
 188         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 189         user 
= mobj
.group('user') 
 190         webpage 
= self
._download
_webpage
(url
, user
) 
 191         full_user 
= self
._html
_search
_regex
( 
 192             r
'<a class="label" href="/%s".*?>(.*?)</' % re
.escape(user
), 
 193             webpage
, u
'user', flags
=re
.DOTALL
) 
 199             'entries': self
._extract
_entries
(user
),