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.

229 lines
8.5 KiB

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