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.

79 lines
2.9 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_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  33. # Looking for official user
  34. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  35. webpage, 'video uploader')
  36. video_upload_date = None
  37. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  38. if mobj is not None:
  39. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  40. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  41. embed_page = self._download_webpage(embed_url, video_id,
  42. u'Downloading embed page')
  43. info = self._search_regex(r'var info = ({.*?}),', embed_page, 'video info')
  44. info = json.loads(info)
  45. # TODO: support choosing qualities
  46. for key in ['stream_h264_hd1080_url','stream_h264_hd_url',
  47. 'stream_h264_hq_url','stream_h264_url',
  48. 'stream_h264_ld_url']:
  49. if info.get(key):#key in info and info[key]:
  50. max_quality = key
  51. self.to_screen(u'Using %s' % key)
  52. break
  53. else:
  54. raise ExtractorError(u'Unable to extract video URL')
  55. video_url = info[max_quality]
  56. return [{
  57. 'id': video_id,
  58. 'url': video_url,
  59. 'uploader': video_uploader,
  60. 'upload_date': video_upload_date,
  61. 'title': self._og_search_title(webpage),
  62. 'ext': video_extension,
  63. 'thumbnail': info['thumbnail_url']
  64. }]