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.

138 lines
5.3 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. ExtractorError,
  12. )
  13. class DailymotionIE(SubtitlesInfoExtractor):
  14. """Information Extractor for Dailymotion"""
  15. _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)'
  16. IE_NAME = u'dailymotion'
  17. _TEST = {
  18. u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  19. u'file': u'x33vw9.mp4',
  20. u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
  21. u'info_dict': {
  22. u"uploader": u"Amphora Alex and Van .",
  23. u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
  24. }
  25. }
  26. def _real_extract(self, url):
  27. # Extract id and simplified title from URL
  28. mobj = re.match(self._VALID_URL, url)
  29. video_id = mobj.group(1).split('_')[0].split('?')[0]
  30. video_extension = 'mp4'
  31. url = 'http://www.dailymotion.com/video/%s' % video_id
  32. # Retrieve video webpage to extract further information
  33. request = compat_urllib_request.Request(url)
  34. request.add_header('Cookie', 'family_filter=off')
  35. webpage = self._download_webpage(request, video_id)
  36. # Extract URL, uploader and title from webpage
  37. self.report_extraction(video_id)
  38. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  39. # Looking for official user
  40. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  41. webpage, 'video uploader')
  42. video_upload_date = None
  43. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  44. if mobj is not None:
  45. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  46. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  47. embed_page = self._download_webpage(embed_url, video_id,
  48. u'Downloading embed page')
  49. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  50. 'video info', flags=re.MULTILINE)
  51. info = json.loads(info)
  52. # TODO: support choosing qualities
  53. for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
  54. 'stream_h264_hq_url','stream_h264_url',
  55. 'stream_h264_ld_url']:
  56. if info.get(key):#key in info and info[key]:
  57. max_quality = key
  58. self.to_screen(u'Using %s' % key)
  59. break
  60. else:
  61. raise ExtractorError(u'Unable to extract video URL')
  62. video_url = info[max_quality]
  63. # subtitles
  64. video_subtitles = self.extract_subtitles(video_id)
  65. if self._downloader.params.get('listsubtitles', False):
  66. self._list_available_subtitles(video_id)
  67. return
  68. return [{
  69. 'id': video_id,
  70. 'url': video_url,
  71. 'uploader': video_uploader,
  72. 'upload_date': video_upload_date,
  73. 'title': self._og_search_title(webpage),
  74. 'ext': video_extension,
  75. 'subtitles': video_subtitles,
  76. 'thumbnail': info['thumbnail_url']
  77. }]
  78. def _get_available_subtitles(self, video_id):
  79. try:
  80. sub_list = self._download_webpage(
  81. 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
  82. video_id, note=False)
  83. except ExtractorError as err:
  84. self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
  85. return {}
  86. info = json.loads(sub_list)
  87. if (info['total'] > 0):
  88. sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
  89. return sub_lang_list
  90. self._downloader.report_warning(u'video doesn\'t have subtitles')
  91. return {}
  92. class DailymotionPlaylistIE(InfoExtractor):
  93. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  94. _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
  95. def _real_extract(self, url):
  96. mobj = re.match(self._VALID_URL, url)
  97. playlist_id = mobj.group('id')
  98. video_ids = []
  99. for pagenum in itertools.count(1):
  100. webpage = self._download_webpage('https://www.dailymotion.com/playlist/%s/%s' % (playlist_id, pagenum),
  101. playlist_id, u'Downloading page %s' % pagenum)
  102. playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
  103. video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
  104. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  105. break
  106. entries = [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  107. for video_id in video_ids]
  108. return {'_type': 'playlist',
  109. 'id': playlist_id,
  110. 'title': get_element_by_id(u'playlist_name', webpage),
  111. 'entries': entries,
  112. }