noco.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. medias = self._call_api(
  90. 'shows/%s/medias' % video_id,
  91. video_id, 'Downloading video JSON')
  92. show = self._call_api(
  93. 'shows/by_id/%s' % video_id,
  94. video_id, 'Downloading show JSON')[0]
  95. options = self._call_api(
  96. 'users/init', video_id,
  97. 'Downloading user options JSON')['options']
  98. audio_lang_pref = options.get('audio_language') or options.get('language', 'fr')
  99. if audio_lang_pref == 'original':
  100. audio_lang_pref = show['original_lang']
  101. if len(medias) == 1:
  102. audio_lang_pref = list(medias.keys())[0]
  103. elif audio_lang_pref not in medias:
  104. audio_lang_pref = 'fr'
  105. qualities = self._call_api(
  106. 'qualities',
  107. video_id, 'Downloading qualities JSON')
  108. formats = []
  109. for audio_lang, audio_lang_dict in medias.items():
  110. preference = 1 if audio_lang == audio_lang_pref else 0
  111. for sub_lang, lang_dict in audio_lang_dict['video_list'].items():
  112. for format_id, fmt in lang_dict['quality_list'].items():
  113. format_id_extended = 'audio-%s_sub-%s_%s' % (audio_lang, sub_lang, format_id)
  114. video = self._call_api(
  115. 'shows/%s/video/%s/%s' % (video_id, format_id.lower(), audio_lang),
  116. video_id, 'Downloading %s video JSON' % format_id_extended,
  117. sub_lang if sub_lang != 'none' else None)
  118. file_url = video['file']
  119. if not file_url:
  120. continue
  121. if file_url in ['forbidden', 'not found']:
  122. popmessage = video['popmessage']
  123. self._raise_error(popmessage['title'], popmessage['message'])
  124. formats.append({
  125. 'url': file_url,
  126. 'format_id': format_id_extended,
  127. 'width': fmt['res_width'],
  128. 'height': fmt['res_lines'],
  129. 'abr': fmt['audiobitrate'],
  130. 'vbr': fmt['videobitrate'],
  131. 'filesize': fmt['filesize'],
  132. 'format_note': qualities[format_id]['quality_name'],
  133. 'quality': qualities[format_id]['priority'],
  134. 'preference': preference,
  135. })
  136. self._sort_formats(formats)
  137. upload_date = unified_strdate(show['online_date_start_utc'])
  138. uploader = show['partner_name']
  139. uploader_id = show['partner_key']
  140. duration = show['duration_ms'] / 1000.0
  141. thumbnails = []
  142. for thumbnail_key, thumbnail_url in show.items():
  143. m = re.search(r'^screenshot_(?P<width>\d+)x(?P<height>\d+)$', thumbnail_key)
  144. if not m:
  145. continue
  146. thumbnails.append({
  147. 'url': thumbnail_url,
  148. 'width': int(m.group('width')),
  149. 'height': int(m.group('height')),
  150. })
  151. episode = show.get('show_TT') or show.get('show_OT')
  152. family = show.get('family_TT') or show.get('family_OT')
  153. episode_number = show.get('episode_number')
  154. title = ''
  155. if family:
  156. title += family
  157. if episode_number:
  158. title += ' #' + compat_str(episode_number)
  159. if episode:
  160. title += ' - ' + episode
  161. description = show.get('show_resume') or show.get('family_resume')
  162. return {
  163. 'id': video_id,
  164. 'title': title,
  165. 'description': description,
  166. 'thumbnails': thumbnails,
  167. 'upload_date': upload_date,
  168. 'uploader': uploader,
  169. 'uploader_id': uploader_id,
  170. 'duration': duration,
  171. 'formats': formats,
  172. }