nhl.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urlparse,
  7. compat_urllib_parse,
  8. compat_urllib_parse_urlparse
  9. )
  10. from ..utils import (
  11. unified_strdate,
  12. )
  13. class NHLBaseInfoExtractor(InfoExtractor):
  14. @staticmethod
  15. def _fix_json(json_string):
  16. return json_string.replace('\\\'', '\'')
  17. def _extract_video(self, info):
  18. video_id = info['id']
  19. self.report_extraction(video_id)
  20. initial_video_url = info['publishPoint']
  21. if info['formats'] == '1':
  22. parsed_url = compat_urllib_parse_urlparse(initial_video_url)
  23. path = parsed_url.path.replace('.', '_sd.', 1)
  24. data = compat_urllib_parse.urlencode({
  25. 'type': 'fvod',
  26. 'path': compat_urlparse.urlunparse(parsed_url[:2] + (path,) + parsed_url[3:])
  27. })
  28. path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
  29. path_doc = self._download_xml(
  30. path_url, video_id, 'Downloading final video url')
  31. video_url = path_doc.find('path').text
  32. else:
  33. video_url = initial_video_url
  34. join = compat_urlparse.urljoin
  35. return {
  36. 'id': video_id,
  37. 'title': info['name'],
  38. 'url': video_url,
  39. 'description': info['description'],
  40. 'duration': int(info['duration']),
  41. 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
  42. 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
  43. }
  44. class NHLIE(NHLBaseInfoExtractor):
  45. IE_NAME = 'nhl.com'
  46. _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console(?:\?(?:.*?[?&])?)id=(?P<id>[0-9a-z-]+)'
  47. _TESTS = [{
  48. 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
  49. 'md5': 'db704a4ea09e8d3988c85e36cc892d09',
  50. 'info_dict': {
  51. 'id': '453614',
  52. 'ext': 'mp4',
  53. 'title': 'Quick clip: Weise 4-3 goal vs Flames',
  54. 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
  55. 'duration': 18,
  56. 'upload_date': '20131006',
  57. },
  58. }, {
  59. 'url': 'http://video.nhl.com/videocenter/console?id=2014020024-628-h',
  60. 'md5': 'd22e82bc592f52d37d24b03531ee9696',
  61. 'info_dict': {
  62. 'id': '2014020024-628-h',
  63. 'ext': 'mp4',
  64. 'title': 'Alex Galchenyuk Goal on Ray Emery (14:40/3rd)',
  65. 'description': 'Home broadcast - Montreal Canadiens at Philadelphia Flyers - October 11, 2014',
  66. 'duration': 0,
  67. 'upload_date': '20141011',
  68. },
  69. }, {
  70. 'url': 'http://video.mapleleafs.nhl.com/videocenter/console?id=58665&catid=802',
  71. 'md5': 'c78fc64ea01777e426cfc202b746c825',
  72. 'info_dict': {
  73. 'id': '58665',
  74. 'ext': 'flv',
  75. 'title': 'Classic Game In Six - April 22, 1979',
  76. 'description': 'It was the last playoff game for the Leafs in the decade, and the last time the Leafs and Habs played in the playoffs. Great game, not a great ending.',
  77. 'duration': 400,
  78. 'upload_date': '20100129'
  79. },
  80. }, {
  81. 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
  82. 'only_matching': True,
  83. }]
  84. def _real_extract(self, url):
  85. mobj = re.match(self._VALID_URL, url)
  86. video_id = mobj.group('id')
  87. json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
  88. data = self._download_json(
  89. json_url, video_id, transform_source=self._fix_json)
  90. return self._extract_video(data[0])
  91. class NHLVideocenterIE(NHLBaseInfoExtractor):
  92. IE_NAME = 'nhl.com:videocenter'
  93. IE_DESC = 'NHL videocenter category'
  94. _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?[^(id=)]*catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
  95. _TEST = {
  96. 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
  97. 'info_dict': {
  98. 'id': '999',
  99. 'title': 'Highlights',
  100. },
  101. 'playlist_count': 12,
  102. }
  103. def _real_extract(self, url):
  104. mobj = re.match(self._VALID_URL, url)
  105. team = mobj.group('team')
  106. webpage = self._download_webpage(url, team)
  107. cat_id = self._search_regex(
  108. [r'var defaultCatId = "(.+?)";',
  109. r'{statusIndex:0,index:0,.*?id:(.*?),'],
  110. webpage, 'category id')
  111. playlist_title = self._html_search_regex(
  112. r'tab0"[^>]*?>(.*?)</td>',
  113. webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
  114. data = compat_urllib_parse.urlencode({
  115. 'cid': cat_id,
  116. # This is the default value
  117. 'count': 12,
  118. 'ptrs': 3,
  119. 'format': 'json',
  120. })
  121. path = '/videocenter/servlets/browse?' + data
  122. request_url = compat_urlparse.urljoin(url, path)
  123. response = self._download_webpage(request_url, playlist_title)
  124. response = self._fix_json(response)
  125. if not response.strip():
  126. self._downloader.report_warning('Got an empty reponse, trying '
  127. 'adding the "newvideos" parameter')
  128. response = self._download_webpage(request_url + '&newvideos=true',
  129. playlist_title)
  130. response = self._fix_json(response)
  131. videos = json.loads(response)
  132. return {
  133. '_type': 'playlist',
  134. 'title': playlist_title,
  135. 'id': cat_id,
  136. 'entries': [self._extract_video(v) for v in videos],
  137. }