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|swf)/(?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'), 
  53         'url': 'http://www.dailymotion.com/video/x5kesuj_office-christmas-party-review-jason-bateman-olivia-munn-t-j-miller_news', 
  54         'md5': '074b95bdee76b9e3654137aee9c79dfe', 
  58             'title': 'Office Christmas Party Review –  Jason Bateman, Olivia Munn, T.J. Miller', 
  59             'description': 'Office Christmas Party Review -  Jason Bateman, Olivia Munn, T.J. Miller', 
  60             'thumbnail': r
're:^https?:.*\.(?:jpg|png)$', 
  62             'timestamp': 1493651285, 
  63             'upload_date': '20170501', 
  64             'uploader': 'Deadline', 
  65             'uploader_id': 'x1xm8ri', 
  70         'url': 'https://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames', 
  71         'md5': '2137c41a8e78554bb09225b8eb322406', 
  75             'title': 'Steam Machine Models, Pricing Listed on Steam Store - IGN News', 
  76             'description': 'Several come bundled with the Steam Controller.', 
  77             'thumbnail': r
're:^https?:.*\.(?:jpg|png)$', 
  79             'timestamp': 1425657362, 
  80             'upload_date': '20150306', 
  82             'uploader_id': 'xijv66', 
  89         'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi', 
  91             'title': 'Roar (Official)', 
  94             'uploader': 'Katy Perry', 
  95             'upload_date': '20130905', 
  98             'skip_download': True, 
 100         'skip': 'VEVO is only available in some countries', 
 102         # age-restricted video 
 103         'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband', 
 104         'md5': '0d667a7b9cebecc3c89ee93099c4159d', 
 108             'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]', 
 109             'uploader': 'HotWaves1012', 
 112         'skip': 'video gone', 
 114         # geo-restricted, player v5 
 115         'url': 'http://www.dailymotion.com/video/xhza0o', 
 116         'only_matching': True, 
 119         'url': 'http://www.dailymotion.com/video/x20su5f_the-power-of-nightmares-1-the-rise-of-the-politics-of-fear-bbc-2004_news', 
 120         'only_matching': True, 
 122         'url': 'http://www.dailymotion.com/swf/video/x3n92nf', 
 123         'only_matching': True, 
 125         'url': 'http://www.dailymotion.com/swf/x3ss1m_funny-magic-trick-barry-and-stuart_fun', 
 126         'only_matching': True, 
 130     def _extract_urls(webpage
): 
 131         # Look for embedded Dailymotion player 
 132         matches 
= re
.findall( 
 133             r
'<(?:(?:embed|iframe)[^>]+?src=|input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^
>]+value
=)(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/(?:embed|swf)/video/.+?)\1', webpage) 
 134         return list(map(lambda m: unescapeHTML(m[1]), matches)) 
 136     def _real_extract(self, url): 
 137         video_id = self._match_id(url) 
 139         webpage = self._download_webpage_no_ff( 
 140             'https://www.dailymotion.com/video/%s' % video_id, video_id) 
 142         age_limit = self._rta_search(webpage) 
 144         description = self._og_search_description(webpage) or self._html_search_meta( 
 145             'description', webpage, 'description') 
 147         view_count_str = self._search_regex( 
 148             (r'<meta[^>]+itemprop="interactionCount
"[^>]+content="UserPlays
:([\s\d
,.]+)"', 
 149              r'video_views_count[^>]+>\s+([\s\d\,.]+)'), 
 150             webpage, 'view count', default=None) 
 152             view_count_str = re.sub(r'\s', '', view_count_str) 
 153         view_count = str_to_int(view_count_str) 
 154         comment_count = int_or_none(self._search_regex( 
 155             r'<meta[^>]+itemprop="interactionCount
"[^>]+content="UserComments
:(\d
+)"', 
 156             webpage, 'comment count', default=None)) 
 158         player_v5 = self._search_regex( 
 159             [r'buildPlayer\(({.+?})\);\n',  # See https://github.com/rg3/youtube-dl/issues/7826 
 160              r'playerV5\s*=\s*dmp\.create\([^,]+?,\s*({.+?})\);', 
 161              r'buildPlayer\(({.+?})\);', 
 162              r'var\s+config\s*=\s*({.+?});', 
 163              # New layout regex (see https://github.com/rg3/youtube-dl/issues/13580) 
 164              r'__PLAYER_CONFIG__\s*=\s*({.+?});'], 
 165             webpage, 'player v5', default=None) 
 167             player = self._parse_json(player_v5, video_id) 
 168             metadata = player['metadata'] 
 170             self._check_error(metadata) 
 173             for quality, media_list in metadata['qualities'].items(): 
 174                 for media in media_list: 
 175                     media_url = media.get('url') 
 178                     type_ = media.get('type') 
 179                     if type_ == 'application/vnd.lumberjack.manifest': 
 181                     ext = mimetype2ext(type_) or determine_ext(media_url) 
 183                         formats.extend(self._extract_m3u8_formats( 
 184                             media_url, video_id, 'mp4', preference=-1, 
 185                             m3u8_id='hls', fatal=False)) 
 187                         formats.extend(self._extract_f4m_formats( 
 188                             media_url, video_id, preference=-1, f4m_id='hds', fatal=False)) 
 192                             'format_id': 'http-%s' % quality, 
 195                         m = re.search(r'H264-(?P<width>\d+)x(?P<height>\d+)', media_url) 
 198                                 'width': int(m.group('width')), 
 199                                 'height': int(m.group('height')), 
 202             self._sort_formats(formats) 
 204             title = metadata['title'] 
 205             duration = int_or_none(metadata.get('duration')) 
 206             timestamp = int_or_none(metadata.get('created_time')) 
 207             thumbnail = metadata.get('poster_url') 
 208             uploader = metadata.get('owner', {}).get('screenname') 
 209             uploader_id = metadata.get('owner', {}).get('id') 
 212             subtitles_data = metadata.get('subtitles', {}).get('data', {}) 
 213             if subtitles_data and isinstance(subtitles_data, dict): 
 214                 for subtitle_lang, subtitle in subtitles_data.items(): 
 215                     subtitles[subtitle_lang] = [{ 
 216                         'ext': determine_ext(subtitle_url), 
 218                     } for subtitle_url in subtitle.get('urls', [])] 
 223                 'description': description, 
 224                 'thumbnail': thumbnail, 
 225                 'duration': duration, 
 226                 'timestamp': timestamp, 
 227                 'uploader': uploader, 
 228                 'uploader_id': uploader_id, 
 229                 'age_limit': age_limit, 
 230                 'view_count': view_count, 
 231                 'comment_count': comment_count, 
 233                 'subtitles': subtitles, 
 237         vevo_id = self._search_regex( 
 238             r'<link rel="video_src
" href="[^
"]*?vevo\.com[^"]*?video
=(?P
<id>[\w
]*)', 
 239             webpage, 'vevo embed
', default=None) 
 241             return self.url_result('vevo
:%s' % vevo_id, 'Vevo
') 
 243         # fallback old player 
 244         embed_page = self._download_webpage_no_ff( 
 245             'https
://www
.dailymotion
.com
/embed
/video
/%s' % video_id, 
 246             video_id, 'Downloading embed page
') 
 248         timestamp = parse_iso8601(self._html_search_meta( 
 249             'video
:release_date
', webpage, 'upload date
')) 
 251         info = self._parse_json( 
 253                 r'var info 
= ({.*?
}),$
', embed_page, 
 254                 'video info
', flags=re.MULTILINE), 
 257         self._check_error(info) 
 260         for (key, format_id) in self._FORMATS: 
 261             video_url = info.get(key) 
 262             if video_url is not None: 
 263                 m_size = re.search(r'H264
-(\d
+)x(\d
+)', video_url) 
 264                 if m_size is not None: 
 265                     width, height = map(int_or_none, (m_size.group(1), m_size.group(2))) 
 267                     width, height = None, None 
 271                     'format_id
': format_id, 
 275         self._sort_formats(formats) 
 278         video_subtitles = self.extract_subtitles(video_id, webpage) 
 280         title = self._og_search_title(webpage, default=None) 
 282             title = self._html_search_regex( 
 283                 r'(?s
)<span\s
+id="video_title"[^
>]*>(.*?
)</span
>', webpage, 
 289             'uploader
': info['owner
.screenname
'], 
 290             'timestamp
': timestamp, 
 292             'description
': description, 
 293             'subtitles
': video_subtitles, 
 294             'thumbnail
': info['thumbnail_url
'], 
 295             'age_limit
': age_limit, 
 296             'view_count
': view_count, 
 297             'duration
': info['duration
'] 
 300     def _check_error(self, info): 
 301         error = info.get('error
') 
 302         if info.get('error
') is not None: 
 303             title = error['title
'] 
 304             # See https://developer.dailymotion.com/api#access-error 
 305             if error.get('code
') == 'DM007
': 
 306                 self.raise_geo_restricted(msg=title) 
 307             raise ExtractorError( 
 308                 '%s said
: %s' % (self.IE_NAME, title), expected=True) 
 310     def _get_subtitles(self, video_id, webpage): 
 312             sub_list = self._download_webpage( 
 313                 'https
://api
.dailymotion
.com
/video
/%s/subtitles?fields
=id,language
,url
' % video_id, 
 314                 video_id, note=False) 
 315         except ExtractorError as err: 
 316             self._downloader.report_warning('unable to download video subtitles
: %s' % error_to_compat_str(err)) 
 318         info = json.loads(sub_list) 
 319         if (info['total
'] > 0): 
 320             sub_lang_list = dict((l['language
'], [{'url
': l['url
'], 'ext
': 'srt
'}]) for l in info['list']) 
 322         self._downloader.report_warning('video doesn
\'t have subtitles
') 
 326 class DailymotionPlaylistIE(DailymotionBaseInfoExtractor): 
 327     IE_NAME = 'dailymotion
:playlist
' 
 328     _VALID_URL = r'(?
:https?
://)?
(?
:www\
.)?dailymotion\
.[a
-z
]{2,3}/playlist
/(?P
<id>[^
/?
#&]+)' 
 329     _MORE_PAGES_INDICATOR 
= r
'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"' 
 330     _PAGE_TEMPLATE 
= 'https://www.dailymotion.com/playlist/%s/%s' 
 332         'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q', 
 335             'id': 'xv4bw_nqtv_sport', 
 337         'playlist_mincount': 20, 
 340     def _extract_entries(self
, id): 
 342         processed_urls 
= set() 
 343         for pagenum 
in itertools
.count(1): 
 344             page_url 
= self
._PAGE
_TEMPLATE 
% (id, pagenum
) 
 345             webpage
, urlh 
= self
._download
_webpage
_handle
_no
_ff
( 
 346                 page_url
, id, 'Downloading page %s' % pagenum
) 
 347             if urlh
.geturl() in processed_urls
: 
 348                 self
.report_warning('Stopped at duplicated page %s, which is the same as %s' % ( 
 349                     page_url
, urlh
.geturl()), id) 
 352             processed_urls
.add(urlh
.geturl()) 
 354             for video_id 
in re
.findall(r
'data-xid="(.+?)"', webpage
): 
 355                 if video_id 
not in video_ids
: 
 356                     yield self
.url_result( 
 357                         'http://www.dailymotion.com/video/%s' % video_id
, 
 358                         DailymotionIE
.ie_key(), video_id
) 
 359                     video_ids
.add(video_id
) 
 361             if re
.search(self
._MORE
_PAGES
_INDICATOR
, webpage
) is None: 
 364     def _real_extract(self
, url
): 
 365         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 366         playlist_id 
= mobj
.group('id') 
 367         webpage 
= self
._download
_webpage
(url
, playlist_id
) 
 372             'title': self
._og
_search
_title
(webpage
), 
 373             'entries': self
._extract
_entries
(playlist_id
), 
 377 class DailymotionUserIE(DailymotionPlaylistIE
): 
 378     IE_NAME 
= 'dailymotion:user' 
 379     _VALID_URL 
= r
'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?!(?:embed|swf|#|video|playlist)/)(?:(?:old/)?user/)?(?P<user>[^/]+)' 
 380     _PAGE_TEMPLATE 
= 'http://www.dailymotion.com/user/%s/%s' 
 382         'url': 'https://www.dailymotion.com/user/nqtv', 
 385             'title': 'Rémi Gaillard', 
 387         'playlist_mincount': 100, 
 389         'url': 'http://www.dailymotion.com/user/UnderProject', 
 391             'id': 'UnderProject', 
 392             'title': 'UnderProject', 
 394         'playlist_mincount': 1800, 
 395         'expected_warnings': [ 
 396             'Stopped at duplicated page', 
 398         'skip': 'Takes too long time', 
 401     def _real_extract(self
, url
): 
 402         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 403         user 
= mobj
.group('user') 
 404         webpage 
= self
._download
_webpage
( 
 405             'https://www.dailymotion.com/user/%s' % user
, user
) 
 406         full_user 
= unescapeHTML(self
._html
_search
_regex
( 
 407             r
'<a class="nav-image" title="([^"]+)" href="/%s">' % re
.escape(user
), 
 414             'entries': self
._extract
_entries
(user
),