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.

86 lines
2.9 KiB

9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hmac
  4. from hashlib import sha1
  5. from base64 import b64encode
  6. from time import time
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. ExtractorError,
  10. determine_ext
  11. )
  12. from ..compat import compat_urllib_parse
  13. class VLiveIE(InfoExtractor):
  14. IE_NAME = 'vlive'
  15. # www.vlive.tv/video/ links redirect to m.vlive.tv/video/ for mobile devices
  16. _VALID_URL = r'https?://(?:(www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
  17. _TEST = {
  18. 'url': 'http://m.vlive.tv/video/1326',
  19. 'md5': 'cc7314812855ce56de70a06a27314983',
  20. 'info_dict': {
  21. 'id': '1326',
  22. 'ext': 'mp4',
  23. 'title': '[V] Girl\'s Day\'s Broadcast',
  24. 'creator': 'Girl\'s Day',
  25. },
  26. }
  27. _SECRET = 'rFkwZet6pqk1vQt6SxxUkAHX7YL3lmqzUMrU4IDusTo4jEBdtOhNfT4BYYAdArwH'
  28. def _real_extract(self, url):
  29. video_id = self._match_id(url)
  30. webpage = self._download_webpage(
  31. 'http://m.vlive.tv/video/%s' % video_id,
  32. video_id, note='Download video page')
  33. title = self._og_search_title(webpage)
  34. thumbnail = self._og_search_thumbnail(webpage)
  35. creator = self._html_search_regex(
  36. r'<span[^>]+class="name">([^<>]+)</span>', webpage, 'creator')
  37. url = 'http://global.apis.naver.com/globalV/globalV/vod/%s/playinfo?' % video_id
  38. msgpad = '%.0f' % (time() * 1000)
  39. md = b64encode(
  40. hmac.new(self._SECRET.encode('ascii'),
  41. (url[:255] + msgpad).encode('ascii'), sha1).digest()
  42. )
  43. url += '&' + compat_urllib_parse.urlencode({'msgpad': msgpad, 'md': md})
  44. playinfo = self._download_json(url, video_id, 'Downloading video json')
  45. if playinfo.get('message', '') != 'success':
  46. raise ExtractorError(playinfo.get('message', 'JSON request unsuccessful'))
  47. if not playinfo.get('result'):
  48. raise ExtractorError('No videos found.')
  49. formats = []
  50. for vid in playinfo['result'].get('videos', {}).get('list', []):
  51. formats.append({
  52. 'url': vid['source'],
  53. 'ext': 'mp4',
  54. 'abr': vid.get('bitrate', {}).get('audio'),
  55. 'vbr': vid.get('bitrate', {}).get('video'),
  56. 'format_id': vid['encodingOption']['name'],
  57. 'height': vid.get('height'),
  58. 'width': vid.get('width'),
  59. })
  60. self._sort_formats(formats)
  61. subtitles = {}
  62. for caption in playinfo['result'].get('captions', {}).get('list', []):
  63. subtitles[caption['language']] = [
  64. {'ext': determine_ext(caption['source'], default_ext='vtt'),
  65. 'url': caption['source']}]
  66. return {
  67. 'id': video_id,
  68. 'title': title,
  69. 'creator': creator,
  70. 'thumbnail': thumbnail,
  71. 'formats': formats,
  72. 'subtitles': subtitles,
  73. }