dailymotion.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import re
  2. import json
  3. import itertools
  4. from .common import InfoExtractor
  5. from .subtitles import SubtitlesInfoExtractor
  6. from ..utils import (
  7. compat_urllib_request,
  8. compat_str,
  9. orderedSet,
  10. str_to_int,
  11. int_or_none,
  12. ExtractorError,
  13. unescapeHTML,
  14. )
  15. class DailymotionBaseInfoExtractor(InfoExtractor):
  16. @staticmethod
  17. def _build_request(url):
  18. """Build a request with the family filter disabled"""
  19. request = compat_urllib_request.Request(url)
  20. request.add_header('Cookie', 'family_filter=off')
  21. request.add_header('Cookie', 'ff=off')
  22. return request
  23. class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
  24. """Information Extractor for Dailymotion"""
  25. _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
  26. IE_NAME = 'dailymotion'
  27. _FORMATS = [
  28. ('stream_h264_ld_url', 'ld'),
  29. ('stream_h264_url', 'standard'),
  30. ('stream_h264_hq_url', 'hq'),
  31. ('stream_h264_hd_url', 'hd'),
  32. ('stream_h264_hd1080_url', 'hd180'),
  33. ]
  34. _TESTS = [
  35. {
  36. 'url': 'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  37. 'md5': '392c4b85a60a90dc4792da41ce3144eb',
  38. 'info_dict': {
  39. 'id': 'x33vw9',
  40. 'ext': 'mp4',
  41. 'uploader': 'Amphora Alex and Van .',
  42. 'title': 'Tutoriel de Youtubeur"DL DES VIDEO DE YOUTUBE"',
  43. }
  44. },
  45. # Vevo video
  46. {
  47. 'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
  48. 'info_dict': {
  49. 'title': 'Roar (Official)',
  50. 'id': 'USUV71301934',
  51. 'ext': 'mp4',
  52. 'uploader': 'Katy Perry',
  53. 'upload_date': '20130905',
  54. },
  55. 'params': {
  56. 'skip_download': True,
  57. },
  58. 'skip': 'VEVO is only available in some countries',
  59. },
  60. # age-restricted video
  61. {
  62. 'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband',
  63. 'md5': '0d667a7b9cebecc3c89ee93099c4159d',
  64. 'info_dict': {
  65. 'id': 'xyh2zz',
  66. 'ext': 'mp4',
  67. 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]',
  68. 'uploader': 'HotWaves1012',
  69. 'age_limit': 18,
  70. }
  71. }
  72. ]
  73. def _real_extract(self, url):
  74. # Extract id and simplified title from URL
  75. mobj = re.match(self._VALID_URL, url)
  76. video_id = mobj.group('id')
  77. url = 'http://www.dailymotion.com/video/%s' % video_id
  78. # Retrieve video webpage to extract further information
  79. request = self._build_request(url)
  80. webpage = self._download_webpage(request, video_id)
  81. # Extract URL, uploader and title from webpage
  82. self.report_extraction(video_id)
  83. # It may just embed a vevo video:
  84. m_vevo = re.search(
  85. r'<link rel="video_src" href="[^"]*?vevo.com[^"]*?videoId=(?P<id>[\w]*)',
  86. webpage)
  87. if m_vevo is not None:
  88. vevo_id = m_vevo.group('id')
  89. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  90. return self.url_result(u'vevo:%s' % vevo_id, ie='Vevo')
  91. age_limit = self._rta_search(webpage)
  92. video_upload_date = None
  93. mobj = re.search(r'<div class="[^"]*uploaded_cont[^"]*" title="[^"]*">([0-9]{2})-([0-9]{2})-([0-9]{4})</div>', webpage)
  94. if mobj is not None:
  95. video_upload_date = mobj.group(3) + mobj.group(2) + mobj.group(1)
  96. embed_url = 'http://www.dailymotion.com/embed/video/%s' % video_id
  97. embed_page = self._download_webpage(embed_url, video_id,
  98. u'Downloading embed page')
  99. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  100. 'video info', flags=re.MULTILINE)
  101. info = json.loads(info)
  102. if info.get('error') is not None:
  103. msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
  104. raise ExtractorError(msg, expected=True)
  105. formats = []
  106. for (key, format_id) in self._FORMATS:
  107. video_url = info.get(key)
  108. if video_url is not None:
  109. m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
  110. if m_size is not None:
  111. width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
  112. else:
  113. width, height = None, None
  114. formats.append({
  115. 'url': video_url,
  116. 'ext': 'mp4',
  117. 'format_id': format_id,
  118. 'width': width,
  119. 'height': height,
  120. })
  121. if not formats:
  122. raise ExtractorError(u'Unable to extract video URL')
  123. # subtitles
  124. video_subtitles = self.extract_subtitles(video_id, webpage)
  125. if self._downloader.params.get('listsubtitles', False):
  126. self._list_available_subtitles(video_id, webpage)
  127. return
  128. view_count = self._search_regex(
  129. r'video_views_count[^>]+>\s+([\d\.,]+)', webpage, u'view count', fatal=False)
  130. if view_count is not None:
  131. view_count = str_to_int(view_count)
  132. return {
  133. 'id': video_id,
  134. 'formats': formats,
  135. 'uploader': info['owner.screenname'],
  136. 'upload_date': video_upload_date,
  137. 'title': self._og_search_title(webpage),
  138. 'subtitles': video_subtitles,
  139. 'thumbnail': info['thumbnail_url'],
  140. 'age_limit': age_limit,
  141. 'view_count': view_count,
  142. }
  143. def _get_available_subtitles(self, video_id, webpage):
  144. try:
  145. sub_list = self._download_webpage(
  146. 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
  147. video_id, note=False)
  148. except ExtractorError as err:
  149. self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
  150. return {}
  151. info = json.loads(sub_list)
  152. if (info['total'] > 0):
  153. sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
  154. return sub_lang_list
  155. self._downloader.report_warning(u'video doesn\'t have subtitles')
  156. return {}
  157. class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
  158. IE_NAME = u'dailymotion:playlist'
  159. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  160. _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
  161. _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
  162. def _extract_entries(self, id):
  163. video_ids = []
  164. for pagenum in itertools.count(1):
  165. request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
  166. webpage = self._download_webpage(request,
  167. id, u'Downloading page %s' % pagenum)
  168. video_ids.extend(re.findall(r'data-xid="(.+?)"', webpage))
  169. if re.search(self._MORE_PAGES_INDICATOR, webpage) is None:
  170. break
  171. return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  172. for video_id in orderedSet(video_ids)]
  173. def _real_extract(self, url):
  174. mobj = re.match(self._VALID_URL, url)
  175. playlist_id = mobj.group('id')
  176. webpage = self._download_webpage(url, playlist_id)
  177. return {
  178. '_type': 'playlist',
  179. 'id': playlist_id,
  180. 'title': self._og_search_title(webpage),
  181. 'entries': self._extract_entries(playlist_id),
  182. }
  183. class DailymotionUserIE(DailymotionPlaylistIE):
  184. IE_NAME = u'dailymotion:user'
  185. _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
  186. _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
  187. def _real_extract(self, url):
  188. mobj = re.match(self._VALID_URL, url)
  189. user = mobj.group('user')
  190. webpage = self._download_webpage(url, user)
  191. full_user = unescapeHTML(self._html_search_regex(
  192. r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
  193. webpage, u'user', flags=re.DOTALL))
  194. return {
  195. '_type': 'playlist',
  196. 'id': user,
  197. 'title': full_user,
  198. 'entries': self._extract_entries(user),
  199. }