dailymotion.py 7.7 KB

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