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.

478 lines
18 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. parse_iso8601,
  10. parse_duration,
  11. str_or_none,
  12. update_url_query,
  13. urljoin,
  14. )
  15. class TVNowBaseIE(InfoExtractor):
  16. _VIDEO_FIELDS = (
  17. 'id', 'title', 'free', 'geoblocked', 'articleLong', 'articleShort',
  18. 'broadcastStartDate', 'isDrm', 'duration', 'season', 'episode',
  19. 'manifest.dashclear', 'manifest.hlsclear', 'manifest.smoothclear',
  20. 'format.title', 'format.defaultImage169Format', 'format.defaultImage169Logo')
  21. def _call_api(self, path, video_id, query):
  22. return self._download_json(
  23. 'https://api.tvnow.de/v3/' + path, video_id, query=query)
  24. def _extract_video(self, info, display_id):
  25. video_id = compat_str(info['id'])
  26. title = info['title']
  27. paths = []
  28. for manifest_url in (info.get('manifest') or {}).values():
  29. if not manifest_url:
  30. continue
  31. manifest_url = update_url_query(manifest_url, {'filter': ''})
  32. path = self._search_regex(r'https?://[^/]+/(.+?)\.ism/', manifest_url, 'path')
  33. if path in paths:
  34. continue
  35. paths.append(path)
  36. def url_repl(proto, suffix):
  37. return re.sub(
  38. r'(?:hls|dash|hss)([.-])', proto + r'\1', re.sub(
  39. r'\.ism/(?:[^.]*\.(?:m3u8|mpd)|[Mm]anifest)',
  40. '.ism/' + suffix, manifest_url))
  41. formats = self._extract_mpd_formats(
  42. url_repl('dash', '.mpd'), video_id,
  43. mpd_id='dash', fatal=False)
  44. formats.extend(self._extract_ism_formats(
  45. url_repl('hss', 'Manifest'),
  46. video_id, ism_id='mss', fatal=False))
  47. formats.extend(self._extract_m3u8_formats(
  48. url_repl('hls', '.m3u8'), video_id, 'mp4',
  49. 'm3u8_native', m3u8_id='hls', fatal=False))
  50. if formats:
  51. break
  52. else:
  53. if info.get('isDrm'):
  54. raise ExtractorError(
  55. 'Video %s is DRM protected' % video_id, expected=True)
  56. if info.get('geoblocked'):
  57. raise self.raise_geo_restricted()
  58. if not info.get('free', True):
  59. raise ExtractorError(
  60. 'Video %s is not available for free' % video_id, expected=True)
  61. self._sort_formats(formats)
  62. description = info.get('articleLong') or info.get('articleShort')
  63. timestamp = parse_iso8601(info.get('broadcastStartDate'), ' ')
  64. duration = parse_duration(info.get('duration'))
  65. f = info.get('format', {})
  66. thumbnails = [{
  67. 'url': 'https://aistvnow-a.akamaihd.net/tvnow/movie/%s' % video_id,
  68. }]
  69. thumbnail = f.get('defaultImage169Format') or f.get('defaultImage169Logo')
  70. if thumbnail:
  71. thumbnails.append({
  72. 'url': thumbnail,
  73. })
  74. return {
  75. 'id': video_id,
  76. 'display_id': display_id,
  77. 'title': title,
  78. 'description': description,
  79. 'thumbnails': thumbnails,
  80. 'timestamp': timestamp,
  81. 'duration': duration,
  82. 'series': f.get('title'),
  83. 'season_number': int_or_none(info.get('season')),
  84. 'episode_number': int_or_none(info.get('episode')),
  85. 'episode': title,
  86. 'formats': formats,
  87. }
  88. class TVNowIE(TVNowBaseIE):
  89. _VALID_URL = r'''(?x)
  90. https?://
  91. (?:www\.)?tvnow\.(?:de|at|ch)/(?P<station>[^/]+)/
  92. (?P<show_id>[^/]+)/
  93. (?!(?:list|jahr)(?:/|$))(?P<id>[^/?\#&]+)
  94. '''
  95. @classmethod
  96. def suitable(cls, url):
  97. return (False if TVNowNewIE.suitable(url) or TVNowSeasonIE.suitable(url) or TVNowAnnualIE.suitable(url) or TVNowShowIE.suitable(url)
  98. else super(TVNowIE, cls).suitable(url))
  99. _TESTS = [{
  100. 'url': 'https://www.tvnow.de/rtl2/grip-das-motormagazin/der-neue-porsche-911-gt-3/player',
  101. 'info_dict': {
  102. 'id': '331082',
  103. 'display_id': 'grip-das-motormagazin/der-neue-porsche-911-gt-3',
  104. 'ext': 'mp4',
  105. 'title': 'Der neue Porsche 911 GT 3',
  106. 'description': 'md5:6143220c661f9b0aae73b245e5d898bb',
  107. 'timestamp': 1495994400,
  108. 'upload_date': '20170528',
  109. 'duration': 5283,
  110. 'series': 'GRIP - Das Motormagazin',
  111. 'season_number': 14,
  112. 'episode_number': 405,
  113. 'episode': 'Der neue Porsche 911 GT 3',
  114. },
  115. }, {
  116. # rtl2
  117. 'url': 'https://www.tvnow.de/rtl2/armes-deutschland/episode-0008/player',
  118. 'only_matching': True,
  119. }, {
  120. # rtlnitro
  121. 'url': 'https://www.tvnow.de/nitro/alarm-fuer-cobra-11-die-autobahnpolizei/auf-eigene-faust-pilot/player',
  122. 'only_matching': True,
  123. }, {
  124. # superrtl
  125. 'url': 'https://www.tvnow.de/superrtl/die-lustigsten-schlamassel-der-welt/u-a-ketchup-effekt/player',
  126. 'only_matching': True,
  127. }, {
  128. # ntv
  129. 'url': 'https://www.tvnow.de/ntv/startup-news/goetter-in-weiss/player',
  130. 'only_matching': True,
  131. }, {
  132. # vox
  133. 'url': 'https://www.tvnow.de/vox/auto-mobil/neues-vom-automobilmarkt-2017-11-19-17-00-00/player',
  134. 'only_matching': True,
  135. }, {
  136. # rtlplus
  137. 'url': 'https://www.tvnow.de/rtlplus/op-ruft-dr-bruckner/die-vernaehte-frau/player',
  138. 'only_matching': True,
  139. }, {
  140. 'url': 'https://www.tvnow.de/rtl2/grip-das-motormagazin/der-neue-porsche-911-gt-3',
  141. 'only_matching': True,
  142. }]
  143. def _real_extract(self, url):
  144. mobj = re.match(self._VALID_URL, url)
  145. display_id = '%s/%s' % mobj.group(2, 3)
  146. info = self._call_api(
  147. 'movies/' + display_id, display_id, query={
  148. 'fields': ','.join(self._VIDEO_FIELDS),
  149. })
  150. return self._extract_video(info, display_id)
  151. class TVNowNewIE(InfoExtractor):
  152. _VALID_URL = r'''(?x)
  153. (?P<base_url>https?://
  154. (?:www\.)?tvnow\.(?:de|at|ch)/
  155. (?:shows|serien))/
  156. (?P<show>[^/]+)-\d+/
  157. [^/]+/
  158. episode-\d+-(?P<episode>[^/?$&]+)-(?P<id>\d+)
  159. '''
  160. _TESTS = [{
  161. 'url': 'https://www.tvnow.de/shows/grip-das-motormagazin-1669/2017-05/episode-405-der-neue-porsche-911-gt-3-331082',
  162. 'only_matching': True,
  163. }]
  164. def _real_extract(self, url):
  165. mobj = re.match(self._VALID_URL, url)
  166. base_url = re.sub(r'(?:shows|serien)', '_', mobj.group('base_url'))
  167. show, episode = mobj.group('show', 'episode')
  168. return self.url_result(
  169. # Rewrite new URLs to the old format and use extraction via old API
  170. # at api.tvnow.de as a loophole for bypassing premium content checks
  171. '%s/%s/%s' % (base_url, show, episode),
  172. ie=TVNowIE.ie_key(), video_id=mobj.group('id'))
  173. class TVNowNewBaseIE(InfoExtractor):
  174. def _call_api(self, path, video_id, query={}):
  175. result = self._download_json(
  176. 'https://apigw.tvnow.de/module/' + path, video_id, query=query)
  177. error = result.get('error')
  178. if error:
  179. raise ExtractorError(
  180. '%s said: %s' % (self.IE_NAME, error), expected=True)
  181. return result
  182. """
  183. TODO: new apigw.tvnow.de based version of TVNowIE. Replace old TVNowIE with it
  184. when api.tvnow.de is shut down. This version can't bypass premium checks though.
  185. class TVNowIE(TVNowNewBaseIE):
  186. _VALID_URL = r'''(?x)
  187. https?://
  188. (?:www\.)?tvnow\.(?:de|at|ch)/
  189. (?:shows|serien)/[^/]+/
  190. (?:[^/]+/)+
  191. (?P<display_id>[^/?$&]+)-(?P<id>\d+)
  192. '''
  193. _TESTS = [{
  194. # episode with annual navigation
  195. 'url': 'https://www.tvnow.de/shows/grip-das-motormagazin-1669/2017-05/episode-405-der-neue-porsche-911-gt-3-331082',
  196. 'info_dict': {
  197. 'id': '331082',
  198. 'display_id': 'grip-das-motormagazin/der-neue-porsche-911-gt-3',
  199. 'ext': 'mp4',
  200. 'title': 'Der neue Porsche 911 GT 3',
  201. 'description': 'md5:6143220c661f9b0aae73b245e5d898bb',
  202. 'thumbnail': r're:^https?://.*\.jpg$',
  203. 'timestamp': 1495994400,
  204. 'upload_date': '20170528',
  205. 'duration': 5283,
  206. 'series': 'GRIP - Das Motormagazin',
  207. 'season_number': 14,
  208. 'episode_number': 405,
  209. 'episode': 'Der neue Porsche 911 GT 3',
  210. },
  211. }, {
  212. # rtl2, episode with season navigation
  213. 'url': 'https://www.tvnow.de/shows/armes-deutschland-11471/staffel-3/episode-14-bernd-steht-seit-der-trennung-von-seiner-frau-allein-da-526124',
  214. 'only_matching': True,
  215. }, {
  216. # rtlnitro
  217. 'url': 'https://www.tvnow.de/serien/alarm-fuer-cobra-11-die-autobahnpolizei-1815/staffel-13/episode-5-auf-eigene-faust-pilot-366822',
  218. 'only_matching': True,
  219. }, {
  220. # superrtl
  221. 'url': 'https://www.tvnow.de/shows/die-lustigsten-schlamassel-der-welt-1221/staffel-2/episode-14-u-a-ketchup-effekt-364120',
  222. 'only_matching': True,
  223. }, {
  224. # ntv
  225. 'url': 'https://www.tvnow.de/shows/startup-news-10674/staffel-2/episode-39-goetter-in-weiss-387630',
  226. 'only_matching': True,
  227. }, {
  228. # vox
  229. 'url': 'https://www.tvnow.de/shows/auto-mobil-174/2017-11/episode-46-neues-vom-automobilmarkt-2017-11-19-17-00-00-380072',
  230. 'only_matching': True,
  231. }, {
  232. 'url': 'https://www.tvnow.de/shows/grip-das-motormagazin-1669/2017-05/episode-405-der-neue-porsche-911-gt-3-331082',
  233. 'only_matching': True,
  234. }]
  235. def _extract_video(self, info, url, display_id):
  236. config = info['config']
  237. source = config['source']
  238. video_id = compat_str(info.get('id') or source['videoId'])
  239. title = source['title'].strip()
  240. paths = []
  241. for manifest_url in (info.get('manifest') or {}).values():
  242. if not manifest_url:
  243. continue
  244. manifest_url = update_url_query(manifest_url, {'filter': ''})
  245. path = self._search_regex(r'https?://[^/]+/(.+?)\.ism/', manifest_url, 'path')
  246. if path in paths:
  247. continue
  248. paths.append(path)
  249. def url_repl(proto, suffix):
  250. return re.sub(
  251. r'(?:hls|dash|hss)([.-])', proto + r'\1', re.sub(
  252. r'\.ism/(?:[^.]*\.(?:m3u8|mpd)|[Mm]anifest)',
  253. '.ism/' + suffix, manifest_url))
  254. formats = self._extract_mpd_formats(
  255. url_repl('dash', '.mpd'), video_id,
  256. mpd_id='dash', fatal=False)
  257. formats.extend(self._extract_ism_formats(
  258. url_repl('hss', 'Manifest'),
  259. video_id, ism_id='mss', fatal=False))
  260. formats.extend(self._extract_m3u8_formats(
  261. url_repl('hls', '.m3u8'), video_id, 'mp4',
  262. 'm3u8_native', m3u8_id='hls', fatal=False))
  263. if formats:
  264. break
  265. else:
  266. if try_get(info, lambda x: x['rights']['isDrm']):
  267. raise ExtractorError(
  268. 'Video %s is DRM protected' % video_id, expected=True)
  269. if try_get(config, lambda x: x['boards']['geoBlocking']['block']):
  270. raise self.raise_geo_restricted()
  271. if not info.get('free', True):
  272. raise ExtractorError(
  273. 'Video %s is not available for free' % video_id, expected=True)
  274. self._sort_formats(formats)
  275. description = source.get('description')
  276. thumbnail = url_or_none(source.get('poster'))
  277. timestamp = unified_timestamp(source.get('previewStart'))
  278. duration = parse_duration(source.get('length'))
  279. series = source.get('format')
  280. season_number = int_or_none(self._search_regex(
  281. r'staffel-(\d+)', url, 'season number', default=None))
  282. episode_number = int_or_none(self._search_regex(
  283. r'episode-(\d+)', url, 'episode number', default=None))
  284. return {
  285. 'id': video_id,
  286. 'display_id': display_id,
  287. 'title': title,
  288. 'description': description,
  289. 'thumbnail': thumbnail,
  290. 'timestamp': timestamp,
  291. 'duration': duration,
  292. 'series': series,
  293. 'season_number': season_number,
  294. 'episode_number': episode_number,
  295. 'episode': title,
  296. 'formats': formats,
  297. }
  298. def _real_extract(self, url):
  299. display_id, video_id = re.match(self._VALID_URL, url).groups()
  300. info = self._call_api('player/' + video_id, video_id)
  301. return self._extract_video(info, video_id, display_id)
  302. """
  303. class TVNowListBaseIE(TVNowNewBaseIE):
  304. _SHOW_VALID_URL = r'''(?x)
  305. (?P<base_url>
  306. https?://
  307. (?:www\.)?tvnow\.(?:de|at|ch)/(?:shows|serien)/
  308. [^/?#&]+-(?P<show_id>\d+)
  309. )
  310. '''
  311. @classmethod
  312. def suitable(cls, url):
  313. return (False if TVNowNewIE.suitable(url)
  314. else super(TVNowListBaseIE, cls).suitable(url))
  315. def _extract_items(self, url, show_id, list_id, query):
  316. items = self._call_api(
  317. 'teaserrow/format/episode/' + show_id, list_id,
  318. query=query)['items']
  319. entries = []
  320. for item in items:
  321. if not isinstance(item, dict):
  322. continue
  323. item_url = urljoin(url, item.get('url'))
  324. if not item_url:
  325. continue
  326. video_id = str_or_none(item.get('id') or item.get('videoId'))
  327. item_title = item.get('subheadline') or item.get('text')
  328. entries.append(self.url_result(
  329. item_url, ie=TVNowNewIE.ie_key(), video_id=video_id,
  330. video_title=item_title))
  331. return self.playlist_result(entries, '%s/%s' % (show_id, list_id))
  332. class TVNowSeasonIE(TVNowListBaseIE):
  333. _VALID_URL = r'%s/staffel-(?P<id>\d+)' % TVNowListBaseIE._SHOW_VALID_URL
  334. _TESTS = [{
  335. 'url': 'https://www.tvnow.de/serien/alarm-fuer-cobra-11-die-autobahnpolizei-1815/staffel-13',
  336. 'info_dict': {
  337. 'id': '1815/13',
  338. },
  339. 'playlist_mincount': 22,
  340. }]
  341. def _real_extract(self, url):
  342. _, show_id, season_id = re.match(self._VALID_URL, url).groups()
  343. return self._extract_items(
  344. url, show_id, season_id, {'season': season_id})
  345. class TVNowAnnualIE(TVNowListBaseIE):
  346. _VALID_URL = r'%s/(?P<year>\d{4})-(?P<month>\d{2})' % TVNowListBaseIE._SHOW_VALID_URL
  347. _TESTS = [{
  348. 'url': 'https://www.tvnow.de/shows/grip-das-motormagazin-1669/2017-05',
  349. 'info_dict': {
  350. 'id': '1669/2017-05',
  351. },
  352. 'playlist_mincount': 2,
  353. }]
  354. def _real_extract(self, url):
  355. _, show_id, year, month = re.match(self._VALID_URL, url).groups()
  356. return self._extract_items(
  357. url, show_id, '%s-%s' % (year, month), {
  358. 'year': int(year),
  359. 'month': int(month),
  360. })
  361. class TVNowShowIE(TVNowListBaseIE):
  362. _VALID_URL = TVNowListBaseIE._SHOW_VALID_URL
  363. _TESTS = [{
  364. # annual navigationType
  365. 'url': 'https://www.tvnow.de/shows/grip-das-motormagazin-1669',
  366. 'info_dict': {
  367. 'id': '1669',
  368. },
  369. 'playlist_mincount': 73,
  370. }, {
  371. # season navigationType
  372. 'url': 'https://www.tvnow.de/shows/armes-deutschland-11471',
  373. 'info_dict': {
  374. 'id': '11471',
  375. },
  376. 'playlist_mincount': 3,
  377. }]
  378. @classmethod
  379. def suitable(cls, url):
  380. return (False if TVNowNewIE.suitable(url) or TVNowSeasonIE.suitable(url) or TVNowAnnualIE.suitable(url)
  381. else super(TVNowShowIE, cls).suitable(url))
  382. def _real_extract(self, url):
  383. base_url, show_id = re.match(self._VALID_URL, url).groups()
  384. result = self._call_api(
  385. 'teaserrow/format/navigation/' + show_id, show_id)
  386. items = result['items']
  387. entries = []
  388. navigation = result.get('navigationType')
  389. if navigation == 'annual':
  390. for item in items:
  391. if not isinstance(item, dict):
  392. continue
  393. year = int_or_none(item.get('year'))
  394. if year is None:
  395. continue
  396. months = item.get('months')
  397. if not isinstance(months, list):
  398. continue
  399. for month_dict in months:
  400. if not isinstance(month_dict, dict) or not month_dict:
  401. continue
  402. month_number = int_or_none(list(month_dict.keys())[0])
  403. if month_number is None:
  404. continue
  405. entries.append(self.url_result(
  406. '%s/%04d-%02d' % (base_url, year, month_number),
  407. ie=TVNowAnnualIE.ie_key()))
  408. elif navigation == 'season':
  409. for item in items:
  410. if not isinstance(item, dict):
  411. continue
  412. season_number = int_or_none(item.get('season'))
  413. if season_number is None:
  414. continue
  415. entries.append(self.url_result(
  416. '%s/staffel-%d' % (base_url, season_number),
  417. ie=TVNowSeasonIE.ie_key()))
  418. else:
  419. raise ExtractorError('Unknown navigationType')
  420. return self.playlist_result(entries, show_id)