nhl.py 6.8 KB

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