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.

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