dailymotion.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import re
  2. import json
  3. import socket
  4. from .common import InfoExtractor
  5. from .subtitles import SubtitlesIE
  6. from ..utils import (
  7. compat_http_client,
  8. compat_urllib_error,
  9. compat_urllib_request,
  10. compat_str,
  11. get_element_by_attribute,
  12. get_element_by_id,
  13. ExtractorError,
  14. )
  15. class DailyMotionSubtitlesIE(SubtitlesIE):
  16. def _get_available_subtitles(self, video_id):
  17. request = compat_urllib_request.Request('https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id)
  18. try:
  19. sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
  20. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  21. self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
  22. return {}
  23. info = json.loads(sub_list)
  24. if (info['total'] > 0):
  25. sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
  26. return sub_lang_list
  27. self._downloader.report_warning(u'video doesn\'t have subtitles')
  28. return {}
  29. def _request_automatic_caption(self, video_id, webpage):
  30. self._downloader.report_warning(u'Automatic Captions not supported by this server')
  31. return {}
  32. class DailymotionIE(DailyMotionSubtitlesIE):
  33. """Information Extractor for Dailymotion"""
  34. _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/video/([^/]+)'
  35. IE_NAME = u'dailymotion'
  36. _TEST = {
  37. u'url': u'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  38. u'file': u'x33vw9.mp4',
  39. u'md5': u'392c4b85a60a90dc4792da41ce3144eb',
  40. u'info_dict': {
  41. u"uploader": u"Alex and Van .",
  42. u"title": u"Tutoriel de Youtubeur\"DL DES VIDEO DE YOUTUBE\""
  43. }
  44. }
  45. def _real_extract(self, url):
  46. # Extract id and simplified title from URL
  47. mobj = re.match(self._VALID_URL, url)
  48. video_id = mobj.group(1).split('_')[0].split('?')[0]
  49. video_extension = 'mp4'
  50. # Retrieve video webpage to extract further information
  51. request = compat_urllib_request.Request(url)
  52. request.add_header('Cookie', 'family_filter=off')
  53. webpage = self._download_webpage(request, video_id)
  54. # Extract URL, uploader and title from webpage
  55. self.report_extraction(video_id)
  56. video_uploader = self._search_regex([r'(?im)<span class="owner[^\"]+?">[^<]+?<a [^>]+?>([^<]+?)</a>',
  57. # Looking for official user
  58. r'<(?:span|a) .*?rel="author".*?>([^<]+?)</'],
  59. webpage, 'video uploader')
  60. video_upload_date = None
  61. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  62. if mobj is not None:
  63. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  64. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  65. embed_page = self._download_webpage(embed_url, video_id,
  66. u'Downloading embed page')
  67. info = self._search_regex(r'var info = ({.*?}),', embed_page, 'video info')
  68. info = json.loads(info)
  69. # TODO: support choosing qualities
  70. for key in ['stream_h264_hd1080_url', 'stream_h264_hd_url',
  71. 'stream_h264_hq_url', 'stream_h264_url',
  72. 'stream_h264_ld_url']:
  73. if info.get(key): # key in info and info[key]:
  74. max_quality = key
  75. self.to_screen(u'%s: Using %s' % (video_id, key))
  76. break
  77. else:
  78. raise ExtractorError(u'Unable to extract video URL')
  79. video_url = info[max_quality]
  80. # subtitles
  81. video_subtitles = None
  82. video_webpage = None
  83. if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
  84. video_subtitles = self._extract_subtitles(video_id)
  85. elif self._downloader.params.get('writeautomaticsub', False):
  86. video_subtitles = self._request_automatic_caption(video_id, video_webpage)
  87. if self._downloader.params.get('listsubtitles', False):
  88. self._list_available_subtitles(video_id)
  89. return
  90. return [{
  91. 'id': video_id,
  92. 'url': video_url,
  93. 'uploader': video_uploader,
  94. 'upload_date': video_upload_date,
  95. 'title': self._og_search_title(webpage),
  96. 'ext': video_extension,
  97. 'subtitles': video_subtitles,
  98. 'thumbnail': info['thumbnail_url']
  99. }]