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
6.7 KiB

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