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.

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