You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.2 KiB

  1. import re
  2. import json
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urlparse,
  6. compat_urllib_parse,
  7. determine_ext,
  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. data = compat_urllib_parse.urlencode({
  19. 'type': 'fvod',
  20. 'path': initial_video_url.replace('.mp4', '_sd.mp4'),
  21. })
  22. path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
  23. path_doc = self._download_xml(path_url, video_id,
  24. u'Downloading final video url')
  25. video_url = path_doc.find('path').text
  26. join = compat_urlparse.urljoin
  27. return {
  28. 'id': video_id,
  29. 'title': info['name'],
  30. 'url': video_url,
  31. 'ext': determine_ext(video_url),
  32. 'description': info['description'],
  33. 'duration': int(info['duration']),
  34. 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
  35. 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
  36. }
  37. class NHLIE(NHLBaseInfoExtractor):
  38. IE_NAME = u'nhl.com'
  39. _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console\?.*?(?<=[?&])id=(?P<id>\d+)'
  40. _TEST = {
  41. u'url': u'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
  42. u'file': u'453614.mp4',
  43. u'info_dict': {
  44. u'title': u'Quick clip: Weise 4-3 goal vs Flames',
  45. u'description': u'Dale Weise scores his first of the season to put the Canucks up 4-3.',
  46. u'duration': 18,
  47. u'upload_date': u'20131006',
  48. },
  49. }
  50. def _real_extract(self, url):
  51. mobj = re.match(self._VALID_URL, url)
  52. video_id = mobj.group('id')
  53. json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
  54. info_json = self._download_webpage(json_url, video_id,
  55. u'Downloading info json')
  56. info_json = self._fix_json(info_json)
  57. info = json.loads(info_json)[0]
  58. return self._extract_video(info)
  59. class NHLVideocenterIE(NHLBaseInfoExtractor):
  60. IE_NAME = u'nhl.com:videocenter'
  61. IE_DESC = u'NHL videocenter category'
  62. _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?.*?catid=(?P<catid>[^&]+))?'
  63. @classmethod
  64. def suitable(cls, url):
  65. if NHLIE.suitable(url):
  66. return False
  67. return super(NHLVideocenterIE, cls).suitable(url)
  68. def _real_extract(self, url):
  69. mobj = re.match(self._VALID_URL, url)
  70. team = mobj.group('team')
  71. webpage = self._download_webpage(url, team)
  72. cat_id = self._search_regex(
  73. [r'var defaultCatId = "(.+?)";',
  74. r'{statusIndex:0,index:0,.*?id:(.*?),'],
  75. webpage, u'category id')
  76. playlist_title = self._html_search_regex(
  77. r'tab0"[^>]*?>(.*?)</td>',
  78. webpage, u'playlist title', flags=re.DOTALL).lower().capitalize()
  79. data = compat_urllib_parse.urlencode({
  80. 'cid': cat_id,
  81. # This is the default value
  82. 'count': 12,
  83. 'ptrs': 3,
  84. 'format': 'json',
  85. })
  86. path = '/videocenter/servlets/browse?' + data
  87. request_url = compat_urlparse.urljoin(url, path)
  88. response = self._download_webpage(request_url, playlist_title)
  89. response = self._fix_json(response)
  90. if not response.strip():
  91. self._downloader.report_warning(u'Got an empty reponse, trying '
  92. u'adding the "newvideos" parameter')
  93. response = self._download_webpage(request_url + '&newvideos=true',
  94. playlist_title)
  95. response = self._fix_json(response)
  96. videos = json.loads(response)
  97. return {
  98. '_type': 'playlist',
  99. 'title': playlist_title,
  100. 'id': cat_id,
  101. 'entries': [self._extract_video(i) for i in videos],
  102. }