noco.py 6.2 KB

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