2
0

abc.py 7.2 KB

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