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.

193 lines
7.4 KiB

  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/(?:[^/]+/)*video/(?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/show/ben-and-hollys-little-kingdom/series/0/video/ZX9371A050S00',
  98. 'md5': 'cde42d728b3b7c2b32b1b94b4a548afc',
  99. 'info_dict': {
  100. 'id': 'ZX9371A050S00',
  101. 'ext': 'mp4',
  102. 'title': "Gaston's Birthday",
  103. 'series': "Ben And Holly's Little Kingdom",
  104. 'description': 'md5:f9de914d02f226968f598ac76f105bcf',
  105. 'upload_date': '20180604',
  106. 'uploader_id': 'abc4kids',
  107. 'timestamp': 1528140219,
  108. },
  109. 'params': {
  110. 'skip_download': True,
  111. },
  112. }]
  113. def _real_extract(self, url):
  114. video_id = self._match_id(url)
  115. video_params = self._download_json(
  116. 'https://iview.abc.net.au/api/programs/' + video_id, video_id)
  117. title = unescapeHTML(video_params.get('title') or video_params['seriesTitle'])
  118. stream = next(s for s in video_params['playlist'] if s.get('type') in ('program', 'livestream'))
  119. house_number = video_params.get('episodeHouseNumber') or video_id
  120. path = '/auth/hls/sign?ts={0}&hn={1}&d=android-tablet'.format(
  121. int(time.time()), house_number)
  122. sig = hmac.new(
  123. b'android.content.res.Resources',
  124. path.encode('utf-8'), hashlib.sha256).hexdigest()
  125. token = self._download_webpage(
  126. 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
  127. def tokenize_url(url, token):
  128. return update_url_query(url, {
  129. 'hdnea': token,
  130. })
  131. for sd in ('sd', 'sd-low'):
  132. sd_url = try_get(
  133. stream, lambda x: x['streams']['hls'][sd], compat_str)
  134. if not sd_url:
  135. continue
  136. formats = self._extract_m3u8_formats(
  137. tokenize_url(sd_url, token), video_id, 'mp4',
  138. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
  139. if formats:
  140. break
  141. self._sort_formats(formats)
  142. subtitles = {}
  143. src_vtt = stream.get('captions', {}).get('src-vtt')
  144. if src_vtt:
  145. subtitles['en'] = [{
  146. 'url': src_vtt,
  147. 'ext': 'vtt',
  148. }]
  149. is_live = video_params.get('livestream') == '1'
  150. if is_live:
  151. title = self._live_title(title)
  152. return {
  153. 'id': video_id,
  154. 'title': title,
  155. 'description': video_params.get('description'),
  156. 'thumbnail': video_params.get('thumbnail'),
  157. 'duration': int_or_none(video_params.get('eventDuration')),
  158. 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
  159. 'series': unescapeHTML(video_params.get('seriesTitle')),
  160. 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
  161. 'season_number': int_or_none(self._search_regex(
  162. r'\bSeries\s+(\d+)\b', title, 'season number', default=None)),
  163. 'episode_number': int_or_none(self._search_regex(
  164. r'\bEp\s+(\d+)\b', title, 'episode number', default=None)),
  165. 'episode_id': house_number,
  166. 'uploader_id': video_params.get('channel'),
  167. 'formats': formats,
  168. 'subtitles': subtitles,
  169. 'is_live': is_live,
  170. }