2 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  23 class DailymotionBaseInfoExtractor(InfoExtractor
): 
  25     def _build_request(url
): 
  26         """Build a request with the family filter disabled""" 
  27         request 
= sanitized_Request(url
) 
  28         request
.add_header('Cookie', 'family_filter=off; ff=off') 
  31     def _download_webpage_handle_no_ff(self
, url
, *args
, **kwargs
): 
  32         request 
= self
._build
_request
(url
) 
  33         return self
._download
_webpage
_handle
(request
, *args
, **kwargs
) 
  35     def _download_webpage_no_ff(self
, url
, *args
, **kwargs
): 
  36         request 
= self
._build
_request
(url
) 
  37         return self
._download
_webpage
(request
, *args
, **kwargs
) 
  40 class DailymotionIE(DailymotionBaseInfoExtractor
): 
  41     _VALID_URL 
= r
'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(?:embed|swf|#)/)?video/(?P<id>[^/?_]+)' 
  42     IE_NAME 
= 'dailymotion' 
  45         ('stream_h264_ld_url', 'ld'), 
  46         ('stream_h264_url', 'standard'), 
  47         ('stream_h264_hq_url', 'hq'), 
  48         ('stream_h264_hd_url', 'hd'), 
  49         ('stream_h264_hd1080_url', 'hd180'), 
  54             'url': 'https://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames', 
  55             'md5': '2137c41a8e78554bb09225b8eb322406', 
  59                 'title': 'Steam Machine Models, Pricing Listed on Steam Store - IGN News', 
  60                 'description': 'Several come bundled with the Steam Controller.', 
  61                 'thumbnail': 're:^https?:.*\.(?:jpg|png)$', 
  63                 'timestamp': 1425657362, 
  64                 'upload_date': '20150306', 
  66                 'uploader_id': 'xijv66', 
  74             'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi', 
  76                 'title': 'Roar (Official)', 
  79                 'uploader': 'Katy Perry', 
  80                 'upload_date': '20130905', 
  83                 'skip_download': True, 
  85             'skip': 'VEVO is only available in some countries', 
  87         # age-restricted video 
  89             'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband', 
  90             'md5': '0d667a7b9cebecc3c89ee93099c4159d', 
  94                 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]', 
  95                 'uploader': 'HotWaves1012', 
  99         # geo-restricted, player v5 
 101             'url': 'http://www.dailymotion.com/video/xhza0o', 
 102             'only_matching': True, 
 106             'url': 'http://www.dailymotion.com/video/x20su5f_the-power-of-nightmares-1-the-rise-of-the-politics-of-fear-bbc-2004_news', 
 107             'only_matching': True, 
 110             'url': 'http://www.dailymotion.com/swf/video/x3n92nf', 
 111             'only_matching': True, 
 116     def _extract_urls(webpage
): 
 117         # Look for embedded Dailymotion player 
 118         matches 
= re
.findall( 
 119             r
'<(?:(?:embed|iframe)[^>]+?src=|input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^
>]+value
=)(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/(?:embed|swf)/video/.+?)\1', webpage) 
 120         return list(map(lambda m: unescapeHTML(m[1]), matches)) 
 122     def _real_extract(self, url): 
 123         video_id = self._match_id(url) 
 125         webpage = self._download_webpage_no_ff( 
 126             'https://www.dailymotion.com/video/%s' % video_id, video_id) 
 128         age_limit = self._rta_search(webpage) 
 130         description = self._og_search_description(webpage) or self._html_search_meta( 
 131             'description', webpage, 'description') 
 133         view_count_str = self._search_regex( 
 134             (r'<meta[^>]+itemprop="interactionCount
"[^>]+content="UserPlays
:([\s\d
,.]+)"', 
 135              r'video_views_count[^>]+>\s+([\s\d\,.]+)'), 
 136             webpage, 'view count', fatal=False) 
 138             view_count_str = re.sub(r'\s', '', view_count_str) 
 139         view_count = str_to_int(view_count_str) 
 140         comment_count = int_or_none(self._search_regex( 
 141             r'<meta[^>]+itemprop="interactionCount
"[^>]+content="UserComments
:(\d
+)"', 
 142             webpage, 'comment count', fatal=False)) 
 144         player_v5 = self._search_regex( 
 145             [r'buildPlayer\(({.+?})\);\n',  # See https://github.com/rg3/youtube-dl/issues/7826 
 146              r'playerV5\s*=\s*dmp\.create\([^,]+?,\s*({.+?})\);', 
 147              r'buildPlayer\(({.+?})\);'], 
 148             webpage, 'player v5', default=None) 
 150             player = self._parse_json(player_v5, video_id) 
 151             metadata = player['metadata'] 
 153             self._check_error(metadata) 
 156             for quality, media_list in metadata['qualities'].items(): 
 157                 for media in media_list: 
 158                     media_url = media.get('url') 
 161                     type_ = media.get('type') 
 162                     if type_ == 'application/vnd.lumberjack.manifest': 
 164                     ext = mimetype2ext(type_) or determine_ext(media_url) 
 166                         formats.extend(self._extract_m3u8_formats( 
 167                             media_url, video_id, 'mp4', preference=-1, 
 168                             m3u8_id='hls', fatal=False)) 
 170                         formats.extend(self._extract_f4m_formats( 
 171                             media_url, video_id, preference=-1, f4m_id='hds', fatal=False)) 
 175                             'format_id': 'http-%s' % quality, 
 178                         m = re.search(r'H264-(?P<width>\d+)x(?P<height>\d+)', media_url) 
 181                                 'width': int(m.group('width')), 
 182                                 'height': int(m.group('height')), 
 185             self._sort_formats(formats) 
 187             title = metadata['title'] 
 188             duration = int_or_none(metadata.get('duration')) 
 189             timestamp = int_or_none(metadata.get('created_time')) 
 190             thumbnail = metadata.get('poster_url') 
 191             uploader = metadata.get('owner', {}).get('screenname') 
 192             uploader_id = metadata.get('owner', {}).get('id') 
 195             subtitles_data = metadata.get('subtitles', {}).get('data', {}) 
 196             if subtitles_data and isinstance(subtitles_data, dict): 
 197                 for subtitle_lang, subtitle in subtitles_data.items(): 
 198                     subtitles[subtitle_lang] = [{ 
 199                         'ext': determine_ext(subtitle_url), 
 201                     } for subtitle_url in subtitle.get('urls', [])] 
 206                 'description': description, 
 207                 'thumbnail': thumbnail, 
 208                 'duration': duration, 
 209                 'timestamp': timestamp, 
 210                 'uploader': uploader, 
 211                 'uploader_id': uploader_id, 
 212                 'age_limit': age_limit, 
 213                 'view_count': view_count, 
 214                 'comment_count': comment_count, 
 216                 'subtitles': subtitles, 
 220         vevo_id = self._search_regex( 
 221             r'<link rel="video_src
" href="[^
"]*?vevo.com[^"]*?video
=(?P
<id>[\w
]*)', 
 222             webpage, 'vevo embed
', default=None) 
 224             return self.url_result('vevo
:%s' % vevo_id, 'Vevo
') 
 226         # fallback old player 
 227         embed_page = self._download_webpage_no_ff( 
 228             'https
://www
.dailymotion
.com
/embed
/video
/%s' % video_id, 
 229             video_id, 'Downloading embed page
') 
 231         timestamp = parse_iso8601(self._html_search_meta( 
 232             'video
:release_date
', webpage, 'upload date
')) 
 234         info = self._parse_json( 
 236                 r'var info 
= ({.*?
}),$
', embed_page, 
 237                 'video info
', flags=re.MULTILINE), 
 240         self._check_error(info) 
 243         for (key, format_id) in self._FORMATS: 
 244             video_url = info.get(key) 
 245             if video_url is not None: 
 246                 m_size = re.search(r'H264
-(\d
+)x(\d
+)', video_url) 
 247                 if m_size is not None: 
 248                     width, height = map(int_or_none, (m_size.group(1), m_size.group(2))) 
 250                     width, height = None, None 
 254                     'format_id
': format_id, 
 258         self._sort_formats(formats) 
 261         video_subtitles = self.extract_subtitles(video_id, webpage) 
 263         title = self._og_search_title(webpage, default=None) 
 265             title = self._html_search_regex( 
 266                 r'(?s
)<span\s
+id="video_title"[^
>]*>(.*?
)</span
>', webpage, 
 272             'uploader
': info['owner
.screenname
'], 
 273             'timestamp
': timestamp, 
 275             'description
': description, 
 276             'subtitles
': video_subtitles, 
 277             'thumbnail
': info['thumbnail_url
'], 
 278             'age_limit
': age_limit, 
 279             'view_count
': view_count, 
 280             'duration
': info['duration
'] 
 283     def _check_error(self, info): 
 284         if info.get('error
') is not None: 
 285             raise ExtractorError( 
 286                 '%s said
: %s' % (self.IE_NAME, info['error
']['title
']), expected=True) 
 288     def _get_subtitles(self, video_id, webpage): 
 290             sub_list = self._download_webpage( 
 291                 'https
://api
.dailymotion
.com
/video
/%s/subtitles?fields
=id,language
,url
' % video_id, 
 292                 video_id, note=False) 
 293         except ExtractorError as err: 
 294             self._downloader.report_warning('unable to download video subtitles
: %s' % error_to_compat_str(err)) 
 296         info = json.loads(sub_list) 
 297         if (info['total
'] > 0): 
 298             sub_lang_list = dict((l['language
'], [{'url
': l['url
'], 'ext
': 'srt
'}]) for l in info['list']) 
 300         self._downloader.report_warning('video doesn
\'t have subtitles
') 
 304 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor): 
 305     IE_NAME = 'dailymotion
:playlist
' 
 306     _VALID_URL = r'(?
:https?
://)?
(?
:www\
.)?dailymotion\
.[a
-z
]{2,3}/playlist
/(?P
<id>.+?
)/' 
 307     _MORE_PAGES_INDICATOR = r'(?s
)<div 
class="pages[^"]*">.*?<a\s+class="[^
"]*?icon-arrow_right[^"]*?
"' 
 308     _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s' 
 310         'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q', 
 313             'id': 'xv4bw_nqtv_sport', 
 315         'playlist_mincount': 20, 
 318     def _extract_entries(self, id): 
 320         processed_urls = set() 
 321         for pagenum in itertools.count(1): 
 322             page_url = self._PAGE_TEMPLATE % (id, pagenum) 
 323             webpage, urlh = self._download_webpage_handle_no_ff( 
 324                 page_url, id, 'Downloading page %s' % pagenum) 
 325             if urlh.geturl() in processed_urls: 
 326                 self.report_warning('Stopped at duplicated page %s, which is the same as %s' % ( 
 327                     page_url, urlh.geturl()), id) 
 330             processed_urls.add(urlh.geturl()) 
 332             for video_id in re.findall(r'data-xid="(.+?
)"', webpage): 
 333                 if video_id not in video_ids: 
 334                     yield self.url_result( 
 335                         'http://www.dailymotion.com/video/%s' % video_id, 
 336                         DailymotionIE.ie_key(), video_id) 
 337                     video_ids.add(video_id) 
 339             if re.search(self._MORE_PAGES_INDICATOR, webpage) is None: 
 342     def _real_extract(self, url): 
 343         mobj = re.match(self._VALID_URL, url) 
 344         playlist_id = mobj.group('id') 
 345         webpage = self._download_webpage(url, playlist_id) 
 350             'title': self._og_search_title(webpage), 
 351             'entries': self._extract_entries(playlist_id), 
 355 class DailymotionUserIE(DailymotionPlaylistIE): 
 356     IE_NAME = 'dailymotion:user' 
 357     _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?!(?:embed|swf|#|video|playlist)/)(?:(?:old/)?user/)?(?P<user>[^/]+)' 
 358     _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s' 
 360         'url': 'https://www.dailymotion.com/user/nqtv', 
 363             'title': 'Rémi Gaillard', 
 365         'playlist_mincount': 100, 
 367         'url': 'http://www.dailymotion.com/user/UnderProject', 
 369             'id': 'UnderProject', 
 370             'title': 'UnderProject', 
 372         'playlist_mincount': 1800, 
 373         'expected_warnings': [ 
 374             'Stopped at duplicated page', 
 376         'skip': 'Takes too long time', 
 379     def _real_extract(self, url): 
 380         mobj = re.match(self._VALID_URL, url) 
 381         user = mobj.group('user') 
 382         webpage = self._download_webpage( 
 383             'https://www.dailymotion.com/user/%s' % user, user) 
 384         full_user = unescapeHTML(self._html_search_regex( 
 385             r'<a class="nav
-image
" title="([^
"]+)" href
="/%s">' % re.escape(user), 
 392             'entries
': self._extract_entries(user), 
 396 class DailymotionCloudIE(DailymotionBaseInfoExtractor): 
 397     _VALID_URL_PREFIX = r'http
://api\
.dmcloud\
.net
/(?
:player
/)?embed
/' 
 398     _VALID_URL = r'%s[^
/]+/(?P
<id>[^
/?
]+)' % _VALID_URL_PREFIX 
 399     _VALID_EMBED_URL = r'%s[^
/]+/[^
\'"]+' % _VALID_URL_PREFIX 
 402         # From http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html 
 403         # Tested at FranceTvInfo_2 
 404         'url': 'http://api.dmcloud.net/embed/4e7343f894a6f677b10006b4/556e03339473995ee145930c?auth=1464865870-0-jyhsm84b-ead4c701fb750cf9367bf4447167a3db&autoplay=1', 
 405         'only_matching': True, 
 407         # http://www.francetvinfo.fr/societe/larguez-les-amarres-le-cobaturage-se-developpe_980101.html 
 408         'url': 'http://api.dmcloud.net/player/embed/4e7343f894a6f677b10006b4/559545469473996d31429f06?auth=1467430263-0-90tglw2l-a3a4b64ed41efe48d7fccad85b8b8fda&autoplay=1', 
 409         'only_matching': True, 
 413     def _extract_dmcloud_url(cls, webpage): 
 414         mobj = re.search(r'<iframe[^>]+src=[\'"](%s)[\'"]' % cls._VALID_EMBED_URL, webpage) 
 419             r'<input[^>]+id=[\'"]dmcloudUrlEmissionSelect
[\'"][^>]+value=[\'"](%s)[\'"]' % cls._VALID_EMBED_URL, 
 424     def _real_extract(self, url): 
 425         video_id = self._match_id(url) 
 427         webpage = self._download_webpage_no_ff(url, video_id) 
 429         title = self._html_search_regex(r'<title>([^>]+)</title>', webpage, 'title') 
 431         video_info = self._parse_json(self._search_regex( 
 432             r'var\s+info\s*=\s*([^;]+);', webpage, 'video info'), video_id) 
 434         # TODO: parse ios_url, which is in fact a manifest 
 435         video_url = video_info['mp4_url'] 
 441             'thumbnail': video_info.get('thumbnail_url'),