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.

213 lines
8.1 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. get_element_by_attribute,
  10. get_element_by_id,
  11. orderedSet,
  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. return request
  21. class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
  22. """Information Extractor for Dailymotion"""
  23. _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)'
  24. IE_NAME = u'dailymotion'
  25. _FORMATS = [
  26. (u'stream_h264_ld_url', u'ld'),
  27. (u'stream_h264_url', u'standard'),
  28. (u'stream_h264_hq_url', u'hq'),
  29. (u'stream_h264_hd_url', u'hd'),
  30. (u'stream_h264_hd1080_url', u'hd180'),
  31. ]
  32. _TESTS = [
  33. {
  34. u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  35. u'file': u'x33vw9.mp4',
  36. u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
  37. u'info_dict': {
  38. u"uploader": u"Amphora Alex and Van .",
  39. u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
  40. }
  41. },
  42. # Vevo video
  43. {
  44. u'url': u'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
  45. u'file': u'USUV71301934.mp4',
  46. u'info_dict': {
  47. u'title': u'Roar (Official)',
  48. u'uploader': u'Katy Perry',
  49. u'upload_date': u'20130905',
  50. },
  51. u'params': {
  52. u'skip_download': True,
  53. },
  54. u'skip': u'VEVO is only available in some countries',
  55. },
  56. ]
  57. def _real_extract(self, url):
  58. # Extract id and simplified title from URL
  59. mobj = re.match(self._VALID_URL, url)
  60. video_id = mobj.group(1).split('_')[0].split('?')[0]
  61. url = 'http://www.dailymotion.com/video/%s' % video_id
  62. # Retrieve video webpage to extract further information
  63. request = self._build_request(url)
  64. webpage = self._download_webpage(request, video_id)
  65. # Extract URL, uploader and title from webpage
  66. self.report_extraction(video_id)
  67. # It may just embed a vevo video:
  68. m_vevo = re.search(
  69. r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?videoId=(?P<id>[\w]*)',
  70. webpage)
  71. if m_vevo is not None:
  72. vevo_id = m_vevo.group('id')
  73. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  74. return self.url_result(u'vevo:%s' % vevo_id, ie='Vevo')
  75. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  76. # Looking for official user
  77. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  78. webpage, 'video uploader')
  79. video_upload_date = None
  80. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  81. if mobj is not None:
  82. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  83. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  84. embed_page = self._download_webpage(embed_url, video_id,
  85. u'Downloading embed page')
  86. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  87. 'video info', flags=re.MULTILINE)
  88. info = json.loads(info)
  89. if info.get('error') is not None:
  90. msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
  91. raise ExtractorError(msg, expected=True)
  92. formats = []
  93. for (key, format_id) in self._FORMATS:
  94. video_url = info.get(key)
  95. if video_url is not None:
  96. m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
  97. if m_size is not None:
  98. width, height = m_size.group(1), m_size.group(2)
  99. else:
  100. width, height = None, None
  101. formats.append({
  102. 'url': video_url,
  103. 'ext': 'mp4',
  104. 'format_id': format_id,
  105. 'width': width,
  106. 'height': height,
  107. })
  108. if not formats:
  109. raise ExtractorError(u'Unable to extract video URL')
  110. # subtitles
  111. video_subtitles = self.extract_subtitles(video_id)
  112. if self._downloader.params.get('listsubtitles', False):
  113. self._list_available_subtitles(video_id)
  114. return
  115. return [{
  116. 'id': video_id,
  117. 'formats': formats,
  118. 'uploader': video_uploader,
  119. 'upload_date': video_upload_date,
  120. 'title': self._og_search_title(webpage),
  121. 'subtitles': video_subtitles,
  122. 'thumbnail': info['thumbnail_url']
  123. }]
  124. def _get_available_subtitles(self, video_id):
  125. try:
  126. sub_list = self._download_webpage(
  127. 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
  128. video_id, note=False)
  129. except ExtractorError as err:
  130. self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
  131. return {}
  132. info = json.loads(sub_list)
  133. if (info['total'] > 0):
  134. sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
  135. return sub_lang_list
  136. self._downloader.report_warning(u'video doesn\'t have subtitles')
  137. return {}
  138. class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
  139. IE_NAME = u'dailymotion:playlist'
  140. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  141. _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
  142. _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
  143. def _extract_entries(self, id):
  144. video_ids = []
  145. for pagenum in itertools.count(1):
  146. request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
  147. webpage = self._download_webpage(request,
  148. id, u'Downloading page %s' % pagenum)
  149. playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
  150. video_ids.extend(re.findall(r'data-id="(.+?)"', playlist_el))
  151. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  152. break
  153. return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  154. for video_id in orderedSet(video_ids)]
  155. def _real_extract(self, url):
  156. mobj = re.match(self._VALID_URL, url)
  157. playlist_id = mobj.group('id')
  158. webpage = self._download_webpage(url, playlist_id)
  159. return {'_type': 'playlist',
  160. 'id': playlist_id,
  161. 'title': get_element_by_id(u'playlist_name', webpage),
  162. 'entries': self._extract_entries(playlist_id),
  163. }
  164. class DailymotionUserIE(DailymotionPlaylistIE):
  165. IE_NAME = u'dailymotion:user'
  166. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
  167. _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/user/.+?".*?>.*?</a>.*?</div>'
  168. _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
  169. def _real_extract(self, url):
  170. mobj = re.match(self._VALID_URL, url)
  171. user = mobj.group('user')
  172. webpage = self._download_webpage(url, user)
  173. full_user = self._html_search_regex(
  174. r'<a class="label" href="/%s".*?>(.*?)</' % re.escape(user),
  175. webpage, u'user', flags=re.DOTALL)
  176. return {
  177. '_type': 'playlist',
  178. 'id': user,
  179. 'title': full_user,
  180. 'entries': self._extract_entries(user),
  181. }