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.

82 lines
3.1 KiB

  1. import re
  2. import json
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urllib_request,
  6. ExtractorError,
  7. )
  8. class DailymotionIE(InfoExtractor):
  9. """Information Extractor for Dailymotion"""
  10. _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
  11. IE_NAME = u'dailymotion'
  12. _TEST = {
  13. u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  14. u'file': u'x33vw9.mp4',
  15. u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
  16. u'info_dict': {
  17. u"uploader": u"Alex and Van .",
  18. u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
  19. }
  20. }
  21. def _real_extract(self, url):
  22. # Extract id and simplified title from URL
  23. mobj = re.match(self._VALID_URL, url)
  24. video_id = mobj.group(1).split('_')[0].split('?')[0]
  25. video_extension = 'mp4'
  26. # Retrieve video webpage to extract further information
  27. request = compat_urllib_request.Request(url)
  28. request.add_header('Cookie', 'family_filter=off')
  29. webpage = self._download_webpage(request, video_id)
  30. # Extract URL, uploader and title from webpage
  31. self.report_extraction(video_id)
  32. video_title = self._html_search_regex(r'<meta property="og:title" content="(.*?)" />',
  33. webpage, 'title')
  34. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  35. # Looking for official user
  36. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  37. webpage, 'video uploader')
  38. video_upload_date = None
  39. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  40. if mobj is not None:
  41. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  42. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  43. embed_page = self._download_webpage(embed_url, video_id,
  44. u'Downloading embed page')
  45. info = self._search_regex(r'var info = ({.*?}),', embed_page, 'video info')
  46. info = json.loads(info)
  47. # TODO: support choosing qualities
  48. for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
  49. 'stream_h264_hq_url','stream_h264_url',
  50. 'stream_h264_ld_url']:
  51. if info.get(key):#key in info and info[key]:
  52. max_quality = key
  53. self.to_screen(u'Using %s' % key)
  54. break
  55. else:
  56. raise ExtractorError(u'Unable to extract video URL')
  57. video_url = info[max_quality]
  58. return [{
  59. 'id': video_id,
  60. 'url': video_url,
  61. 'uploader': video_uploader,
  62. 'upload_date': video_upload_date,
  63. 'title': video_title,
  64. 'ext': video_extension,
  65. 'thumbnail': info['thumbnail_url']
  66. }]