You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
9.0 KiB

  1. #coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import itertools
  6. from .common import InfoExtractor
  7. from .subtitles import SubtitlesInfoExtractor
  8. from ..utils import (
  9. compat_urllib_request,
  10. compat_str,
  11. orderedSet,
  12. str_to_int,
  13. int_or_none,
  14. ExtractorError,
  15. unescapeHTML,
  16. )
  17. class DailymotionBaseInfoExtractor(InfoExtractor):
  18. @staticmethod
  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')
  23. request.add_header('Cookie', 'ff=off')
  24. return request
  25. class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
  26. """Information Extractor for Dailymotion"""
  27. _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
  28. IE_NAME = 'dailymotion'
  29. _FORMATS = [
  30. ('stream_h264_ld_url', 'ld'),
  31. ('stream_h264_url', 'standard'),
  32. ('stream_h264_hq_url', 'hq'),
  33. ('stream_h264_hd_url', 'hd'),
  34. ('stream_h264_hd1080_url', 'hd180'),
  35. ]
  36. _TESTS = [
  37. {
  38. 'url': 'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  39. 'md5': '392c4b85a60a90dc4792da41ce3144eb',
  40. 'info_dict': {
  41. 'id': 'x33vw9',
  42. 'ext': 'mp4',
  43. 'uploader': 'Amphora Alex and Van .',
  44. 'title': 'Tutoriel de Youtubeur"DL DES VIDEO DE YOUTUBE"',
  45. }
  46. },
  47. # Vevo video
  48. {
  49. 'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
  50. 'info_dict': {
  51. 'title': 'Roar (Official)',
  52. 'id': 'USUV71301934',
  53. 'ext': 'mp4',
  54. 'uploader': 'Katy Perry',
  55. 'upload_date': '20130905',
  56. },
  57. 'params': {
  58. 'skip_download': True,
  59. },
  60. 'skip': 'VEVO is only available in some countries',
  61. },
  62. # age-restricted video
  63. {
  64. 'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband',
  65. 'md5': '0d667a7b9cebecc3c89ee93099c4159d',
  66. 'info_dict': {
  67. 'id': 'xyh2zz',
  68. 'ext': 'mp4',
  69. 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]',
  70. 'uploader': 'HotWaves1012',
  71. 'age_limit': 18,
  72. }
  73. }
  74. ]
  75. def _real_extract(self, url):
  76. # Extract id and simplified title from URL
  77. mobj = re.match(self._VALID_URL, url)
  78. video_id = mobj.group('id')
  79. url = 'http://www.dailymotion.com/video/%s' % video_id
  80. # Retrieve video webpage to extract further information
  81. request = self._build_request(url)
  82. webpage = self._download_webpage(request, video_id)
  83. # Extract URL, uploader and title from webpage
  84. self.report_extraction(video_id)
  85. # It may just embed a vevo video:
  86. m_vevo = re.search(
  87. r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?videoId=(?P<id>[\w]*)',
  88. webpage)
  89. if m_vevo is not None:
  90. vevo_id = m_vevo.group('id')
  91. self.to_screen('Vevo video detected: %s' % vevo_id)
  92. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  93. age_limit = self._rta_search(webpage)
  94. video_upload_date = None
  95. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  96. if mobj is not None:
  97. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  98. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  99. embed_page = self._download_webpage(embed_url, video_id,
  100. 'Downloading embed page')
  101. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  102. 'video info', flags=re.MULTILINE)
  103. info = json.loads(info)
  104. if info.get('error') is not None:
  105. msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
  106. raise ExtractorError(msg, expected=True)
  107. formats = []
  108. for (key, format_id) in self._FORMATS:
  109. video_url = info.get(key)
  110. if video_url is not None:
  111. m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
  112. if m_size is not None:
  113. width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
  114. else:
  115. width, height = None, None
  116. formats.append({
  117. 'url': video_url,
  118. 'ext': 'mp4',
  119. 'format_id': format_id,
  120. 'width': width,
  121. 'height': height,
  122. })
  123. if not formats:
  124. raise ExtractorError('Unable to extract video URL')
  125. # subtitles
  126. video_subtitles = self.extract_subtitles(video_id, webpage)
  127. if self._downloader.params.get('listsubtitles', False):
  128. self._list_available_subtitles(video_id, webpage)
  129. return
  130. view_count = self._search_regex(
  131. r'video_views_count[^>]+>\s+([\d\.,]+)', webpage, 'view count', fatal=False)
  132. if view_count is not None:
  133. view_count = str_to_int(view_count)
  134. return {
  135. 'id': video_id,
  136. 'formats': formats,
  137. 'uploader': info['owner.screenname'],
  138. 'upload_date': video_upload_date,
  139. 'title': self._og_search_title(webpage),
  140. 'subtitles': video_subtitles,
  141. 'thumbnail': info['thumbnail_url'],
  142. 'age_limit': age_limit,
  143. 'view_count': view_count,
  144. }
  145. def _get_available_subtitles(self, video_id, webpage):
  146. try:
  147. sub_list = self._download_webpage(
  148. 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
  149. video_id, note=False)
  150. except ExtractorError as err:
  151. self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
  152. return {}
  153. info = json.loads(sub_list)
  154. if (info['total'] > 0):
  155. sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
  156. return sub_lang_list
  157. self._downloader.report_warning('video doesn\'t have subtitles')
  158. return {}
  159. class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
  160. IE_NAME = 'dailymotion:playlist'
  161. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  162. _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
  163. _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
  164. _TESTS = [{
  165. 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
  166. 'info_dict': {
  167. 'title': 'SPORT',
  168. },
  169. 'playlist_mincount': 20,
  170. }]
  171. def _extract_entries(self, id):
  172. video_ids = []
  173. for pagenum in itertools.count(1):
  174. request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
  175. webpage = self._download_webpage(request,
  176. id, 'Downloading page %s' % pagenum)
  177. video_ids.extend(re.findall(r'data-xid="(.+?)"', webpage))
  178. if re.search(self._MORE_PAGES_INDICATOR, webpage) is None:
  179. break
  180. return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  181. for video_id in orderedSet(video_ids)]
  182. def _real_extract(self, url):
  183. mobj = re.match(self._VALID_URL, url)
  184. playlist_id = mobj.group('id')
  185. webpage = self._download_webpage(url, playlist_id)
  186. return {
  187. '_type': 'playlist',
  188. 'id': playlist_id,
  189. 'title': self._og_search_title(webpage),
  190. 'entries': self._extract_entries(playlist_id),
  191. }
  192. class DailymotionUserIE(DailymotionPlaylistIE):
  193. IE_NAME = 'dailymotion:user'
  194. _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
  195. _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
  196. _TESTS = [{
  197. 'url': 'https://www.dailymotion.com/user/nqtv',
  198. 'info_dict': {
  199. 'id': 'nqtv',
  200. 'title': 'Rémi Gaillard',
  201. },
  202. 'playlist_mincount': 100,
  203. }]
  204. def _real_extract(self, url):
  205. mobj = re.match(self._VALID_URL, url)
  206. user = mobj.group('user')
  207. webpage = self._download_webpage(url, user)
  208. full_user = unescapeHTML(self._html_search_regex(
  209. r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
  210. webpage, 'user'))
  211. return {
  212. '_type': 'playlist',
  213. 'id': user,
  214. 'title': full_user,
  215. 'entries': self._extract_entries(user),
  216. }