2
0

nhl.py 4.9 KB

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