nhl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 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. join = compat_urlparse.urljoin
  28. return {
  29. 'id': video_id,
  30. 'title': info['name'],
  31. 'url': video_url,
  32. 'ext': determine_ext(video_url),
  33. 'description': info['description'],
  34. 'duration': int(info['duration']),
  35. 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
  36. 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
  37. }
  38. class NHLIE(NHLBaseInfoExtractor):
  39. IE_NAME = 'nhl.com'
  40. _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console(?:\?(?:.*?[?&])?)id=(?P<id>[0-9]+)'
  41. _TESTS = [{
  42. 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
  43. 'info_dict': {
  44. 'id': '453614',
  45. 'ext': 'mp4',
  46. 'title': 'Quick clip: Weise 4-3 goal vs Flames',
  47. 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
  48. 'duration': 18,
  49. 'upload_date': '20131006',
  50. },
  51. }, {
  52. 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
  53. 'only_matching': True,
  54. }]
  55. def _real_extract(self, url):
  56. mobj = re.match(self._VALID_URL, url)
  57. video_id = mobj.group('id')
  58. json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
  59. data = self._download_json(
  60. json_url, video_id, transform_source=self._fix_json)
  61. return self._extract_video(data[0])
  62. class NHLVideocenterIE(NHLBaseInfoExtractor):
  63. IE_NAME = 'nhl.com:videocenter'
  64. IE_DESC = 'NHL videocenter category'
  65. _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?.*?catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
  66. _TEST = {
  67. 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
  68. 'info_dict': {
  69. 'id': '999',
  70. 'title': 'Highlights',
  71. },
  72. 'playlist_count': 12,
  73. }
  74. def _real_extract(self, url):
  75. mobj = re.match(self._VALID_URL, url)
  76. team = mobj.group('team')
  77. webpage = self._download_webpage(url, team)
  78. cat_id = self._search_regex(
  79. [r'var defaultCatId = "(.+?)";',
  80. r'{statusIndex:0,index:0,.*?id:(.*?),'],
  81. webpage, 'category id')
  82. playlist_title = self._html_search_regex(
  83. r'tab0"[^>]*?>(.*?)</td>',
  84. webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
  85. data = compat_urllib_parse.urlencode({
  86. 'cid': cat_id,
  87. # This is the default value
  88. 'count': 12,
  89. 'ptrs': 3,
  90. 'format': 'json',
  91. })
  92. path = '/videocenter/servlets/browse?' + data
  93. request_url = compat_urlparse.urljoin(url, path)
  94. response = self._download_webpage(request_url, playlist_title)
  95. response = self._fix_json(response)
  96. if not response.strip():
  97. self._downloader.report_warning(u'Got an empty reponse, trying '
  98. 'adding the "newvideos" parameter')
  99. response = self._download_webpage(request_url + '&newvideos=true',
  100. playlist_title)
  101. response = self._fix_json(response)
  102. videos = json.loads(response)
  103. return {
  104. '_type': 'playlist',
  105. 'title': playlist_title,
  106. 'id': cat_id,
  107. 'entries': [self._extract_video(v) for v in videos],
  108. }