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.

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