dailymotion.py 5.8 KB

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