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.

305 lines
11 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; ff=off')
  24. return request
  25. class DailymotionIE(DailymotionBaseInfoExtractor):
  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': 'https://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames',
  39. 'md5': '2137c41a8e78554bb09225b8eb322406',
  40. 'info_dict': {
  41. 'id': 'x2iuewm',
  42. 'ext': 'mp4',
  43. 'uploader': 'IGN',
  44. 'title': 'Steam Machine Models, Pricing Listed on Steam Store - IGN News',
  45. 'upload_date': '20150306',
  46. 'duration': 74,
  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 = 'https://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'<meta property="video:release_date" content="([0-9]{4})-([0-9]{2})-([0-9]{2}).+?"/>', webpage)
  96. if mobj is not None:
  97. video_upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3)
  98. embed_url = 'https://www.dailymotion.com/embed/video/%s' % video_id
  99. embed_request = self._build_request(embed_url)
  100. embed_page = self._download_webpage(
  101. embed_request, video_id, 'Downloading embed page')
  102. info = self._search_regex(r'var info = ({.*?}),$', embed_page,
  103. 'video info', flags=re.MULTILINE)
  104. info = json.loads(info)
  105. if info.get('error') is not None:
  106. msg = 'Couldn\'t get video, Dailymotion says: %s' % info['error']['title']
  107. raise ExtractorError(msg, expected=True)
  108. formats = []
  109. for (key, format_id) in self._FORMATS:
  110. video_url = info.get(key)
  111. if video_url is not None:
  112. m_size = re.search(r'H264-(\d+)x(\d+)', video_url)
  113. if m_size is not None:
  114. width, height = map(int_or_none, (m_size.group(1), m_size.group(2)))
  115. else:
  116. width, height = None, None
  117. formats.append({
  118. 'url': video_url,
  119. 'ext': 'mp4',
  120. 'format_id': format_id,
  121. 'width': width,
  122. 'height': height,
  123. })
  124. if not formats:
  125. raise ExtractorError('Unable to extract video URL')
  126. # subtitles
  127. video_subtitles = self.extract_subtitles(video_id, webpage)
  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. 'duration': info['duration']
  147. }
  148. def _get_subtitles(self, video_id, webpage):
  149. try:
  150. sub_list = self._download_webpage(
  151. 'https://api.dailymotion.com/video/%s/subtitles?fields=id,language,url' % video_id,
  152. video_id, note=False)
  153. except ExtractorError as err:
  154. self._downloader.report_warning('unable to download video subtitles: %s' % compat_str(err))
  155. return {}
  156. info = json.loads(sub_list)
  157. if (info['total'] > 0):
  158. sub_lang_list = dict((l['language'], [{'url': l['url'], 'ext': 'srt'}]) for l in info['list'])
  159. return sub_lang_list
  160. self._downloader.report_warning('video doesn\'t have subtitles')
  161. return {}
  162. class DailymotionPlaylistIE(DailymotionBaseInfoExtractor):
  163. IE_NAME = 'dailymotion:playlist'
  164. _VALID_URL = r'(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>.+?)/'
  165. _MORE_PAGES_INDICATOR = r'(?s)<div class="pages[^"]*">.*?<a\s+class="[^"]*?icon-arrow_right[^"]*?"'
  166. _PAGE_TEMPLATE = 'https://www.dailymotion.com/playlist/%s/%s'
  167. _TESTS = [{
  168. 'url': 'http://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3q',
  169. 'info_dict': {
  170. 'title': 'SPORT',
  171. 'id': 'xv4bw_nqtv_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}/(?:(?:old/)?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(
  212. 'https://www.dailymotion.com/user/%s' % user, user)
  213. full_user = unescapeHTML(self._html_search_regex(
  214. r'<a class="nav-image" title="([^"]+)" href="/%s">' % re.escape(user),
  215. webpage, 'user'))
  216. return {
  217. '_type': 'playlist',
  218. 'id': user,
  219. 'title': full_user,
  220. 'entries': self._extract_entries(user),
  221. }
  222. class DailymotionCloudIE(DailymotionBaseInfoExtractor):
  223. _VALID_URL_PREFIX = r'http://api\.dmcloud\.net/(?:player/)?embed/'
  224. _VALID_URL = r'%s[^/]+/(?P<id>[^/?]+)' % _VALID_URL_PREFIX
  225. _VALID_EMBED_URL = r'%s[^/]+/[^\'"]+' % _VALID_URL_PREFIX
  226. _TESTS = [{
  227. # From http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html
  228. # Tested at FranceTvInfo_2
  229. 'url': 'http://api.dmcloud.net/embed/4e7343f894a6f677b10006b4/556e03339473995ee145930c?auth=1464865870-0-jyhsm84b-ead4c701fb750cf9367bf4447167a3db&autoplay=1',
  230. 'only_matching': True,
  231. }, {
  232. # http://www.francetvinfo.fr/societe/larguez-les-amarres-le-cobaturage-se-developpe_980101.html
  233. 'url': 'http://api.dmcloud.net/player/embed/4e7343f894a6f677b10006b4/559545469473996d31429f06?auth=1467430263-0-90tglw2l-a3a4b64ed41efe48d7fccad85b8b8fda&autoplay=1',
  234. 'only_matching': True,
  235. }]
  236. @classmethod
  237. def _extract_dmcloud_url(self, webpage):
  238. mobj = re.search(r'<iframe[^>]+src=[\'"](%s)[\'"]' % self._VALID_EMBED_URL, webpage)
  239. if mobj:
  240. return mobj.group(1)
  241. mobj = re.search(
  242. r'<input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^>]+value=[\'"](%s)[\'"]' % self._VALID_EMBED_URL,
  243. webpage)
  244. if mobj:
  245. return mobj.group(1)
  246. def _real_extract(self, url):
  247. video_id = self._match_id(url)
  248. request = self._build_request(url)
  249. webpage = self._download_webpage(request, video_id)
  250. title = self._html_search_regex(r'<title>([^>]+)</title>', webpage, 'title')
  251. video_info = self._parse_json(self._search_regex(
  252. r'var\s+info\s*=\s*([^;]+);', webpage, 'video info'), video_id)
  253. # TODO: parse ios_url, which is in fact a manifest
  254. video_url = video_info['mp4_url']
  255. return {
  256. 'id': video_id,
  257. 'url': video_url,
  258. 'title': title,
  259. 'thumbnail': video_info.get('thumbnail_url'),
  260. }