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.

110 lines
4.2 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"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, 'video info')
  47. info = json.loads(info)
  48. # TODO: support choosing qualities
  49. for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
  50. 'stream_h264_hq_url','stream_h264_url',
  51. 'stream_h264_ld_url']:
  52. if info.get(key):#key in info and info[key]:
  53. max_quality = key
  54. self.to_screen(u'Using %s' % key)
  55. break
  56. else:
  57. raise ExtractorError(u'Unable to extract video URL')
  58. video_url = info[max_quality]
  59. return [{
  60. 'id': video_id,
  61. 'url': video_url,
  62. 'uploader': video_uploader,
  63. 'upload_date': video_upload_date,
  64. 'title': self._og_search_title(webpage),
  65. 'ext': video_extension,
  66. 'thumbnail': info['thumbnail_url']
  67. }]
  68. class DailymotionPlaylistIE(InfoExtractor):
  69. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  70. _MORE_PAGES_INDICATOR = r'<div class="next">.*?<a.*?href="/playlist/.+?".*?>.*?</a>.*?</div>'
  71. def _real_extract(self, url):
  72. mobj = re.match(self._VALID_URL, url)
  73. playlist_id = mobj.group('id')
  74. video_ids = []
  75. for pagenum in itertools.count(1):
  76. webpage = self._download_webpage('https://www.dailymotion.com/playlist/%s/%s' % (playlist_id, pagenum),
  77. playlist_id, u'Downloading page %s' % pagenum)
  78. playlist_el = get_element_by_attribute(u'class', u'video_list', webpage)
  79. video_ids.extend(re.findall(r'data-id="(.+?)" data-ext-id', playlist_el))
  80. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  81. break
  82. entries = [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  83. for video_id in video_ids]
  84. return {'_type': 'playlist',
  85. 'id': playlist_id,
  86. 'title': get_element_by_id(u'playlist_name', webpage),
  87. 'entries': entries,
  88. }