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.

165 lines
5.3 KiB

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