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.

112 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}/(?:embed/)?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. url = 'http://www.dailymotion.com/video/%s' % video_id
  30. # Retrieve video webpage to extract further information
  31. request = compat_urllib_request.Request(url)
  32. request.add_header('Cookie', 'family_filter=off')
  33. webpage = self._download_webpage(request, video_id)
  34. # Extract URL, uploader and title from webpage
  35. self.report_extraction(video_id)
  36. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  37. # Looking for official user
  38. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  39. webpage, 'video uploader')
  40. video_upload_date = None
  41. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  42. if mobj is not None:
  43. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  44. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  45. embed_page = self._download_webpage(embed_url, video_id,
  46. u'Downloading embed page')
  47. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  48. 'video info', flags=re.MULTILINE)
  49. info = json.loads(info)
  50. # TODO: support choosing qualities
  51. for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
  52. 'stream_h264_hq_url','stream_h264_url',
  53. 'stream_h264_ld_url']:
  54. if info.get(key):#key in info and info[key]:
  55. max_quality = key
  56. self.to_screen(u'Using %s' % key)
  57. break
  58. else:
  59. raise ExtractorError(u'Unable to extract video URL')
  60. video_url = info[max_quality]
  61. return [{
  62. 'id': video_id,
  63. 'url': video_url,
  64. 'uploader': video_uploader,
  65. 'upload_date': video_upload_date,
  66. 'title': self._og_search_title(webpage),
  67. 'ext': video_extension,
  68. 'thumbnail': info['thumbnail_url']
  69. }]
  70. class DailymotionPlaylistIE(InfoExtractor):
  71. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  72. _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
  73. def _real_extract(self, url):
  74. mobj = re.match(self._VALID_URL, url)
  75. playlist_id = mobj.group('id')
  76. video_ids = []
  77. for pagenum in itertools.count(1):
  78. webpage = self._download_webpage('https://www.dailymotion.com/playlist/%s/%s' % (playlist_id, pagenum),
  79. playlist_id, u'Downloading page %s' % pagenum)
  80. playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
  81. video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
  82. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  83. break
  84. entries = [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  85. for video_id in video_ids]
  86. return {'_type': 'playlist',
  87. 'id': playlist_id,
  88. 'title': get_element_by_id(u'playlist_name', webpage),
  89. 'entries': entries,
  90. }