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.

120 lines
4.3 KiB

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