iprima.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import re
  2. import time
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. js_to_json,
  7. urlencode_postdata,
  8. ExtractorError,
  9. parse_qs
  10. )
  11. class IPrimaIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?!cnn)(?:[^/]+)\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  13. _GEO_BYPASS = False
  14. _NETRC_MACHINE = 'iprima'
  15. _LOGIN_URL = 'https://auth.iprima.cz/oauth2/login'
  16. _TOKEN_URL = 'https://auth.iprima.cz/oauth2/token'
  17. access_token = None
  18. _TESTS = [{
  19. 'url': 'https://prima.iprima.cz/particka/92-epizoda',
  20. 'info_dict': {
  21. 'id': 'p51388',
  22. 'ext': 'mp4',
  23. 'title': 'Partička (92)',
  24. 'description': 'md5:859d53beae4609e6dd7796413f1b6cac',
  25. 'upload_date': '20201103',
  26. 'timestamp': 1604437480,
  27. },
  28. 'params': {
  29. 'skip_download': True, # m3u8 download
  30. },
  31. }, {
  32. 'url': 'http://play.iprima.cz/particka/particka-92',
  33. 'only_matching': True,
  34. }, {
  35. # geo restricted
  36. 'url': 'http://play.iprima.cz/closer-nove-pripady/closer-nove-pripady-iv-1',
  37. 'only_matching': True,
  38. }, {
  39. 'url': 'https://prima.iprima.cz/my-little-pony/mapa-znameni-2-2',
  40. 'only_matching': True,
  41. }, {
  42. 'url': 'https://prima.iprima.cz/porady/jak-se-stavi-sen/rodina-rathousova-praha',
  43. 'only_matching': True,
  44. }, {
  45. 'url': 'http://www.iprima.cz/filmy/desne-rande',
  46. 'only_matching': True,
  47. }, {
  48. 'url': 'https://zoom.iprima.cz/10-nejvetsich-tajemstvi-zahad/posvatna-mista-a-stavby',
  49. 'only_matching': True,
  50. }, {
  51. 'url': 'https://krimi.iprima.cz/mraz-0/sebevrazdy',
  52. 'only_matching': True,
  53. }, {
  54. 'url': 'https://cool.iprima.cz/derava-silnice-nevadi',
  55. 'only_matching': True,
  56. }, {
  57. 'url': 'https://love.iprima.cz/laska-az-za-hrob/slib-dany-bratrovi',
  58. 'only_matching': True,
  59. }]
  60. def _perform_login(self, username, password):
  61. if self.access_token:
  62. return
  63. login_page = self._download_webpage(
  64. self._LOGIN_URL, None, note='Downloading login page',
  65. errnote='Downloading login page failed')
  66. login_form = self._hidden_inputs(login_page)
  67. login_form.update({
  68. '_email': username,
  69. '_password': password})
  70. _, login_handle = self._download_webpage_handle(
  71. self._LOGIN_URL, None, data=urlencode_postdata(login_form),
  72. note='Logging in')
  73. code = parse_qs(login_handle.geturl()).get('code')[0]
  74. if not code:
  75. raise ExtractorError('Login failed', expected=True)
  76. token_request_data = {
  77. 'scope': 'openid+email+profile+phone+address+offline_access',
  78. 'client_id': 'prima_sso',
  79. 'grant_type': 'authorization_code',
  80. 'code': code,
  81. 'redirect_uri': 'https://auth.iprima.cz/sso/auth-check'}
  82. token_data = self._download_json(
  83. self._TOKEN_URL, None,
  84. note='Downloading token', errnote='Downloading token failed',
  85. data=urlencode_postdata(token_request_data))
  86. self.access_token = token_data.get('access_token')
  87. if self.access_token is None:
  88. raise ExtractorError('Getting token failed', expected=True)
  89. def _real_initialize(self):
  90. if not self.access_token:
  91. self.raise_login_required('Login is required to access any iPrima content', method='password')
  92. def _raise_access_error(self, error_code):
  93. if error_code == 'PLAY_GEOIP_DENIED':
  94. self.raise_geo_restricted(countries=['CZ'], metadata_available=True)
  95. elif error_code is not None:
  96. self.raise_no_formats('Access to stream infos forbidden', expected=True)
  97. def _real_extract(self, url):
  98. video_id = self._match_id(url)
  99. webpage = self._download_webpage(url, video_id)
  100. title = self._html_search_meta(
  101. ['og:title', 'twitter:title'],
  102. webpage, 'title', default=None)
  103. video_id = self._search_regex((
  104. r'productId\s*=\s*([\'"])(?P<id>p\d+)\1',
  105. r'pproduct_id\s*=\s*([\'"])(?P<id>p\d+)\1'),
  106. webpage, 'real id', group='id')
  107. metadata = self._download_json(
  108. f'https://api.play-backend.iprima.cz/api/v1//products/id-{video_id}/play',
  109. video_id, note='Getting manifest URLs', errnote='Failed to get manifest URLs',
  110. headers={'X-OTT-Access-Token': self.access_token},
  111. expected_status=403)
  112. self._raise_access_error(metadata.get('errorCode'))
  113. stream_infos = metadata.get('streamInfos')
  114. formats = []
  115. if stream_infos is None:
  116. self.raise_no_formats('Reading stream infos failed', expected=True)
  117. else:
  118. for manifest in stream_infos:
  119. manifest_type = manifest.get('type')
  120. manifest_url = manifest.get('url')
  121. ext = determine_ext(manifest_url)
  122. if manifest_type == 'HLS' or ext == 'm3u8':
  123. formats += self._extract_m3u8_formats(
  124. manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
  125. m3u8_id='hls', fatal=False)
  126. elif manifest_type == 'DASH' or ext == 'mpd':
  127. formats += self._extract_mpd_formats(
  128. manifest_url, video_id, mpd_id='dash', fatal=False)
  129. final_result = self._search_json_ld(webpage, video_id, default={})
  130. final_result.update({
  131. 'id': video_id,
  132. 'title': title,
  133. 'thumbnail': self._html_search_meta(
  134. ['thumbnail', 'og:image', 'twitter:image'],
  135. webpage, 'thumbnail', default=None),
  136. 'formats': formats,
  137. 'description': self._html_search_meta(
  138. ['description', 'og:description', 'twitter:description'],
  139. webpage, 'description', default=None)})
  140. return final_result
  141. class IPrimaCNNIE(InfoExtractor):
  142. _VALID_URL = r'https?://cnn\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  143. _GEO_BYPASS = False
  144. _TESTS = [{
  145. 'url': 'https://cnn.iprima.cz/porady/strunc/24072020-koronaviru-mam-plne-zuby-strasit-druhou-vlnou-je-absurdni-rika-senatorka-dernerova',
  146. 'info_dict': {
  147. 'id': 'p716177',
  148. 'ext': 'mp4',
  149. 'title': 'md5:277c6b1ed0577e51b40ddd35602ff43e',
  150. },
  151. 'params': {
  152. 'skip_download': 'm3u8'
  153. }
  154. }]
  155. def _real_extract(self, url):
  156. video_id = self._match_id(url)
  157. self._set_cookie('play.iprima.cz', 'ott_adult_confirmed', '1')
  158. webpage = self._download_webpage(url, video_id)
  159. title = self._og_search_title(
  160. webpage, default=None) or self._search_regex(
  161. r'<h1>([^<]+)', webpage, 'title')
  162. video_id = self._search_regex(
  163. (r'<iframe[^>]+\bsrc=["\'](?:https?:)?//(?:api\.play-backend\.iprima\.cz/prehravac/embedded|prima\.iprima\.cz/[^/]+/[^/]+)\?.*?\bid=(p\d+)',
  164. r'data-product="([^"]+)">',
  165. r'id=["\']player-(p\d+)"',
  166. r'playerId\s*:\s*["\']player-(p\d+)',
  167. r'\bvideos\s*=\s*["\'](p\d+)'),
  168. webpage, 'real id')
  169. playerpage = self._download_webpage(
  170. 'http://play.iprima.cz/prehravac/init',
  171. video_id, note='Downloading player', query={
  172. '_infuse': 1,
  173. '_ts': round(time.time()),
  174. 'productId': video_id,
  175. }, headers={'Referer': url})
  176. formats = []
  177. def extract_formats(format_url, format_key=None, lang=None):
  178. ext = determine_ext(format_url)
  179. new_formats = []
  180. if format_key == 'hls' or ext == 'm3u8':
  181. new_formats = self._extract_m3u8_formats(
  182. format_url, video_id, 'mp4', entry_protocol='m3u8_native',
  183. m3u8_id='hls', fatal=False)
  184. elif format_key == 'dash' or ext == 'mpd':
  185. return
  186. new_formats = self._extract_mpd_formats(
  187. format_url, video_id, mpd_id='dash', fatal=False)
  188. if lang:
  189. for f in new_formats:
  190. if not f.get('language'):
  191. f['language'] = lang
  192. formats.extend(new_formats)
  193. options = self._parse_json(
  194. self._search_regex(
  195. r'(?s)(?:TDIPlayerOptions|playerOptions)\s*=\s*({.+?});\s*\]\]',
  196. playerpage, 'player options', default='{}'),
  197. video_id, transform_source=js_to_json, fatal=False)
  198. if options:
  199. for key, tracks in options.get('tracks', {}).items():
  200. if not isinstance(tracks, list):
  201. continue
  202. for track in tracks:
  203. src = track.get('src')
  204. if src:
  205. extract_formats(src, key.lower(), track.get('lang'))
  206. if not formats:
  207. for _, src in re.findall(r'src["\']\s*:\s*(["\'])(.+?)\1', playerpage):
  208. extract_formats(src)
  209. if not formats and '>GEO_IP_NOT_ALLOWED<' in playerpage:
  210. self.raise_geo_restricted(countries=['CZ'], metadata_available=True)
  211. return {
  212. 'id': video_id,
  213. 'title': title,
  214. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  215. 'formats': formats,
  216. 'description': self._og_search_description(webpage, default=None),
  217. }