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.

234 lines
8.3 KiB

10 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import time
  5. import hashlib
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_str,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. clean_html,
  13. ExtractorError,
  14. int_or_none,
  15. float_or_none,
  16. parse_iso8601,
  17. sanitized_Request,
  18. urlencode_postdata,
  19. )
  20. class NocoIE(InfoExtractor):
  21. _VALID_URL = r'https?://(?:(?:www\.)?noco\.tv/emission/|player\.noco\.tv/\?idvideo=)(?P<id>\d+)'
  22. _LOGIN_URL = 'http://noco.tv/do.php'
  23. _API_URL_TEMPLATE = 'https://api.noco.tv/1.1/%s?ts=%s&tk=%s'
  24. _SUB_LANG_TEMPLATE = '&sub_lang=%s'
  25. _NETRC_MACHINE = 'noco'
  26. _TESTS = [
  27. {
  28. 'url': 'http://noco.tv/emission/11538/nolife/ami-ami-idol-hello-france/',
  29. 'md5': '0a993f0058ddbcd902630b2047ef710e',
  30. 'info_dict': {
  31. 'id': '11538',
  32. 'ext': 'mp4',
  33. 'title': 'Ami Ami Idol - Hello! France',
  34. 'description': 'md5:4eaab46ab68fa4197a317a88a53d3b86',
  35. 'upload_date': '20140412',
  36. 'uploader': 'Nolife',
  37. 'uploader_id': 'NOL',
  38. 'duration': 2851.2,
  39. },
  40. 'skip': 'Requires noco account',
  41. },
  42. {
  43. 'url': 'http://noco.tv/emission/12610/lbl42/the-guild/s01e01-wake-up-call',
  44. 'md5': 'c190f1f48e313c55838f1f412225934d',
  45. 'info_dict': {
  46. 'id': '12610',
  47. 'ext': 'mp4',
  48. 'title': 'The Guild #1 - Wake-Up Call',
  49. 'timestamp': 1403863200,
  50. 'upload_date': '20140627',
  51. 'uploader': 'LBL42',
  52. 'uploader_id': 'LBL',
  53. 'duration': 233.023,
  54. },
  55. 'skip': 'Requires noco account',
  56. }
  57. ]
  58. def _real_initialize(self):
  59. self._login()
  60. def _login(self):
  61. (username, password) = self._get_login_info()
  62. if username is None:
  63. return
  64. login_form = {
  65. 'a': 'login',
  66. 'cookie': '1',
  67. 'username': username,
  68. 'password': password,
  69. }
  70. request = sanitized_Request(self._LOGIN_URL, urlencode_postdata(login_form))
  71. request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
  72. login = self._download_json(request, None, 'Logging in as %s' % username)
  73. if 'erreur' in login:
  74. raise ExtractorError('Unable to login: %s' % clean_html(login['erreur']), expected=True)
  75. @staticmethod
  76. def _ts():
  77. return int(time.time() * 1000)
  78. def _call_api(self, path, video_id, note, sub_lang=None):
  79. ts = compat_str(self._ts() + self._ts_offset)
  80. tk = hashlib.md5((hashlib.md5(ts.encode('ascii')).hexdigest() + '#8S?uCraTedap6a').encode('ascii')).hexdigest()
  81. url = self._API_URL_TEMPLATE % (path, ts, tk)
  82. if sub_lang:
  83. url += self._SUB_LANG_TEMPLATE % sub_lang
  84. request = sanitized_Request(url)
  85. request.add_header('Referer', self._referer)
  86. resp = self._download_json(request, video_id, note)
  87. if isinstance(resp, dict) and resp.get('error'):
  88. self._raise_error(resp['error'], resp['description'])
  89. return resp
  90. def _raise_error(self, error, description):
  91. raise ExtractorError(
  92. '%s returned error: %s - %s' % (self.IE_NAME, error, description),
  93. expected=True)
  94. def _real_extract(self, url):
  95. video_id = self._match_id(url)
  96. # Timestamp adjustment offset between server time and local time
  97. # must be calculated in order to use timestamps closest to server's
  98. # in all API requests (see https://github.com/rg3/youtube-dl/issues/7864)
  99. webpage = self._download_webpage(url, video_id)
  100. player_url = self._search_regex(
  101. r'(["\'])(?P<player>https?://noco\.tv/(?:[^/]+/)+NocoPlayer.+?\.swf.*?)\1',
  102. webpage, 'noco player', group='player',
  103. default='http://noco.tv/cdata/js/player/NocoPlayer-v1.2.40.swf')
  104. qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(player_url).query)
  105. ts = int_or_none(qs.get('ts', [None])[0])
  106. self._ts_offset = ts - self._ts() if ts else 0
  107. self._referer = player_url
  108. medias = self._call_api(
  109. 'shows/%s/medias' % video_id,
  110. video_id, 'Downloading video JSON')
  111. show = self._call_api(
  112. 'shows/by_id/%s' % video_id,
  113. video_id, 'Downloading show JSON')[0]
  114. options = self._call_api(
  115. 'users/init', video_id,
  116. 'Downloading user options JSON')['options']
  117. audio_lang_pref = options.get('audio_language') or options.get('language', 'fr')
  118. if audio_lang_pref == 'original':
  119. audio_lang_pref = show['original_lang']
  120. if len(medias) == 1:
  121. audio_lang_pref = list(medias.keys())[0]
  122. elif audio_lang_pref not in medias:
  123. audio_lang_pref = 'fr'
  124. qualities = self._call_api(
  125. 'qualities',
  126. video_id, 'Downloading qualities JSON')
  127. formats = []
  128. for audio_lang, audio_lang_dict in medias.items():
  129. preference = 1 if audio_lang == audio_lang_pref else 0
  130. for sub_lang, lang_dict in audio_lang_dict['video_list'].items():
  131. for format_id, fmt in lang_dict['quality_list'].items():
  132. format_id_extended = 'audio-%s_sub-%s_%s' % (audio_lang, sub_lang, format_id)
  133. video = self._call_api(
  134. 'shows/%s/video/%s/%s' % (video_id, format_id.lower(), audio_lang),
  135. video_id, 'Downloading %s video JSON' % format_id_extended,
  136. sub_lang if sub_lang != 'none' else None)
  137. file_url = video['file']
  138. if not file_url:
  139. continue
  140. if file_url in ['forbidden', 'not found']:
  141. popmessage = video['popmessage']
  142. self._raise_error(popmessage['title'], popmessage['message'])
  143. formats.append({
  144. 'url': file_url,
  145. 'format_id': format_id_extended,
  146. 'width': int_or_none(fmt.get('res_width')),
  147. 'height': int_or_none(fmt.get('res_lines')),
  148. 'abr': int_or_none(fmt.get('audiobitrate'), 1000),
  149. 'vbr': int_or_none(fmt.get('videobitrate'), 1000),
  150. 'filesize': int_or_none(fmt.get('filesize')),
  151. 'format_note': qualities[format_id].get('quality_name'),
  152. 'quality': qualities[format_id].get('priority'),
  153. 'preference': preference,
  154. })
  155. self._sort_formats(formats)
  156. timestamp = parse_iso8601(show.get('online_date_start_utc'), ' ')
  157. if timestamp is not None and timestamp < 0:
  158. timestamp = None
  159. uploader = show.get('partner_name')
  160. uploader_id = show.get('partner_key')
  161. duration = float_or_none(show.get('duration_ms'), 1000)
  162. thumbnails = []
  163. for thumbnail_key, thumbnail_url in show.items():
  164. m = re.search(r'^screenshot_(?P<width>\d+)x(?P<height>\d+)$', thumbnail_key)
  165. if not m:
  166. continue
  167. thumbnails.append({
  168. 'url': thumbnail_url,
  169. 'width': int(m.group('width')),
  170. 'height': int(m.group('height')),
  171. })
  172. episode = show.get('show_TT') or show.get('show_OT')
  173. family = show.get('family_TT') or show.get('family_OT')
  174. episode_number = show.get('episode_number')
  175. title = ''
  176. if family:
  177. title += family
  178. if episode_number:
  179. title += ' #' + compat_str(episode_number)
  180. if episode:
  181. title += ' - ' + compat_str(episode)
  182. description = show.get('show_resume') or show.get('family_resume')
  183. return {
  184. 'id': video_id,
  185. 'title': title,
  186. 'description': description,
  187. 'thumbnails': thumbnails,
  188. 'timestamp': timestamp,
  189. 'uploader': uploader,
  190. 'uploader_id': uploader_id,
  191. 'duration': duration,
  192. 'formats': formats,
  193. }