nhl.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_urlencode,
  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. vid_parts = video_id.split(',')
  20. if len(vid_parts) == 3:
  21. video_id = '%s0%s%s-X-h' % (vid_parts[0][:4], vid_parts[1], vid_parts[2].rjust(4, '0'))
  22. json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
  23. data = self._download_json(
  24. json_url, video_id, transform_source=self._fix_json)
  25. return self._extract_video(data[0])
  26. def _extract_video(self, info):
  27. video_id = info['id']
  28. self.report_extraction(video_id)
  29. initial_video_url = info['publishPoint']
  30. if info['formats'] == '1':
  31. parsed_url = compat_urllib_parse_urlparse(initial_video_url)
  32. filename, ext = os.path.splitext(parsed_url.path)
  33. path = '%s_sd%s' % (filename, ext)
  34. data = compat_urllib_parse_urlencode({
  35. 'type': 'fvod',
  36. 'path': compat_urlparse.urlunparse(parsed_url[:2] + (path,) + parsed_url[3:])
  37. })
  38. path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
  39. path_doc = self._download_xml(
  40. path_url, video_id, 'Downloading final video url')
  41. video_url = path_doc.find('path').text
  42. else:
  43. video_url = initial_video_url
  44. join = compat_urlparse.urljoin
  45. ret = {
  46. 'id': video_id,
  47. 'title': info['name'],
  48. 'url': video_url,
  49. 'description': info['description'],
  50. 'duration': int(info['duration']),
  51. 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
  52. 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
  53. }
  54. if video_url.startswith('rtmp:'):
  55. mobj = re.match(r'(?P<tc_url>rtmp://[^/]+/(?P<app>[a-z0-9/]+))/(?P<play_path>mp4:.*)', video_url)
  56. ret.update({
  57. 'tc_url': mobj.group('tc_url'),
  58. 'play_path': mobj.group('play_path'),
  59. 'app': mobj.group('app'),
  60. 'no_resume': True,
  61. })
  62. return ret
  63. class NHLIE(NHLBaseInfoExtractor):
  64. IE_NAME = 'nhl.com'
  65. _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/(?:console|embed)?(?:\?(?:.*?[?&])?)(?:id|hlg|playlist)=(?P<id>[-0-9a-zA-Z,]+)'
  66. _TESTS = [{
  67. 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
  68. 'md5': 'db704a4ea09e8d3988c85e36cc892d09',
  69. 'info_dict': {
  70. 'id': '453614',
  71. 'ext': 'mp4',
  72. 'title': 'Quick clip: Weise 4-3 goal vs Flames',
  73. 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
  74. 'duration': 18,
  75. 'upload_date': '20131006',
  76. },
  77. }, {
  78. 'url': 'http://video.nhl.com/videocenter/console?id=2014020024-628-h',
  79. 'md5': 'd22e82bc592f52d37d24b03531ee9696',
  80. 'info_dict': {
  81. 'id': '2014020024-628-h',
  82. 'ext': 'mp4',
  83. 'title': 'Alex Galchenyuk Goal on Ray Emery (14:40/3rd)',
  84. 'description': 'Home broadcast - Montreal Canadiens at Philadelphia Flyers - October 11, 2014',
  85. 'duration': 0,
  86. 'upload_date': '20141011',
  87. },
  88. }, {
  89. 'url': 'http://video.mapleleafs.nhl.com/videocenter/console?id=58665&catid=802',
  90. 'md5': 'c78fc64ea01777e426cfc202b746c825',
  91. 'info_dict': {
  92. 'id': '58665',
  93. 'ext': 'flv',
  94. 'title': 'Classic Game In Six - April 22, 1979',
  95. '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.',
  96. 'duration': 400,
  97. 'upload_date': '20100129'
  98. },
  99. }, {
  100. 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
  101. 'only_matching': True,
  102. }, {
  103. 'url': 'http://video.nhl.com/videocenter/?id=736722',
  104. 'only_matching': True,
  105. }, {
  106. 'url': 'http://video.nhl.com/videocenter/console?hlg=20142015,2,299&lang=en',
  107. 'md5': '076fcb88c255154aacbf0a7accc3f340',
  108. 'info_dict': {
  109. 'id': '2014020299-X-h',
  110. 'ext': 'mp4',
  111. 'title': 'Penguins at Islanders / Game Highlights',
  112. 'description': 'Home broadcast - Pittsburgh Penguins at New York Islanders - November 22, 2014',
  113. 'duration': 268,
  114. 'upload_date': '20141122',
  115. }
  116. }, {
  117. 'url': 'http://video.oilers.nhl.com/videocenter/console?id=691469&catid=4',
  118. 'info_dict': {
  119. 'id': '691469',
  120. 'ext': 'mp4',
  121. 'title': 'RAW | Craig MacTavish Full Press Conference',
  122. 'description': 'Oilers GM Craig MacTavish addresses the media at Rexall Place on Friday.',
  123. 'upload_date': '20141205',
  124. },
  125. 'params': {
  126. 'skip_download': True, # Requires rtmpdump
  127. }
  128. }, {
  129. 'url': 'http://video.nhl.com/videocenter/embed?playlist=836127',
  130. 'only_matching': True,
  131. }]
  132. def _real_extract(self, url):
  133. video_id = self._match_id(url)
  134. return self._real_extract_video(video_id)
  135. class NHLNewsIE(NHLBaseInfoExtractor):
  136. IE_NAME = 'nhl.com:news'
  137. IE_DESC = 'NHL news'
  138. _VALID_URL = r'https?://(?:.+?\.)?nhl\.com/(?:ice|club)/news\.html?(?:\?(?:.*?[?&])?)id=(?P<id>[-0-9a-zA-Z]+)'
  139. _TESTS = [{
  140. 'url': 'http://www.nhl.com/ice/news.htm?id=750727',
  141. 'md5': '4b3d1262e177687a3009937bd9ec0be8',
  142. 'info_dict': {
  143. 'id': '736722',
  144. 'ext': 'mp4',
  145. 'title': 'Cal Clutterbuck has been fined $2,000',
  146. 'description': 'md5:45fe547d30edab88b23e0dd0ab1ed9e6',
  147. 'duration': 37,
  148. 'upload_date': '20150128',
  149. },
  150. }, {
  151. # iframe embed
  152. 'url': 'http://sabres.nhl.com/club/news.htm?id=780189',
  153. 'md5': '9f663d1c006c90ac9fb82777d4294e12',
  154. 'info_dict': {
  155. 'id': '836127',
  156. 'ext': 'mp4',
  157. 'title': 'Morning Skate: OTT vs. BUF (9/23/15)',
  158. 'description': "Brian Duff chats with Tyler Ennis prior to Buffalo's first preseason home game.",
  159. 'duration': 93,
  160. 'upload_date': '20150923',
  161. },
  162. }]
  163. def _real_extract(self, url):
  164. news_id = self._match_id(url)
  165. webpage = self._download_webpage(url, news_id)
  166. video_id = self._search_regex(
  167. [r'pVid(\d+)', r"nlid\s*:\s*'(\d+)'",
  168. r'<iframe[^>]+src=["\']https?://video.*?\.nhl\.com/videocenter/embed\?.*\bplaylist=(\d+)'],
  169. webpage, 'video id')
  170. return self._real_extract_video(video_id)
  171. class NHLVideocenterIE(NHLBaseInfoExtractor):
  172. IE_NAME = 'nhl.com:videocenter'
  173. IE_DESC = 'NHL videocenter category'
  174. _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?[^(id=)]*catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
  175. _TEST = {
  176. 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
  177. 'info_dict': {
  178. 'id': '999',
  179. 'title': 'Highlights',
  180. },
  181. 'playlist_count': 12,
  182. }
  183. def _real_extract(self, url):
  184. mobj = re.match(self._VALID_URL, url)
  185. team = mobj.group('team')
  186. webpage = self._download_webpage(url, team)
  187. cat_id = self._search_regex(
  188. [r'var defaultCatId = "(.+?)";',
  189. r'{statusIndex:0,index:0,.*?id:(.*?),'],
  190. webpage, 'category id')
  191. playlist_title = self._html_search_regex(
  192. r'tab0"[^>]*?>(.*?)</td>',
  193. webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
  194. data = compat_urllib_parse_urlencode({
  195. 'cid': cat_id,
  196. # This is the default value
  197. 'count': 12,
  198. 'ptrs': 3,
  199. 'format': 'json',
  200. })
  201. path = '/videocenter/servlets/browse?' + data
  202. request_url = compat_urlparse.urljoin(url, path)
  203. response = self._download_webpage(request_url, playlist_title)
  204. response = self._fix_json(response)
  205. if not response.strip():
  206. self._downloader.report_warning('Got an empty response, trying '
  207. 'adding the "newvideos" parameter')
  208. response = self._download_webpage(request_url + '&newvideos=true',
  209. playlist_title)
  210. response = self._fix_json(response)
  211. videos = json.loads(response)
  212. return {
  213. '_type': 'playlist',
  214. 'title': playlist_title,
  215. 'id': cat_id,
  216. 'entries': [self._extract_video(v) for v in videos],
  217. }