nhl.py 5.0 KB

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