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.

252 lines
9.0 KiB

10 years ago
10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import itertools
  6. from .common import InfoExtractor
  7. from .subtitles import SubtitlesInfoExtractor
  8. from ..utils import (
  9. compat_urllib_request,
  10. compat_str,
  11. orderedSet,
  12. str_to_int,
  13. int_or_none,
  14. ExtractorError,
  15. unescapeHTML,
  16. )
  17. class DailymotionBaseInfoExtractor(InfoExtractor):
  18. @staticmethod
  19. def _build_request(url):
  20. """Build a request with the family filter disabled"""
  21. request = compat_urllib_request.Request(url)
  22. request.add_header('Cookie', 'family_filter=off')
  23. request.add_header('Cookie', 'ff=off')
  24. return request
  25. class DailymotionIE(DailymotionBaseInfoExtractor, SubtitlesInfoExtractor):
  26. """Information Extractor for Dailymotion"""
  27. _VALID_URL = r'(?i)(?:https?://)?(?:(www|touch)\.)?dailymotion\.[a-z]{2,3}/(?:(embed|#)/)?video/(?P<id>[^/?_]+)'
  28. IE_NAME = 'dailymotion'
  29. _FORMATS = [
  30. ('stream_h264_ld_url', 'ld'),
  31. ('stream_h264_url', 'standard'),
  32. ('stream_h264_hq_url', 'hq'),
  33. ('stream_h264_hd_url', 'hd'),
  34. ('stream_h264_hd1080_url', 'hd180'),
  35. ]
  36. _TESTS = [
  37. {
  38. 'url': 'http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech',
  39. 'md5': '392c4b85a60a90dc4792da41ce3144eb',
  40. 'info_dict': {
  41. 'id': 'x33vw9',
  42. 'ext': 'mp4',
  43. 'uploader': 'Amphora Alex and Van .',
  44. 'title': 'Tutoriel de Youtubeur"DL DES VIDEO DE YOUTUBE"',
  45. }
  46. },
  47. # Vevo video
  48. {
  49. 'url': 'http://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musi',
  50. 'info_dict': {
  51. 'title': 'Roar (Official)',
  52. 'id': 'USUV71301934',
  53. 'ext': 'mp4',
  54. 'uploader': 'Katy Perry',
  55. 'upload_date': '20130905',
  56. },
  57. 'params': {
  58. 'skip_download': True,
  59. },
  60. 'skip': 'VEVO is only available in some countries',
  61. },
  62. # age-restricted video
  63. {
  64. 'url': 'http://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband',
  65. 'md5': '0d667a7b9cebecc3c89ee93099c4159d',
  66. 'info_dict': {
  67. 'id': 'xyh2zz',
  68. 'ext': 'mp4',
  69. 'title': 'Leanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]',
  70. 'uploader': 'HotWaves1012',
  71. 'age_limit': 18,
  72. }
  73. }
  74. ]
  75. def _real_extract(self, url):
  76. video_id = self._match_id(url)
  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[^"]*?video=(?P<id>[\w]*)',
  86. webpage)
  87. if m_vevo is not None:
  88. vevo_id = m_vevo.group('id')
  89. self.to_screen('Vevo video detected: %s' % vevo_id)
  90. return self.url_result('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. '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('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 = str_to_int(self._search_regex(
  129. r'video_views_count[^>]+>\s+([\d\.,]+)',
  130. webpage, 'view count', fatal=False))
  131. title = self._og_search_title(webpage, default=None)
  132. if title is None:
  133. title = self._html_search_regex(
  134. r'(?s)<span\s+id="video_title"[^>]*>(.*?)</span>', webpage,
  135. 'title')
  136. return {
  137. 'id': video_id,
  138. 'formats': formats,
  139. 'uploader': info['owner.screenname'],
  140. 'upload_date': video_upload_date,
  141. 'title': title,
  142. 'subtitles': video_subtitles,
  143. 'thumbnail': info['thumbnail_url'],
  144. 'age_limit': age_limit,
  145. 'view_count': view_count,
  146. }
  147. def _get_available_subtitles(self, video_id, webpage):
  148. try:
  149. sub_list = self._download_webpage(
  150. 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
  151. video_id, note=False)
  152. except ExtractorError as err:
  153. self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
  154. return {}
  155. info = json.loads(sub_list)
  156. if (info['total'] > 0):
  157. sub_lang_list = dict((l['language'], l['url']) for l in info['list'])
  158. return sub_lang_list
  159. self._downloader.report_warning('video doesn\'t have subtitles')
  160. return {}
  161. class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
  162. IE_NAME = 'dailymotion:playlist'
  163. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  164. _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
  165. _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
  166. _TESTS = [{
  167. 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
  168. 'info_dict': {
  169. 'title': 'SPORT',
  170. },
  171. 'playlist_mincount': 20,
  172. }]
  173. def _extract_entries(self, id):
  174. video_ids = []
  175. for pagenum in itertools.count(1):
  176. request = self._build_request(self._PAGE_TEMPLATE % (id, pagenum))
  177. webpage = self._download_webpage(request,
  178. id, 'Downloading page %s' % pagenum)
  179. video_ids.extend(re.findall(r'data-xid="(.+?)"', webpage))
  180. if re.search(self._MORE_PAGES_INDICATOR, webpage) is None:
  181. break
  182. return [self.url_result('http://www.dailymotion.com/video/%s' % video_id, 'Dailymotion')
  183. for video_id in orderedSet(video_ids)]
  184. def _real_extract(self, url):
  185. mobj = re.match(self._VALID_URL, url)
  186. playlist_id = mobj.group('id')
  187. webpage = self._download_webpage(url, playlist_id)
  188. return {
  189. '_type': 'playlist',
  190. 'id': playlist_id,
  191. 'title': self._og_search_title(webpage),
  192. 'entries': self._extract_entries(playlist_id),
  193. }
  194. class DailymotionUserIE(DailymotionPlaylistIE):
  195. IE_NAME = 'dailymotion:user'
  196. _VALID_URL = r'https?://(?:www\.)?dailymotion\.[a-z]{2,3}/user/(?P<user>[^/]+)'
  197. _PAGE_TEMPLATE = 'http://www.dailymotion.com/user/%s/%s'
  198. _TESTS = [{
  199. 'url': 'https://www.dailymotion.com/user/nqtv',
  200. 'info_dict': {
  201. 'id': 'nqtv',
  202. 'title': 'Rémi Gaillard',
  203. },
  204. 'playlist_mincount': 100,
  205. }]
  206. def _real_extract(self, url):
  207. mobj = re.match(self._VALID_URL, url)
  208. user = mobj.group('user')
  209. webpage = self._download_webpage(url, user)
  210. full_user = unescapeHTML(self._html_search_regex(
  211. r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
  212. webpage, 'user'))
  213. return {
  214. '_type': 'playlist',
  215. 'id': user,
  216. 'title': full_user,
  217. 'entries': self._extract_entries(user),
  218. }