rozhlas.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import itertools
  2. from .common import InfoExtractor
  3. from ..networking.exceptions import HTTPError
  4. from ..utils import (
  5. ExtractorError,
  6. extract_attributes,
  7. int_or_none,
  8. remove_start,
  9. str_or_none,
  10. traverse_obj,
  11. unified_timestamp,
  12. url_or_none,
  13. )
  14. class RozhlasIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?prehravac\.rozhlas\.cz/audio/(?P<id>[0-9]+)'
  16. _TESTS = [{
  17. 'url': 'http://prehravac.rozhlas.cz/audio/3421320',
  18. 'md5': '504c902dbc9e9a1fd50326eccf02a7e2',
  19. 'info_dict': {
  20. 'id': '3421320',
  21. 'ext': 'mp3',
  22. 'title': 'Echo Pavla Klusáka (30.06.2015 21:00)',
  23. 'description': 'Osmdesátiny Terryho Rileyho jsou skvělou příležitostí proletět se elektronickými i akustickými díly zakladatatele minimalismu, který je aktivní už přes padesát let',
  24. },
  25. }, {
  26. 'url': 'http://prehravac.rozhlas.cz/audio/3421320/embed',
  27. 'only_matching': True,
  28. }]
  29. def _real_extract(self, url):
  30. audio_id = self._match_id(url)
  31. webpage = self._download_webpage(
  32. f'http://prehravac.rozhlas.cz/audio/{audio_id}', audio_id)
  33. title = self._html_search_regex(
  34. r'<h3>(.+?)</h3>\s*<p[^>]*>.*?</p>\s*<div[^>]+id=["\']player-track',
  35. webpage, 'title', default=None) or remove_start(
  36. self._og_search_title(webpage), 'Radio Wave - ')
  37. description = self._html_search_regex(
  38. r'<p[^>]+title=(["\'])(?P<url>(?:(?!\1).)+)\1[^>]*>.*?</p>\s*<div[^>]+id=["\']player-track',
  39. webpage, 'description', fatal=False, group='url')
  40. duration = int_or_none(self._search_regex(
  41. r'data-duration=["\'](\d+)', webpage, 'duration', default=None))
  42. return {
  43. 'id': audio_id,
  44. 'url': f'http://media.rozhlas.cz/_audio/{audio_id}.mp3',
  45. 'title': title,
  46. 'description': description,
  47. 'duration': duration,
  48. 'vcodec': 'none',
  49. }
  50. class RozhlasBaseIE(InfoExtractor):
  51. def _extract_formats(self, entry, audio_id):
  52. formats = []
  53. for audio in traverse_obj(entry, ('audioLinks', lambda _, v: url_or_none(v['url']))):
  54. ext = audio.get('variant')
  55. for retry in self.RetryManager():
  56. if retry.attempt > 1:
  57. self._sleep(1, audio_id)
  58. try:
  59. if ext == 'dash':
  60. formats.extend(self._extract_mpd_formats(
  61. audio['url'], audio_id, mpd_id=ext))
  62. elif ext == 'hls':
  63. formats.extend(self._extract_m3u8_formats(
  64. audio['url'], audio_id, 'm4a', m3u8_id=ext))
  65. else:
  66. formats.append({
  67. 'url': audio['url'],
  68. 'ext': ext,
  69. 'format_id': ext,
  70. 'abr': int_or_none(audio.get('bitrate')),
  71. 'acodec': ext,
  72. 'vcodec': 'none',
  73. })
  74. except ExtractorError as e:
  75. if isinstance(e.cause, HTTPError) and e.cause.status == 429:
  76. retry.error = e.cause
  77. else:
  78. self.report_warning(e.msg)
  79. return formats
  80. class RozhlasVltavaIE(RozhlasBaseIE):
  81. _VALID_URL = r'https?://(?:\w+\.rozhlas|english\.radio)\.cz/[\w-]+-(?P<id>\d+)'
  82. _TESTS = [{
  83. 'url': 'https://wave.rozhlas.cz/papej-masicko-porcujeme-a-bilancujeme-filmy-a-serialy-ktere-letos-zabily-8891337',
  84. 'md5': 'ba2fdbc1242fc16771c7695d271ec355',
  85. 'info_dict': {
  86. 'id': '8891337',
  87. 'title': 'md5:21f99739d04ab49d8c189ec711eef4ec',
  88. },
  89. 'playlist_count': 1,
  90. 'playlist': [{
  91. 'md5': 'ba2fdbc1242fc16771c7695d271ec355',
  92. 'info_dict': {
  93. 'id': '10520988',
  94. 'ext': 'mp3',
  95. 'title': 'Papej masíčko! Porcujeme a bilancujeme filmy a seriály, které to letos zabily',
  96. 'description': 'md5:1c6d29fb9564e1f17fc1bb83ae7da0bc',
  97. 'duration': 1574,
  98. 'artist': 'Aleš Stuchlý',
  99. 'channel_id': 'radio-wave',
  100. },
  101. }],
  102. }, {
  103. 'url': 'https://wave.rozhlas.cz/poslechnete-si-neklid-podcastovy-thriller-o-vine-strachu-a-vztahu-ktery-zasel-8554744',
  104. 'info_dict': {
  105. 'id': '8554744',
  106. 'title': 'Poslechněte si Neklid. Podcastový thriller o vině, strachu a vztahu, který zašel příliš daleko',
  107. },
  108. 'playlist_count': 5,
  109. 'playlist': [{
  110. 'md5': '93d4109cf8f40523699ae9c1d4600bdd',
  111. 'info_dict': {
  112. 'id': '9890713',
  113. 'ext': 'mp3',
  114. 'title': 'Neklid #1',
  115. 'description': '1. díl: Neklid: 1. díl',
  116. 'duration': 1025,
  117. 'artist': 'Josef Kokta',
  118. 'channel_id': 'radio-wave',
  119. 'chapter': 'Neklid #1',
  120. 'chapter_number': 1,
  121. },
  122. }, {
  123. 'md5': 'e9763235be4a6dcf94bc8a5bac1ca126',
  124. 'info_dict': {
  125. 'id': '9890716',
  126. 'ext': 'mp3',
  127. 'title': 'Neklid #2',
  128. 'description': '2. díl: Neklid: 2. díl',
  129. 'duration': 768,
  130. 'artist': 'Josef Kokta',
  131. 'channel_id': 'radio-wave',
  132. 'chapter': 'Neklid #2',
  133. 'chapter_number': 2,
  134. },
  135. }, {
  136. 'md5': '00b642ea94b78cc949ac84da09f87895',
  137. 'info_dict': {
  138. 'id': '9890722',
  139. 'ext': 'mp3',
  140. 'title': 'Neklid #3',
  141. 'description': '3. díl: Neklid: 3. díl',
  142. 'duration': 607,
  143. 'artist': 'Josef Kokta',
  144. 'channel_id': 'radio-wave',
  145. 'chapter': 'Neklid #3',
  146. 'chapter_number': 3,
  147. },
  148. }, {
  149. 'md5': 'faef97b1b49da7df874740f118c19dea',
  150. 'info_dict': {
  151. 'id': '9890728',
  152. 'ext': 'mp3',
  153. 'title': 'Neklid #4',
  154. 'description': '4. díl: Neklid: 4. díl',
  155. 'duration': 621,
  156. 'artist': 'Josef Kokta',
  157. 'channel_id': 'radio-wave',
  158. 'chapter': 'Neklid #4',
  159. 'chapter_number': 4,
  160. },
  161. }, {
  162. 'md5': '6e729fa39b647325b868d419c76f3efa',
  163. 'info_dict': {
  164. 'id': '9890734',
  165. 'ext': 'mp3',
  166. 'title': 'Neklid #5',
  167. 'description': '5. díl: Neklid: 5. díl',
  168. 'duration': 908,
  169. 'artist': 'Josef Kokta',
  170. 'channel_id': 'radio-wave',
  171. 'chapter': 'Neklid #5',
  172. 'chapter_number': 5,
  173. },
  174. }],
  175. }, {
  176. 'url': 'https://dvojka.rozhlas.cz/karel-siktanc-cerny-jezdec-bily-kun-napinava-pohadka-o-tajemnem-prizraku-8946969',
  177. 'info_dict': {
  178. 'id': '8946969',
  179. 'title': 'Karel Šiktanc: Černý jezdec, bílý kůň. Napínavá pohádka o tajemném přízraku',
  180. },
  181. 'playlist_count': 1,
  182. 'playlist': [{
  183. 'info_dict': {
  184. 'id': '10631121',
  185. 'ext': 'm4a',
  186. 'title': 'Karel Šiktanc: Černý jezdec, bílý kůň. Napínavá pohádka o tajemném přízraku',
  187. 'description': 'Karel Šiktanc: Černý jezdec, bílý kůň',
  188. 'duration': 2656,
  189. 'artist': 'Tvůrčí skupina Drama a literatura',
  190. 'channel_id': 'dvojka',
  191. },
  192. }],
  193. 'params': {'skip_download': 'dash'},
  194. }]
  195. def _extract_video(self, entry):
  196. audio_id = entry['meta']['ga']['contentId']
  197. chapter_number = traverse_obj(entry, ('meta', 'ga', 'contentSerialPart', {int_or_none}))
  198. return {
  199. 'id': audio_id,
  200. 'chapter': traverse_obj(entry, ('meta', 'ga', 'contentNameShort')) if chapter_number else None,
  201. 'chapter_number': chapter_number,
  202. 'formats': self._extract_formats(entry, audio_id),
  203. **traverse_obj(entry, {
  204. 'title': ('meta', 'ga', 'contentName'),
  205. 'description': 'title',
  206. 'duration': ('duration', {int_or_none}),
  207. 'artist': ('meta', 'ga', 'contentAuthor'),
  208. 'channel_id': ('meta', 'ga', 'contentCreator'),
  209. }),
  210. }
  211. def _real_extract(self, url):
  212. video_id = self._match_id(url)
  213. webpage = self._download_webpage(url, video_id)
  214. # FIXME: Use get_element_text_and_html_by_tag when it accepts less strict html
  215. data = self._parse_json(extract_attributes(self._search_regex(
  216. r'(<div class="mujRozhlasPlayer" data-player=\'[^\']+\'>)',
  217. webpage, 'player'))['data-player'], video_id)['data']
  218. return {
  219. '_type': 'playlist',
  220. 'id': str_or_none(data.get('embedId')) or video_id,
  221. 'title': traverse_obj(data, ('series', 'title')),
  222. 'entries': map(self._extract_video, data['playlist']),
  223. }
  224. class MujRozhlasIE(RozhlasBaseIE):
  225. _VALID_URL = r'https?://(?:www\.)?mujrozhlas\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  226. _TESTS = [{
  227. # single episode extraction
  228. 'url': 'https://www.mujrozhlas.cz/vykopavky/ach-jo-zase-teleci-rizek-je-mnohem-min-cesky-nez-jsme-si-mysleli',
  229. 'md5': '6f8fd68663e64936623e67c152a669e0',
  230. 'info_dict': {
  231. 'id': '10787730',
  232. 'ext': 'mp3',
  233. 'title': 'Ach jo, zase to telecí! Řízek je mnohem míň český, než jsme si mysleli',
  234. 'description': 'md5:db7141e9caaedc9041ec7cefb9a62908',
  235. 'timestamp': 1684915200,
  236. 'modified_timestamp': 1687550432,
  237. 'series': 'Vykopávky',
  238. 'thumbnail': 'https://portal.rozhlas.cz/sites/default/files/images/84377046610af6ddc54d910b1dd7a22b.jpg',
  239. 'channel_id': 'radio-wave',
  240. 'upload_date': '20230524',
  241. 'modified_date': '20230623',
  242. },
  243. }, {
  244. # serial extraction
  245. 'url': 'https://www.mujrozhlas.cz/radiokniha/jaroslava-janackova-pribeh-tajemneho-psani-o-pramenech-genezi-babicky',
  246. 'playlist_mincount': 7,
  247. 'info_dict': {
  248. 'id': 'bb2b5f4e-ffb4-35a6-a34a-046aa62d6f6b',
  249. 'title': 'Jaroslava Janáčková: Příběh tajemného psaní. O pramenech a genezi Babičky',
  250. 'description': 'md5:7434d8fac39ac9fee6df098e11dfb1be',
  251. },
  252. }, {
  253. # show extraction
  254. 'url': 'https://www.mujrozhlas.cz/nespavci',
  255. 'playlist_mincount': 14,
  256. 'info_dict': {
  257. 'id': '09db9b37-d0f4-368c-986a-d3439f741f08',
  258. 'title': 'Nespavci',
  259. 'description': 'md5:c430adcbf9e2b9eac88b745881e814dc',
  260. },
  261. }, {
  262. # serialPart
  263. 'url': 'https://www.mujrozhlas.cz/povidka/gustavo-adolfo-becquer-hora-duchu',
  264. 'info_dict': {
  265. 'id': '8889035',
  266. 'ext': 'm4a',
  267. 'title': 'Gustavo Adolfo Bécquer: Hora duchů',
  268. 'description': 'md5:343a15257b376c276e210b78e900ffea',
  269. 'chapter': 'Hora duchů a Polibek – dva tajemné příběhy Gustava Adolfa Bécquera',
  270. 'thumbnail': 'https://portal.rozhlas.cz/sites/default/files/images/2adfe1387fb140634be725c1ccf26214.jpg',
  271. 'timestamp': 1708173000,
  272. 'episode': 'Episode 1',
  273. 'episode_number': 1,
  274. 'series': 'Povídka',
  275. 'modified_date': '20240217',
  276. 'upload_date': '20240217',
  277. 'modified_timestamp': 1708173198,
  278. 'channel_id': 'vltava',
  279. },
  280. 'params': {'skip_download': 'dash'},
  281. }]
  282. def _call_api(self, path, item_id, msg='API JSON'):
  283. return self._download_json(
  284. f'https://api.mujrozhlas.cz/{path}/{item_id}', item_id,
  285. note=f'Downloading {msg}', errnote=f'Failed to download {msg}')['data']
  286. def _extract_audio_entry(self, entry):
  287. audio_id = entry['meta']['ga']['contentId']
  288. return {
  289. 'id': audio_id,
  290. 'formats': self._extract_formats(entry['attributes'], audio_id),
  291. **traverse_obj(entry, {
  292. 'title': ('attributes', 'title'),
  293. 'description': ('attributes', 'description'),
  294. 'episode_number': ('attributes', 'part'),
  295. 'series': ('attributes', 'mirroredShow', 'title'),
  296. 'chapter': ('attributes', 'mirroredSerial', 'title'),
  297. 'artist': ('meta', 'ga', 'contentAuthor'),
  298. 'channel_id': ('meta', 'ga', 'contentCreator'),
  299. 'timestamp': ('attributes', 'since', {unified_timestamp}),
  300. 'modified_timestamp': ('attributes', 'updated', {unified_timestamp}),
  301. 'thumbnail': ('attributes', 'asset', 'url', {url_or_none}),
  302. }),
  303. }
  304. def _entries(self, api_url, playlist_id):
  305. for page in itertools.count(1):
  306. episodes = self._download_json(
  307. api_url, playlist_id, note=f'Downloading episodes page {page}',
  308. errnote=f'Failed to download episodes page {page}', fatal=False)
  309. for episode in traverse_obj(episodes, ('data', lambda _, v: v['meta']['ga']['contentId'])):
  310. yield self._extract_audio_entry(episode)
  311. api_url = traverse_obj(episodes, ('links', 'next', {url_or_none}))
  312. if not api_url:
  313. break
  314. def _real_extract(self, url):
  315. display_id = self._match_id(url)
  316. webpage = self._download_webpage(url, display_id)
  317. info = self._search_json(r'\bvar\s+dl\s*=', webpage, 'info json', display_id)
  318. entity = info['siteEntityBundle']
  319. if entity in ('episode', 'serialPart'):
  320. return self._extract_audio_entry(self._call_api(
  321. 'episodes', info['contentId'], 'episode info API JSON'))
  322. elif entity in ('show', 'serial'):
  323. playlist_id = info['contentShow'].split(':')[0] if entity == 'show' else info['contentId']
  324. data = self._call_api(f'{entity}s', playlist_id, f'{entity} playlist JSON')
  325. api_url = data['relationships']['episodes']['links']['related']
  326. return self.playlist_result(
  327. self._entries(api_url, playlist_id), playlist_id,
  328. **traverse_obj(data, ('attributes', {
  329. 'title': 'title',
  330. 'description': 'description',
  331. })))
  332. else:
  333. # `entity == 'person'` not implemented yet by API, ref:
  334. # https://api.mujrozhlas.cz/persons/8367e456-2a57-379a-91bb-e699619bea49/participation
  335. raise ExtractorError(f'Unsupported entity type "{entity}"')