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.

151 lines
5.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urllib_request,
  6. compat_urllib_parse,
  7. compat_urllib_parse_unquote,
  8. )
  9. from ..utils import (
  10. determine_ext,
  11. int_or_none,
  12. parse_iso8601,
  13. HEADRequest,
  14. )
  15. class ViewsterIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:www\.)?viewster\.com/(?:serie|movie)/(?P<id>\d+-\d+-\d+)'
  17. _TESTS = [{
  18. # movie, Type=Movie
  19. 'url': 'http://www.viewster.com/movie/1140-11855-000/the-listening-project/',
  20. 'md5': 'e642d1b27fcf3a4ffa79f194f5adde36',
  21. 'info_dict': {
  22. 'id': '1140-11855-000',
  23. 'ext': 'mp4',
  24. 'title': 'The listening Project',
  25. 'description': 'md5:bac720244afd1a8ea279864e67baa071',
  26. 'timestamp': 1214870400,
  27. 'upload_date': '20080701',
  28. 'duration': 4680,
  29. },
  30. }, {
  31. # series episode, Type=Episode
  32. 'url': 'http://www.viewster.com/serie/1284-19427-001/the-world-and-a-wall/',
  33. 'md5': '9243079a8531809efe1b089db102c069',
  34. 'info_dict': {
  35. 'id': '1284-19427-001',
  36. 'ext': 'mp4',
  37. 'title': 'The World and a Wall',
  38. 'description': 'md5:24814cf74d3453fdf5bfef9716d073e3',
  39. 'timestamp': 1428192000,
  40. 'upload_date': '20150405',
  41. 'duration': 1500,
  42. },
  43. }, {
  44. # serie, Type=Serie
  45. 'url': 'http://www.viewster.com/serie/1303-19426-000/',
  46. 'info_dict': {
  47. 'id': '1303-19426-000',
  48. 'title': 'Is It Wrong to Try to Pick up Girls in a Dungeon?',
  49. 'description': 'md5:eeda9bef25b0d524b3a29a97804c2f11',
  50. },
  51. 'playlist_count': 13,
  52. }, {
  53. # unfinished serie, no Type
  54. 'url': 'http://www.viewster.com/serie/1284-19427-000/baby-steps-season-2/',
  55. 'info_dict': {
  56. 'id': '1284-19427-000',
  57. 'title': 'Baby Steps—Season 2',
  58. 'description': 'md5:e7097a8fc97151e25f085c9eb7a1cdb1',
  59. },
  60. 'playlist_mincount': 16,
  61. }]
  62. _ACCEPT_HEADER = 'application/json, text/javascript, */*; q=0.01'
  63. def _download_json(self, url, video_id, note='Downloading JSON metadata', fatal=True):
  64. request = compat_urllib_request.Request(url)
  65. request.add_header('Accept', self._ACCEPT_HEADER)
  66. request.add_header('Auth-token', self._AUTH_TOKEN)
  67. return super(ViewsterIE, self)._download_json(request, video_id, note, fatal=fatal)
  68. def _real_extract(self, url):
  69. video_id = self._match_id(url)
  70. # Get 'api_token' cookie
  71. self._request_webpage(HEADRequest('http://www.viewster.com/'), video_id)
  72. cookies = self._get_cookies('http://www.viewster.com/')
  73. self._AUTH_TOKEN = compat_urllib_parse_unquote(cookies['api_token'].value)
  74. info = self._download_json(
  75. 'https://public-api.viewster.com/search/%s' % video_id,
  76. video_id, 'Downloading entry JSON')
  77. entry_id = info.get('Id') or info['id']
  78. # unfinished serie has no Type
  79. if info.get('Type') in ['Serie', None]:
  80. episodes = self._download_json(
  81. 'https://public-api.viewster.com/series/%s/episodes' % entry_id,
  82. video_id, 'Downloading series JSON')
  83. entries = [
  84. self.url_result(
  85. 'http://www.viewster.com/movie/%s' % episode['OriginId'], 'Viewster')
  86. for episode in episodes]
  87. title = (info.get('Title') or info['Synopsis']['Title']).strip()
  88. description = info.get('Synopsis', {}).get('Detailed')
  89. return self.playlist_result(entries, video_id, title, description)
  90. formats = []
  91. for media_type in ('application/f4m+xml', 'application/x-mpegURL', 'video/mp4'):
  92. media = self._download_json(
  93. 'https://public-api.viewster.com/movies/%s/video?mediaType=%s'
  94. % (entry_id, compat_urllib_parse.quote(media_type)),
  95. video_id, 'Downloading %s JSON' % media_type, fatal=False)
  96. if not media:
  97. continue
  98. video_url = media.get('Uri')
  99. if not video_url:
  100. continue
  101. ext = determine_ext(video_url)
  102. if ext == 'f4m':
  103. video_url += '&' if '?' in video_url else '?'
  104. video_url += 'hdcore=3.2.0&plugin=flowplayer-3.2.0.1'
  105. formats.extend(self._extract_f4m_formats(
  106. video_url, video_id, f4m_id='hds'))
  107. elif ext == 'm3u8':
  108. formats.extend(self._extract_m3u8_formats(
  109. video_url, video_id, 'mp4', m3u8_id='hls',
  110. fatal=False # m3u8 sometimes fail
  111. ))
  112. else:
  113. format_id = media.get('Bitrate')
  114. f = {
  115. 'url': video_url,
  116. 'format_id': 'mp4-%s' % format_id,
  117. 'height': int_or_none(media.get('Height')),
  118. 'width': int_or_none(media.get('Width')),
  119. 'preference': 1,
  120. }
  121. if format_id and not f['height']:
  122. f['height'] = int_or_none(self._search_regex(
  123. r'^(\d+)[pP]$', format_id, 'height', default=None))
  124. formats.append(f)
  125. self._sort_formats(formats)
  126. synopsis = info.get('Synopsis', {})
  127. # Prefer title outside synopsis since it's less messy
  128. title = (info.get('Title') or synopsis['Title']).strip()
  129. description = synopsis.get('Detailed') or info.get('Synopsis', {}).get('Short')
  130. duration = int_or_none(info.get('Duration'))
  131. timestamp = parse_iso8601(info.get('ReleaseDate'))
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'description': description,
  136. 'timestamp': timestamp,
  137. 'duration': duration,
  138. 'formats': formats,
  139. }