2 from __future__ 
import unicode_literals
 
   4 from .common 
import InfoExtractor
 
   8     compat_urllib_parse_unquote
, 
  20 class ViewsterIE(InfoExtractor
): 
  21     _VALID_URL 
= r
'https?://(?:www\.)?viewster\.com/(?:serie|movie)/(?P<id>\d+-\d+-\d+)' 
  24         'url': 'http://www.viewster.com/movie/1140-11855-000/the-listening-project/', 
  25         'md5': 'e642d1b27fcf3a4ffa79f194f5adde36', 
  27             'id': '1140-11855-000', 
  29             'title': 'The listening Project', 
  30             'description': 'md5:bac720244afd1a8ea279864e67baa071', 
  31             'timestamp': 1214870400, 
  32             'upload_date': '20080701', 
  36         # series episode, Type=Episode 
  37         'url': 'http://www.viewster.com/serie/1284-19427-001/the-world-and-a-wall/', 
  38         'md5': '9243079a8531809efe1b089db102c069', 
  40             'id': '1284-19427-001', 
  42             'title': 'The World and a Wall', 
  43             'description': 'md5:24814cf74d3453fdf5bfef9716d073e3', 
  44             'timestamp': 1428192000, 
  45             'upload_date': '20150405', 
  50         'url': 'http://www.viewster.com/serie/1303-19426-000/', 
  52             'id': '1303-19426-000', 
  53             'title': 'Is It Wrong to Try to Pick up Girls in a Dungeon?', 
  54             'description': 'md5:eeda9bef25b0d524b3a29a97804c2f11', 
  58         # unfinished serie, no Type 
  59         'url': 'http://www.viewster.com/serie/1284-19427-000/baby-steps-season-2/', 
  61             'id': '1284-19427-000', 
  62             'title': 'Baby Steps—Season 2', 
  63             'description': 'md5:e7097a8fc97151e25f085c9eb7a1cdb1', 
  65         'playlist_mincount': 16, 
  67         # geo restricted series 
  68         'url': 'https://www.viewster.com/serie/1280-18794-002/', 
  69         'only_matching': True, 
  71         # geo restricted video 
  72         'url': 'https://www.viewster.com/serie/1280-18794-002/what-is-extraterritoriality-lawo/', 
  73         'only_matching': True, 
  76     _ACCEPT_HEADER 
= 'application/json, text/javascript, */*; q=0.01' 
  78     def _download_json(self
, url
, video_id
, note
='Downloading JSON metadata', fatal
=True): 
  79         request 
= sanitized_Request(url
) 
  80         request
.add_header('Accept', self
._ACCEPT
_HEADER
) 
  81         request
.add_header('Auth-token', self
._AUTH
_TOKEN
) 
  82         return super(ViewsterIE
, self
)._download
_json
(request
, video_id
, note
, fatal
=fatal
) 
  84     def _real_extract(self
, url
): 
  85         video_id 
= self
._match
_id
(url
) 
  86         # Get 'api_token' cookie 
  87         self
._request
_webpage
(HEADRequest('http://www.viewster.com/'), video_id
) 
  88         cookies 
= self
._get
_cookies
('http://www.viewster.com/') 
  89         self
._AUTH
_TOKEN 
= compat_urllib_parse_unquote(cookies
['api_token'].value
) 
  91         info 
= self
._download
_json
( 
  92             'https://public-api.viewster.com/search/%s' % video_id
, 
  93             video_id
, 'Downloading entry JSON') 
  95         entry_id 
= info
.get('Id') or info
['id'] 
  97         # unfinished serie has no Type 
  98         if info
.get('Type') in ('Serie', None): 
 100                 episodes 
= self
._download
_json
( 
 101                     'https://public-api.viewster.com/series/%s/episodes' % entry_id
, 
 102                     video_id
, 'Downloading series JSON') 
 103             except ExtractorError 
as e
: 
 104                 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code 
== 404: 
 105                     self
.raise_geo_restricted() 
 110                     'http://www.viewster.com/movie/%s' % episode
['OriginId'], 'Viewster') 
 111                 for episode 
in episodes
] 
 112             title 
= (info
.get('Title') or info
['Synopsis']['Title']).strip() 
 113             description 
= info
.get('Synopsis', {}).get('Detailed') 
 114             return self
.playlist_result(entries
, video_id
, title
, description
) 
 117         for media_type 
in ('application/f4m+xml', 'application/x-mpegURL', 'video/mp4'): 
 118             media 
= self
._download
_json
( 
 119                 'https://public-api.viewster.com/movies/%s/video?mediaType=%s' 
 120                 % (entry_id
, compat_urllib_parse
.quote(media_type
)), 
 121                 video_id
, 'Downloading %s JSON' % media_type
, fatal
=False) 
 124             video_url 
= media
.get('Uri') 
 127             ext 
= determine_ext(video_url
) 
 129                 video_url 
+= '&' if '?' in video_url 
else '?' 
 130                 video_url 
+= 'hdcore=3.2.0&plugin=flowplayer-3.2.0.1' 
 131                 formats
.extend(self
._extract
_f
4m
_formats
( 
 132                     video_url
, video_id
, f4m_id
='hds')) 
 134                 m3u8_formats 
= self
._extract
_m
3u8_formats
( 
 135                     video_url
, video_id
, 'mp4', m3u8_id
='hls', 
 136                     fatal
=False)  # m3u8 sometimes fail 
 138                     formats
.extend(m3u8_formats
) 
 140                 format_id 
= media
.get('Bitrate') 
 143                     'format_id': 'mp4-%s' % format_id
, 
 144                     'height': int_or_none(media
.get('Height')), 
 145                     'width': int_or_none(media
.get('Width')), 
 148                 if format_id 
and not f
['height']: 
 149                     f
['height'] = int_or_none(self
._search
_regex
( 
 150                         r
'^(\d+)[pP]$', format_id
, 'height', default
=None)) 
 153         if not formats 
and not info
.get('LanguageSets') and not info
.get('VODSettings'): 
 154             self
.raise_geo_restricted() 
 156         self
._sort
_formats
(formats
) 
 158         synopsis 
= info
.get('Synopsis') or {} 
 159         # Prefer title outside synopsis since it's less messy 
 160         title 
= (info
.get('Title') or synopsis
['Title']).strip() 
 161         description 
= synopsis
.get('Detailed') or (info
.get('Synopsis') or {}).get('Short') 
 162         duration 
= int_or_none(info
.get('Duration')) 
 163         timestamp 
= parse_iso8601(info
.get('ReleaseDate')) 
 168             'description': description
, 
 169             'timestamp': timestamp
, 
 170             'duration': duration
,