abc.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import hashlib
  2. import hmac
  3. import re
  4. import time
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. dict_get,
  9. int_or_none,
  10. js_to_json,
  11. parse_iso8601,
  12. str_or_none,
  13. traverse_obj,
  14. try_get,
  15. unescapeHTML,
  16. update_url_query,
  17. url_or_none,
  18. )
  19. class ABCIE(InfoExtractor):
  20. IE_NAME = 'abc.net.au'
  21. _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/(?:news|btn)/(?:[^/]+/){1,4}(?P<id>\d{5,})'
  22. _TESTS = [{
  23. 'url': 'http://www.abc.net.au/news/2014-11-05/australia-to-staff-ebola-treatment-centre-in-sierra-leone/5868334',
  24. 'md5': 'cb3dd03b18455a661071ee1e28344d9f',
  25. 'info_dict': {
  26. 'id': '5868334',
  27. 'ext': 'mp4',
  28. 'title': 'Australia to help staff Ebola treatment centre in Sierra Leone',
  29. 'description': 'md5:809ad29c67a05f54eb41f2a105693a67',
  30. },
  31. 'skip': 'this video has expired',
  32. }, {
  33. 'url': 'http://www.abc.net.au/news/2015-08-17/warren-entsch-introduces-same-sex-marriage-bill/6702326',
  34. 'md5': '4ebd61bdc82d9a8b722f64f1f4b4d121',
  35. 'info_dict': {
  36. 'id': 'NvqvPeNZsHU',
  37. 'ext': 'mp4',
  38. 'upload_date': '20150816',
  39. 'uploader': 'ABC News (Australia)',
  40. 'description': 'Government backbencher Warren Entsch introduces a cross-party sponsored bill to legalise same-sex marriage, saying the bill is designed to promote "an inclusive Australia, not a divided one.". Read more here: http://ab.co/1Mwc6ef',
  41. 'uploader_id': 'NewsOnABC',
  42. 'title': 'Marriage Equality: Warren Entsch introduces same sex marriage bill',
  43. },
  44. 'add_ie': ['Youtube'],
  45. 'skip': 'Not accessible from Travis CI server',
  46. }, {
  47. 'url': 'http://www.abc.net.au/news/2015-10-23/nab-lifts-interest-rates-following-westpac-and-cba/6880080',
  48. 'md5': 'b96eee7c9edf4fc5a358a0252881cc1f',
  49. 'info_dict': {
  50. 'id': '6880080',
  51. 'ext': 'mp3',
  52. 'title': 'NAB lifts interest rates, following Westpac and CBA',
  53. 'description': 'md5:f13d8edc81e462fce4a0437c7dc04728',
  54. },
  55. }, {
  56. 'url': 'http://www.abc.net.au/news/2015-10-19/6866214',
  57. 'only_matching': True,
  58. }, {
  59. 'url': 'https://www.abc.net.au/btn/classroom/wwi-centenary/10527914',
  60. 'info_dict': {
  61. 'id': '10527914',
  62. 'ext': 'mp4',
  63. 'title': 'WWI Centenary',
  64. 'description': 'md5:c2379ec0ca84072e86b446e536954546',
  65. },
  66. }, {
  67. 'url': 'https://www.abc.net.au/news/programs/the-world/2020-06-10/black-lives-matter-protests-spawn-support-for/12342074',
  68. 'info_dict': {
  69. 'id': '12342074',
  70. 'ext': 'mp4',
  71. 'title': 'Black Lives Matter protests spawn support for Papuans in Indonesia',
  72. 'description': 'md5:2961a17dc53abc558589ccd0fb8edd6f',
  73. },
  74. }, {
  75. 'url': 'https://www.abc.net.au/btn/newsbreak/btn-newsbreak-20200814/12560476',
  76. 'info_dict': {
  77. 'id': 'tDL8Ld4dK_8',
  78. 'ext': 'mp4',
  79. 'title': 'Fortnite Banned From Apple and Google App Stores',
  80. 'description': 'md5:a6df3f36ce8f816b74af4bd6462f5651',
  81. 'upload_date': '20200813',
  82. 'uploader': 'Behind the News',
  83. 'uploader_id': 'behindthenews',
  84. },
  85. }, {
  86. 'url': 'https://www.abc.net.au/news/2023-06-25/wagner-boss-orders-troops-back-to-bases-to-avoid-bloodshed/102520540',
  87. 'info_dict': {
  88. 'id': '102520540',
  89. 'title': 'Wagner Group retreating from Russia, leader Prigozhin to move to Belarus',
  90. 'ext': 'mp4',
  91. 'description': 'Wagner troops leave Rostov-on-Don and\xa0Yevgeny Prigozhin will move to Belarus under a deal brokered by Belarusian President Alexander Lukashenko to end the mutiny.',
  92. 'thumbnail': 'https://live-production.wcms.abc-cdn.net.au/0c170f5b57f0105c432f366c0e8e267b?impolicy=wcms_crop_resize&cropH=2813&cropW=5000&xPos=0&yPos=249&width=862&height=485',
  93. },
  94. }]
  95. def _real_extract(self, url):
  96. video_id = self._match_id(url)
  97. webpage = self._download_webpage(url, video_id)
  98. mobj = re.search(r'<a\s+href="(?P<url>[^"]+)"\s+data-duration="\d+"\s+title="Download audio directly">', webpage)
  99. if mobj:
  100. urls_info = mobj.groupdict()
  101. youtube = False
  102. video = False
  103. else:
  104. mobj = re.search(r'<a href="(?P<url>http://www\.youtube\.com/watch\?v=[^"]+)"><span><strong>External Link:</strong>',
  105. webpage)
  106. if mobj is None:
  107. mobj = re.search(r'<iframe width="100%" src="(?P<url>//www\.youtube-nocookie\.com/embed/[^?"]+)', webpage)
  108. if mobj:
  109. urls_info = mobj.groupdict()
  110. youtube = True
  111. video = True
  112. if mobj is None:
  113. mobj = re.search(r'(?P<type>)"(?:sources|files|renditions)":\s*(?P<json_data>\[[^\]]+\])', webpage)
  114. if mobj is None:
  115. mobj = re.search(
  116. r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
  117. webpage)
  118. if mobj is None:
  119. expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
  120. if expired:
  121. raise ExtractorError(f'{self.IE_NAME} said: {expired}', expected=True)
  122. raise ExtractorError('Unable to extract video urls')
  123. urls_info = self._parse_json(
  124. mobj.group('json_data'), video_id, transform_source=js_to_json)
  125. youtube = mobj.group('type') == 'YouTube'
  126. video = mobj.group('type') == 'Video' or traverse_obj(
  127. urls_info, (0, ('contentType', 'MIMEType')), get_all=False) == 'video/mp4'
  128. if not isinstance(urls_info, list):
  129. urls_info = [urls_info]
  130. if youtube:
  131. return self.playlist_result([
  132. self.url_result(url_info['url']) for url_info in urls_info])
  133. formats = []
  134. for url_info in urls_info:
  135. height = int_or_none(url_info.get('height'))
  136. bitrate = int_or_none(url_info.get('bitrate'))
  137. width = int_or_none(url_info.get('width'))
  138. format_id = None
  139. mobj = re.search(r'_(?:(?P<height>\d+)|(?P<bitrate>\d+)k)\.mp4$', url_info['url'])
  140. if mobj:
  141. height_from_url = mobj.group('height')
  142. if height_from_url:
  143. height = height or int_or_none(height_from_url)
  144. width = width or int_or_none(url_info.get('label'))
  145. else:
  146. bitrate = bitrate or int_or_none(mobj.group('bitrate'))
  147. format_id = str_or_none(url_info.get('label'))
  148. formats.append({
  149. 'url': url_info['url'],
  150. 'vcodec': url_info.get('codec') if video else 'none',
  151. 'width': width,
  152. 'height': height,
  153. 'tbr': bitrate,
  154. 'filesize': int_or_none(url_info.get('filesize')),
  155. 'format_id': format_id,
  156. })
  157. return {
  158. 'id': video_id,
  159. 'title': self._og_search_title(webpage),
  160. 'formats': formats,
  161. 'description': self._og_search_description(webpage),
  162. 'thumbnail': self._og_search_thumbnail(webpage),
  163. }
  164. class ABCIViewIE(InfoExtractor):
  165. IE_NAME = 'abc.net.au:iview'
  166. _VALID_URL = r'https?://iview\.abc\.net\.au/(?:[^/]+/)*video/(?P<id>[^/?#]+)'
  167. _GEO_COUNTRIES = ['AU']
  168. _TESTS = [{
  169. 'url': 'https://iview.abc.net.au/show/utopia/series/1/video/CO1211V001S00',
  170. 'md5': '52a942bfd7a0b79a6bfe9b4ce6c9d0ed',
  171. 'info_dict': {
  172. 'id': 'CO1211V001S00',
  173. 'ext': 'mp4',
  174. 'title': 'Series 1 Ep 1 Wood For The Trees',
  175. 'series': 'Utopia',
  176. 'description': 'md5:0cfb2c183c1b952d1548fd65c8a95c00',
  177. 'upload_date': '20230726',
  178. 'uploader_id': 'abc1',
  179. 'series_id': 'CO1211V',
  180. 'episode_id': 'CO1211V001S00',
  181. 'season_number': 1,
  182. 'season': 'Season 1',
  183. 'episode_number': 1,
  184. 'episode': 'Wood For The Trees',
  185. 'thumbnail': 'https://cdn.iview.abc.net.au/thumbs/i/co/CO1211V001S00_5ad8353f4df09_1280.jpg',
  186. 'timestamp': 1690403700,
  187. },
  188. 'params': {
  189. 'skip_download': True,
  190. },
  191. }, {
  192. 'note': 'No episode name',
  193. 'url': 'https://iview.abc.net.au/show/gruen/series/11/video/LE1927H001S00',
  194. 'md5': '67715ce3c78426b11ba167d875ac6abf',
  195. 'info_dict': {
  196. 'id': 'LE1927H001S00',
  197. 'ext': 'mp4',
  198. 'title': 'Series 11 Ep 1',
  199. 'series': 'Gruen',
  200. 'description': 'md5:52cc744ad35045baf6aded2ce7287f67',
  201. 'upload_date': '20190925',
  202. 'uploader_id': 'abc1',
  203. 'series_id': 'LE1927H',
  204. 'episode_id': 'LE1927H001S00',
  205. 'season_number': 11,
  206. 'season': 'Season 11',
  207. 'episode_number': 1,
  208. 'episode': 'Episode 1',
  209. 'thumbnail': 'https://cdn.iview.abc.net.au/thumbs/i/le/LE1927H001S00_5d954fbd79e25_1280.jpg',
  210. 'timestamp': 1569445289,
  211. },
  212. 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
  213. 'params': {
  214. 'skip_download': True,
  215. },
  216. }, {
  217. 'note': 'No episode number',
  218. 'url': 'https://iview.abc.net.au/show/four-corners/series/2022/video/NC2203H039S00',
  219. 'md5': '77cb7d8434440e3b28fbebe331c2456a',
  220. 'info_dict': {
  221. 'id': 'NC2203H039S00',
  222. 'ext': 'mp4',
  223. 'title': 'Series 2022 Locking Up Kids',
  224. 'series': 'Four Corners',
  225. 'description': 'md5:54829ca108846d1a70e1fcce2853e720',
  226. 'upload_date': '20221114',
  227. 'uploader_id': 'abc1',
  228. 'series_id': 'NC2203H',
  229. 'episode_id': 'NC2203H039S00',
  230. 'season_number': 2022,
  231. 'season': 'Season 2022',
  232. 'episode': 'Locking Up Kids',
  233. 'thumbnail': 'https://cdn.iview.abc.net.au/thumbs/i/nc/NC2203H039S00_636d8a0944a22_1920.jpg',
  234. 'timestamp': 1668460497,
  235. },
  236. 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
  237. 'params': {
  238. 'skip_download': True,
  239. },
  240. }, {
  241. 'note': 'No episode name or number',
  242. 'url': 'https://iview.abc.net.au/show/landline/series/2021/video/RF2004Q043S00',
  243. 'md5': '2e17dec06b13cc81dc119d2565289396',
  244. 'info_dict': {
  245. 'id': 'RF2004Q043S00',
  246. 'ext': 'mp4',
  247. 'title': 'Series 2021',
  248. 'series': 'Landline',
  249. 'description': 'md5:c9f30d9c0c914a7fd23842f6240be014',
  250. 'upload_date': '20211205',
  251. 'uploader_id': 'abc1',
  252. 'series_id': 'RF2004Q',
  253. 'episode_id': 'RF2004Q043S00',
  254. 'season_number': 2021,
  255. 'season': 'Season 2021',
  256. 'thumbnail': 'https://cdn.iview.abc.net.au/thumbs/i/rf/RF2004Q043S00_61a950639dbc0_1920.jpg',
  257. 'timestamp': 1638710705,
  258. },
  259. 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
  260. 'params': {
  261. 'skip_download': True,
  262. },
  263. }]
  264. def _real_extract(self, url):
  265. video_id = self._match_id(url)
  266. video_params = self._download_json(
  267. 'https://iview.abc.net.au/api/programs/' + video_id, video_id)
  268. title = unescapeHTML(video_params.get('title') or video_params['seriesTitle'])
  269. stream = next(s for s in video_params['playlist'] if s.get('type') in ('program', 'livestream'))
  270. house_number = video_params.get('episodeHouseNumber') or video_id
  271. path = f'/auth/hls/sign?ts={int(time.time())}&hn={house_number}&d=android-tablet'
  272. sig = hmac.new(
  273. b'android.content.res.Resources',
  274. path.encode(), hashlib.sha256).hexdigest()
  275. token = self._download_webpage(
  276. f'http://iview.abc.net.au{path}&sig={sig}', video_id)
  277. def tokenize_url(url, token):
  278. return update_url_query(url, {
  279. 'hdnea': token,
  280. })
  281. for sd in ('1080', '720', 'sd', 'sd-low'):
  282. sd_url = try_get(
  283. stream, lambda x: x['streams']['hls'][sd], str)
  284. if not sd_url:
  285. continue
  286. formats = self._extract_m3u8_formats(
  287. tokenize_url(sd_url, token), video_id, 'mp4',
  288. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
  289. if formats:
  290. break
  291. subtitles = {}
  292. src_vtt = stream.get('captions', {}).get('src-vtt')
  293. if src_vtt:
  294. subtitles['en'] = [{
  295. 'url': src_vtt,
  296. 'ext': 'vtt',
  297. }]
  298. is_live = video_params.get('livestream') == '1'
  299. return {
  300. 'id': video_id,
  301. 'title': title,
  302. 'description': video_params.get('description'),
  303. 'thumbnail': video_params.get('thumbnail'),
  304. 'duration': int_or_none(video_params.get('eventDuration')),
  305. 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
  306. 'series': unescapeHTML(video_params.get('seriesTitle')),
  307. 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
  308. 'season_number': int_or_none(self._search_regex(
  309. r'\bSeries\s+(\d+)\b', title, 'season number', default=None)),
  310. 'episode_number': int_or_none(self._search_regex(
  311. r'\bEp\s+(\d+)\b', title, 'episode number', default=None)),
  312. 'episode_id': house_number,
  313. 'episode': self._search_regex(
  314. r'^(?:Series\s+\d+)?\s*(?:Ep\s+\d+)?\s*(.*)$', title, 'episode', default='') or None,
  315. 'uploader_id': video_params.get('channel'),
  316. 'formats': formats,
  317. 'subtitles': subtitles,
  318. 'is_live': is_live,
  319. }
  320. class ABCIViewShowSeriesIE(InfoExtractor):
  321. IE_NAME = 'abc.net.au:iview:showseries'
  322. _VALID_URL = r'https?://iview\.abc\.net\.au/show/(?P<id>[^/]+)(?:/series/\d+)?$'
  323. _GEO_COUNTRIES = ['AU']
  324. _TESTS = [{
  325. 'url': 'https://iview.abc.net.au/show/upper-middle-bogan',
  326. 'info_dict': {
  327. 'id': '124870-1',
  328. 'title': 'Series 1',
  329. 'description': 'md5:93119346c24a7c322d446d8eece430ff',
  330. 'series': 'Upper Middle Bogan',
  331. 'season': 'Series 1',
  332. 'thumbnail': r're:^https?://cdn\.iview\.abc\.net\.au/thumbs/.*\.jpg$',
  333. },
  334. 'playlist_count': 8,
  335. }, {
  336. 'url': 'https://iview.abc.net.au/show/upper-middle-bogan',
  337. 'info_dict': {
  338. 'id': 'CO1108V001S00',
  339. 'ext': 'mp4',
  340. 'title': 'Series 1 Ep 1 I\'m A Swan',
  341. 'description': 'md5:7b676758c1de11a30b79b4d301e8da93',
  342. 'series': 'Upper Middle Bogan',
  343. 'uploader_id': 'abc1',
  344. 'upload_date': '20210630',
  345. 'timestamp': 1625036400,
  346. },
  347. 'params': {
  348. 'noplaylist': True,
  349. 'skip_download': 'm3u8',
  350. },
  351. }, {
  352. # 'videoEpisodes' is a dict with `items` key
  353. 'url': 'https://iview.abc.net.au/show/7-30-mark-humphries-satire',
  354. 'info_dict': {
  355. 'id': '178458-0',
  356. 'title': 'Episodes',
  357. 'description': 'Satirist Mark Humphries brings his unique perspective on current political events for 7.30.',
  358. 'series': '7.30 Mark Humphries Satire',
  359. 'season': 'Episodes',
  360. 'thumbnail': r're:^https?://cdn\.iview\.abc\.net\.au/thumbs/.*\.jpg$',
  361. },
  362. 'playlist_count': 15,
  363. }]
  364. def _real_extract(self, url):
  365. show_id = self._match_id(url)
  366. webpage = self._download_webpage(url, show_id)
  367. webpage_data = self._search_regex(
  368. r'window\.__INITIAL_STATE__\s*=\s*[\'"](.+?)[\'"]\s*;',
  369. webpage, 'initial state')
  370. video_data = self._parse_json(
  371. unescapeHTML(webpage_data).encode().decode('unicode_escape'), show_id)
  372. video_data = video_data['route']['pageData']['_embedded']
  373. highlight = try_get(video_data, lambda x: x['highlightVideo']['shareUrl'])
  374. if not self._yes_playlist(show_id, bool(highlight), video_label='highlight video'):
  375. return self.url_result(highlight, ie=ABCIViewIE.ie_key())
  376. series = video_data['selectedSeries']
  377. return {
  378. '_type': 'playlist',
  379. 'entries': [self.url_result(episode_url, ABCIViewIE)
  380. for episode_url in traverse_obj(series, (
  381. '_embedded', 'videoEpisodes', (None, 'items'), ..., 'shareUrl', {url_or_none}))],
  382. 'id': series.get('id'),
  383. 'title': dict_get(series, ('title', 'displaySubtitle')),
  384. 'description': series.get('description'),
  385. 'series': dict_get(series, ('showTitle', 'displayTitle')),
  386. 'season': dict_get(series, ('title', 'displaySubtitle')),
  387. 'thumbnail': traverse_obj(
  388. series, 'thumbnail', ('images', lambda _, v: v['name'] == 'seriesThumbnail', 'url'), get_all=False),
  389. }