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.

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