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.

201 lines
6.8 KiB

11 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_urllib_parse,
  10. compat_urllib_request,
  11. )
  12. from ..utils import (
  13. clean_html,
  14. ExtractorError,
  15. unified_strdate,
  16. )
  17. class NocoIE(InfoExtractor):
  18. _VALID_URL = r'http://(?:(?:www\.)?noco\.tv/emission/|player\.noco\.tv/\?idvideo=)(?P<id>\d+)'
  19. _LOGIN_URL = 'http://noco.tv/do.php'
  20. _API_URL_TEMPLATE = 'https://api.noco.tv/1.1/%s?ts=%s&tk=%s'
  21. _SUB_LANG_TEMPLATE = '&sub_lang=%s'
  22. _NETRC_MACHINE = 'noco'
  23. _TESTS = [
  24. {
  25. 'url': 'http://noco.tv/emission/11538/nolife/ami-ami-idol-hello-france/',
  26. 'md5': '0a993f0058ddbcd902630b2047ef710e',
  27. 'info_dict': {
  28. 'id': '11538',
  29. 'ext': 'mp4',
  30. 'title': 'Ami Ami Idol - Hello! France',
  31. 'description': 'md5:4eaab46ab68fa4197a317a88a53d3b86',
  32. 'upload_date': '20140412',
  33. 'uploader': 'Nolife',
  34. 'uploader_id': 'NOL',
  35. 'duration': 2851.2,
  36. },
  37. 'skip': 'Requires noco account',
  38. },
  39. {
  40. 'url': 'http://noco.tv/emission/12610/lbl42/the-guild/s01e01-wake-up-call',
  41. 'md5': 'c190f1f48e313c55838f1f412225934d',
  42. 'info_dict': {
  43. 'id': '12610',
  44. 'ext': 'mp4',
  45. 'title': 'The Guild #1 - Wake-Up Call',
  46. 'description': '',
  47. 'upload_date': '20140627',
  48. 'uploader': 'LBL42',
  49. 'uploader_id': 'LBL',
  50. 'duration': 233.023,
  51. },
  52. 'skip': 'Requires noco account',
  53. }
  54. ]
  55. def _real_initialize(self):
  56. self._login()
  57. def _login(self):
  58. (username, password) = self._get_login_info()
  59. if username is None:
  60. return
  61. login_form = {
  62. 'a': 'login',
  63. 'cookie': '1',
  64. 'username': username,
  65. 'password': password,
  66. }
  67. request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
  68. request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
  69. login = self._download_json(request, None, 'Logging in as %s' % username)
  70. if 'erreur' in login:
  71. raise ExtractorError('Unable to login: %s' % clean_html(login['erreur']), expected=True)
  72. def _call_api(self, path, video_id, note, sub_lang=None):
  73. ts = compat_str(int(time.time() * 1000))
  74. tk = hashlib.md5((hashlib.md5(ts.encode('ascii')).hexdigest() + '#8S?uCraTedap6a').encode('ascii')).hexdigest()
  75. url = self._API_URL_TEMPLATE % (path, ts, tk)
  76. if sub_lang:
  77. url += self._SUB_LANG_TEMPLATE % sub_lang
  78. resp = self._download_json(url, video_id, note)
  79. if isinstance(resp, dict) and resp.get('error'):
  80. self._raise_error(resp['error'], resp['description'])
  81. return resp
  82. def _raise_error(self, error, description):
  83. raise ExtractorError(
  84. '%s returned error: %s - %s' % (self.IE_NAME, error, description),
  85. expected=True)
  86. def _real_extract(self, url):
  87. mobj = re.match(self._VALID_URL, url)
  88. video_id = mobj.group('id')
  89. options = self._call_api('users/init', None, 'Downloading user options JSON')['options']
  90. audio_lang = options.get('audio_language', 'fr')
  91. medias = self._call_api(
  92. 'shows/%s/medias' % video_id,
  93. video_id, 'Downloading video JSON')
  94. show = self._call_api(
  95. 'shows/by_id/%s' % video_id,
  96. video_id, 'Downloading show JSON')[0]
  97. if audio_lang == 'original':
  98. audio_lang = show['original_lang']
  99. if len(medias) == 1:
  100. audio_lang = list(medias.keys())[0]
  101. elif not audio_lang in medias:
  102. audio_lang = 'fr'
  103. qualities = self._call_api(
  104. 'qualities',
  105. video_id, 'Downloading qualities JSON')
  106. formats = []
  107. for lang, lang_dict in medias[audio_lang]['video_list'].items():
  108. for format_id, fmt in lang_dict['quality_list'].items():
  109. format_id_extended = '%s-%s' % (lang, format_id) if lang != 'none' else format_id
  110. video = self._call_api(
  111. 'shows/%s/video/%s/%s' % (video_id, format_id.lower(), audio_lang),
  112. video_id, 'Downloading %s video JSON' % format_id_extended,
  113. lang if lang != 'none' else None)
  114. file_url = video['file']
  115. if not file_url:
  116. continue
  117. if file_url in ['forbidden', 'not found']:
  118. popmessage = video['popmessage']
  119. self._raise_error(popmessage['title'], popmessage['message'])
  120. formats.append({
  121. 'url': file_url,
  122. 'format_id': format_id_extended,
  123. 'width': fmt['res_width'],
  124. 'height': fmt['res_lines'],
  125. 'abr': fmt['audiobitrate'],
  126. 'vbr': fmt['videobitrate'],
  127. 'filesize': fmt['filesize'],
  128. 'format_note': qualities[format_id]['quality_name'],
  129. 'preference': qualities[format_id]['priority'],
  130. })
  131. self._sort_formats(formats)
  132. upload_date = unified_strdate(show['online_date_start_utc'])
  133. uploader = show['partner_name']
  134. uploader_id = show['partner_key']
  135. duration = show['duration_ms'] / 1000.0
  136. thumbnails = []
  137. for thumbnail_key, thumbnail_url in show.items():
  138. m = re.search(r'^screenshot_(?P<width>\d+)x(?P<height>\d+)$', thumbnail_key)
  139. if not m:
  140. continue
  141. thumbnails.append({
  142. 'url': thumbnail_url,
  143. 'width': int(m.group('width')),
  144. 'height': int(m.group('height')),
  145. })
  146. episode = show.get('show_TT') or show.get('show_OT')
  147. family = show.get('family_TT') or show.get('family_OT')
  148. episode_number = show.get('episode_number')
  149. title = ''
  150. if family:
  151. title += family
  152. if episode_number:
  153. title += ' #' + compat_str(episode_number)
  154. if episode:
  155. title += ' - ' + episode
  156. description = show.get('show_resume') or show.get('family_resume')
  157. return {
  158. 'id': video_id,
  159. 'title': title,
  160. 'description': description,
  161. 'thumbnails': thumbnails,
  162. 'upload_date': upload_date,
  163. 'uploader': uploader,
  164. 'uploader_id': uploader_id,
  165. 'duration': duration,
  166. 'formats': formats,
  167. }