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.

77 lines
2.8 KiB

  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. compat_urllib_request,
  5. compat_urllib_parse,
  6. ExtractorError,
  7. unescapeHTML,
  8. )
  9. class DailymotionIE(InfoExtractor):
  10. """Information Extractor for Dailymotion"""
  11. _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
  12. IE_NAME = u'dailymotion'
  13. def _real_extract(self, url):
  14. # Extract id and simplified title from URL
  15. mobj = re.match(self._VALID_URL, url)
  16. video_id = mobj.group(1).split('_')[0].split('?')[0]
  17. video_extension = 'mp4'
  18. # Retrieve video webpage to extract further information
  19. request = compat_urllib_request.Request(url)
  20. request.add_header('Cookie', 'family_filter=off')
  21. webpage = self._download_webpage(request, video_id)
  22. # Extract URL, uploader and title from webpage
  23. self.report_extraction(video_id)
  24. mobj = re.search(r'\s*var flashvars = (.*)', webpage)
  25. if mobj is None:
  26. raise ExtractorError(u'Unable to extract media URL')
  27. flashvars = compat_urllib_parse.unquote(mobj.group(1))
  28. for key in ['hd1080URL', 'hd720URL', 'hqURL', 'sdURL', 'ldURL', 'video_url']:
  29. if key in flashvars:
  30. max_quality = key
  31. self.to_screen(u'Using %s' % key)
  32. break
  33. else:
  34. raise ExtractorError(u'Unable to extract video URL')
  35. mobj = re.search(r'"' + max_quality + r'":"(.+?)"', flashvars)
  36. if mobj is None:
  37. raise ExtractorError(u'Unable to extract video URL')
  38. video_url = compat_urllib_parse.unquote(mobj.group(1)).replace('\\/', '/')
  39. # TODO: support choosing qualities
  40. mobj = re.search(r'<meta property="og:title" content="(?P<title>[^"]*)" />', webpage)
  41. if mobj is None:
  42. raise ExtractorError(u'Unable to extract title')
  43. video_title = unescapeHTML(mobj.group('title'))
  44. video_uploader = None
  45. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  46. # Looking for official user
  47. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  48. webpage, 'video uploader')
  49. video_upload_date = None
  50. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  51. if mobj is not None:
  52. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  53. return [{
  54. 'id': video_id,
  55. 'url': video_url,
  56. 'uploader': video_uploader,
  57. 'upload_date': video_upload_date,
  58. 'title': video_title,
  59. 'ext': video_extension,
  60. }]