dailymotion.py 3.2 KB

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