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.

174 lines
5.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. _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. medias = self._call_api(
  73. 'shows/%s/medias' % video_id,
  74. video_id, 'Downloading video JSON')
  75. qualities = self._call_api(
  76. 'qualities',
  77. video_id, 'Downloading qualities JSON')
  78. formats = []
  79. for lang, lang_dict in medias['fr']['video_list'].items():
  80. for format_id, fmt in lang_dict['quality_list'].items():
  81. format_id_extended = '%s-%s' % (lang, format_id) if lang != 'none' else format_id
  82. video = self._call_api(
  83. 'shows/%s/video/%s/fr' % (video_id, format_id.lower()),
  84. video_id, 'Downloading %s video JSON' % format_id_extended,
  85. lang if lang != 'none' else None)
  86. file_url = video['file']
  87. if not file_url:
  88. continue
  89. if file_url in ['forbidden', 'not found']:
  90. popmessage = video['popmessage']
  91. self._raise_error(popmessage['title'], popmessage['message'])
  92. formats.append({
  93. 'url': file_url,
  94. 'format_id': format_id_extended,
  95. 'width': fmt['res_width'],
  96. 'height': fmt['res_lines'],
  97. 'abr': fmt['audiobitrate'],
  98. 'vbr': fmt['videobitrate'],
  99. 'filesize': fmt['filesize'],
  100. 'format_note': qualities[format_id]['quality_name'],
  101. 'preference': qualities[format_id]['priority'],
  102. })
  103. self._sort_formats(formats)
  104. show = self._call_api(
  105. 'shows/by_id/%s' % video_id,
  106. video_id, 'Downloading show JSON')[0]
  107. upload_date = unified_strdate(show['online_date_start_utc'])
  108. uploader = show['partner_name']
  109. uploader_id = show['partner_key']
  110. duration = show['duration_ms'] / 1000.0
  111. thumbnails = []
  112. for thumbnail_key, thumbnail_url in show.items():
  113. m = re.search(r'^screenshot_(?P<width>\d+)x(?P<height>\d+)$', thumbnail_key)
  114. if not m:
  115. continue
  116. thumbnails.append({
  117. 'url': thumbnail_url,
  118. 'width': int(m.group('width')),
  119. 'height': int(m.group('height')),
  120. })
  121. episode = show.get('show_TT') or show.get('show_OT')
  122. family = show.get('family_TT') or show.get('family_OT')
  123. episode_number = show.get('episode_number')
  124. title = ''
  125. if family:
  126. title += family
  127. if episode_number:
  128. title += ' #' + compat_str(episode_number)
  129. if episode:
  130. title += ' - ' + episode
  131. description = show.get('show_resume') or show.get('family_resume')
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'description': description,
  136. 'thumbnails': thumbnails,
  137. 'upload_date': upload_date,
  138. 'uploader': uploader,
  139. 'uploader_id': uploader_id,
  140. 'duration': duration,
  141. 'formats': formats,
  142. }