nhl.py 4.9 KB

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