2
0

viewster.py 5.3 KB

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