viewster.py 7.4 KB

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