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.

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