abc.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from __future__ import unicode_literals
  2. import hashlib
  3. import hmac
  4. import re
  5. import time
  6. from .common import InfoExtractor
  7. from ..compat import compat_str
  8. from ..utils import (
  9. ExtractorError,
  10. js_to_json,
  11. int_or_none,
  12. parse_iso8601,
  13. try_get,
  14. unescapeHTML,
  15. update_url_query,
  16. )
  17. class ABCIE(InfoExtractor):
  18. IE_NAME = 'abc.net.au'
  19. _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/news/(?:[^/]+/){1,2}(?P<id>\d+)'
  20. _TESTS = [{
  21. 'url': 'http://www.abc.net.au/news/2014-11-05/australia-to-staff-ebola-treatment-centre-in-sierra-leone/5868334',
  22. 'md5': 'cb3dd03b18455a661071ee1e28344d9f',
  23. 'info_dict': {
  24. 'id': '5868334',
  25. 'ext': 'mp4',
  26. 'title': 'Australia to help staff Ebola treatment centre in Sierra Leone',
  27. 'description': 'md5:809ad29c67a05f54eb41f2a105693a67',
  28. },
  29. 'skip': 'this video has expired',
  30. }, {
  31. 'url': 'http://www.abc.net.au/news/2015-08-17/warren-entsch-introduces-same-sex-marriage-bill/6702326',
  32. 'md5': 'db2a5369238b51f9811ad815b69dc086',
  33. 'info_dict': {
  34. 'id': 'NvqvPeNZsHU',
  35. 'ext': 'mp4',
  36. 'upload_date': '20150816',
  37. 'uploader': 'ABC News (Australia)',
  38. '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',
  39. 'uploader_id': 'NewsOnABC',
  40. 'title': 'Marriage Equality: Warren Entsch introduces same sex marriage bill',
  41. },
  42. 'add_ie': ['Youtube'],
  43. 'skip': 'Not accessible from Travis CI server',
  44. }, {
  45. 'url': 'http://www.abc.net.au/news/2015-10-23/nab-lifts-interest-rates-following-westpac-and-cba/6880080',
  46. 'md5': 'b96eee7c9edf4fc5a358a0252881cc1f',
  47. 'info_dict': {
  48. 'id': '6880080',
  49. 'ext': 'mp3',
  50. 'title': 'NAB lifts interest rates, following Westpac and CBA',
  51. 'description': 'md5:f13d8edc81e462fce4a0437c7dc04728',
  52. },
  53. }, {
  54. 'url': 'http://www.abc.net.au/news/2015-10-19/6866214',
  55. 'only_matching': True,
  56. }]
  57. def _real_extract(self, url):
  58. video_id = self._match_id(url)
  59. webpage = self._download_webpage(url, video_id)
  60. mobj = re.search(
  61. r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
  62. webpage)
  63. if mobj is None:
  64. expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
  65. if expired:
  66. raise ExtractorError('%s said: %s' % (self.IE_NAME, expired), expected=True)
  67. raise ExtractorError('Unable to extract video urls')
  68. urls_info = self._parse_json(
  69. mobj.group('json_data'), video_id, transform_source=js_to_json)
  70. if not isinstance(urls_info, list):
  71. urls_info = [urls_info]
  72. if mobj.group('type') == 'YouTube':
  73. return self.playlist_result([
  74. self.url_result(url_info['url']) for url_info in urls_info])
  75. formats = [{
  76. 'url': url_info['url'],
  77. 'vcodec': url_info.get('codec') if mobj.group('type') == 'Video' else 'none',
  78. 'width': int_or_none(url_info.get('width')),
  79. 'height': int_or_none(url_info.get('height')),
  80. 'tbr': int_or_none(url_info.get('bitrate')),
  81. 'filesize': int_or_none(url_info.get('filesize')),
  82. } for url_info in urls_info]
  83. self._sort_formats(formats)
  84. return {
  85. 'id': video_id,
  86. 'title': self._og_search_title(webpage),
  87. 'formats': formats,
  88. 'description': self._og_search_description(webpage),
  89. 'thumbnail': self._og_search_thumbnail(webpage),
  90. }
  91. class ABCIViewIE(InfoExtractor):
  92. IE_NAME = 'abc.net.au:iview'
  93. _VALID_URL = r'https?://iview\.abc\.net\.au/programs/[^/]+/(?P<id>[^/?#]+)'
  94. _GEO_COUNTRIES = ['AU']
  95. # ABC iview programs are normally available for 14 days only.
  96. _TESTS = [{
  97. 'url': 'https://iview.abc.net.au/programs/ben-and-hollys-little-kingdom/ZY9247A021S00',
  98. 'md5': 'cde42d728b3b7c2b32b1b94b4a548afc',
  99. 'info_dict': {
  100. 'id': 'ZY9247A021S00',
  101. 'ext': 'mp4',
  102. 'title': "Gaston's Visit",
  103. 'series': "Ben And Holly's Little Kingdom",
  104. 'description': 'md5:18db170ad71cf161e006a4c688e33155',
  105. 'upload_date': '20180318',
  106. 'uploader_id': 'abc4kids',
  107. 'timestamp': 1521400959,
  108. },
  109. 'params': {
  110. 'skip_download': True,
  111. },
  112. }]
  113. def _real_extract(self, url):
  114. video_id = self._match_id(url)
  115. webpage = self._download_webpage(url, video_id)
  116. video_params = self._parse_json(self._search_regex(
  117. r'videoParams\s*=\s*({.+?});', webpage, 'video params'), video_id)
  118. title = video_params.get('title') or video_params['seriesTitle']
  119. stream = next(s for s in video_params['playlist'] if s.get('type') == 'program')
  120. house_number = video_params.get('episodeHouseNumber')
  121. path = '/auth/hls/sign?ts={0}&hn={1}&d=android-mobile'.format(
  122. int(time.time()), house_number)
  123. sig = hmac.new(
  124. 'android.content.res.Resources'.encode('utf-8'),
  125. path.encode('utf-8'), hashlib.sha256).hexdigest()
  126. token = self._download_webpage(
  127. 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
  128. def tokenize_url(url, token):
  129. return update_url_query(url, {
  130. 'hdnea': token,
  131. })
  132. for sd in ('sd', 'sd-low'):
  133. sd_url = try_get(
  134. stream, lambda x: x['streams']['hls'][sd], compat_str)
  135. if not sd_url:
  136. continue
  137. formats = self._extract_m3u8_formats(
  138. tokenize_url(sd_url, token), video_id, 'mp4',
  139. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
  140. if formats:
  141. break
  142. self._sort_formats(formats)
  143. subtitles = {}
  144. src_vtt = stream.get('captions', {}).get('src-vtt')
  145. if src_vtt:
  146. subtitles['en'] = [{
  147. 'url': src_vtt,
  148. 'ext': 'vtt',
  149. }]
  150. return {
  151. 'id': video_id,
  152. 'title': unescapeHTML(title),
  153. 'description': self._html_search_meta(['og:description', 'twitter:description'], webpage),
  154. 'thumbnail': self._html_search_meta(['og:image', 'twitter:image:src'], webpage),
  155. 'duration': int_or_none(video_params.get('eventDuration')),
  156. 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
  157. 'series': unescapeHTML(video_params.get('seriesTitle')),
  158. 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
  159. 'episode_number': int_or_none(self._html_search_meta('episodeNumber', webpage, default=None)),
  160. 'episode': self._html_search_meta('episode_title', webpage, default=None),
  161. 'uploader_id': video_params.get('channel'),
  162. 'formats': formats,
  163. 'subtitles': subtitles,
  164. }