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.

111 lines
4.3 KiB

  1. import re
  2. import json
  3. import itertools
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urllib_request,
  7. get_element_by_attribute,
  8. get_element_by_id,
  9. ExtractorError,
  10. )
  11. class DailymotionIE(InfoExtractor):
  12. """Information Extractor for Dailymotion"""
  13. _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
  14. IE_NAME = u'dailymotion'
  15. _TEST = {
  16. u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  17. u'file': u'x33vw9.mp4',
  18. u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
  19. u'info_dict': {
  20. u"uploader": u"Amphora Alex and Van .",
  21. u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
  22. }
  23. }
  24. def _real_extract(self, url):
  25. # Extract id and simplified title from URL
  26. mobj = re.match(self._VALID_URL, url)
  27. video_id = mobj.group(1).split('_')[0].split('?')[0]
  28. video_extension = 'mp4'
  29. # Retrieve video webpage to extract further information
  30. request = compat_urllib_request.Request(url)
  31. request.add_header('Cookie', 'family_filter=off')
  32. webpage = self._download_webpage(request, video_id)
  33. # Extract URL, uploader and title from webpage
  34. self.report_extraction(video_id)
  35. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  36. # Looking for official user
  37. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  38. webpage, 'video uploader')
  39. video_upload_date = None
  40. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  41. if mobj is not None:
  42. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  43. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  44. embed_page = self._download_webpage(embed_url, video_id,
  45. u'Downloading embed page')
  46. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  47. 'video info', flags=re.MULTILINE)
  48. info = json.loads(info)
  49. # TODO: support choosing qualities
  50. for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
  51. 'stream_h264_hq_url','stream_h264_url',
  52. 'stream_h264_ld_url']:
  53. if info.get(key):#key in info and info[key]:
  54. max_quality = key
  55. self.to_screen(u'Using %s' % key)
  56. break
  57. else:
  58. raise ExtractorError(u'Unable to extract video URL')
  59. video_url = info[max_quality]
  60. return [{
  61. 'id': video_id,
  62. 'url': video_url,
  63. 'uploader': video_uploader,
  64. 'upload_date': video_upload_date,
  65. 'title': self._og_search_title(webpage),
  66. 'ext': video_extension,
  67. 'thumbnail': info['thumbnail_url']
  68. }]
  69. class DailymotionPlaylistIE(InfoExtractor):
  70. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  71. _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
  72. def _real_extract(self, url):
  73. mobj = re.match(self._VALID_URL, url)
  74. playlist_id = mobj.group('id')
  75. video_ids = []
  76. for pagenum in itertools.count(1):
  77. webpage = self._download_webpage('https://www.dailymotion.com/playlist/%s/%s' % (playlist_id, pagenum),
  78. playlist_id, u'Downloading page %s' % pagenum)
  79. playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
  80. video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
  81. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  82. break
  83. entries = [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  84. for video_id in video_ids]
  85. return {'_type': 'playlist',
  86. 'id': playlist_id,
  87. 'title': get_element_by_id(u'playlist_name', webpage),
  88. 'entries': entries,
  89. }