sohu.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # encoding: utf-8
  2. import re
  3. import json
  4. import time
  5. import logging
  6. import urllib2
  7. from .common import InfoExtractor
  8. from ..utils import compat_urllib_request
  9. class SohuIE(InfoExtractor):
  10. _VALID_URL = r'https?://tv\.sohu\.com/\d+?/n(?P<id>\d+)\.shtml.*?'
  11. _TEST = {
  12. u'url': u'http://tv.sohu.com/20130724/n382479172.shtml#super',
  13. u'file': u'382479172.flv',
  14. u'md5': u'cc84eed6b6fbf0f2f9a8d3cb9da1939b',
  15. u'info_dict': {
  16. u'title': u'The Illest - Far East Movement Riff Raff',
  17. },
  18. }
  19. def _clearn_html(self, string):
  20. tags = re.findall(r'<.+?>', string)
  21. for t in tags:
  22. string = string.replace(t, ' ')
  23. for i in range(2):
  24. spaces = re.findall(r'\s+', string)
  25. for s in spaces:
  26. string = string.replace(s, ' ')
  27. string = string.strip()
  28. return string
  29. def _real_extract(self, url):
  30. mobj = re.match(self._VALID_URL, url)
  31. video_id = mobj.group('id')
  32. webpage = self._download_webpage(url, video_id)
  33. pattern = r'<h1 id="video-title">\n*?(.+?)\n*?</h1>'
  34. compiled = re.compile(pattern, re.DOTALL)
  35. title = self._search_regex(compiled, webpage, u'video title').strip('\t\n')
  36. title = self._clearn_html(title)
  37. pattern = re.compile(r'var vid="(\d+)"')
  38. result = re.search(pattern, webpage)
  39. if not result:
  40. logging.info('[Sohu] could not get vid')
  41. return None
  42. vid = result.group(1)
  43. logging.info('vid: %s' % vid)
  44. base_url_1 = 'http://hot.vrs.sohu.com/vrs_flash.action?vid='
  45. url_1 = base_url_1 + vid
  46. logging.info('json url: %s' % url_1)
  47. json_1 = json.loads(urllib2.urlopen(url_1).read())
  48. # get the highest definition video vid and json infomation.
  49. vids = []
  50. qualities = ('oriVid', 'superVid', 'highVid', 'norVid')
  51. for vid_name in qualities:
  52. vids.append(json_1['data'][vid_name])
  53. clearest_vid = 0
  54. for i, v in enumerate(vids):
  55. if v != 0:
  56. clearest_vid = v
  57. logging.info('quality definition: %s' % qualities[i][:-3])
  58. break
  59. if not clearest_vid:
  60. logging.warning('could not find valid clearest_vid')
  61. return None
  62. if vid != clearest_vid:
  63. url_1 = '%s%d' % (base_url_1, clearest_vid)
  64. logging.info('highest definition json url: %s' % url_1)
  65. json_1 = json.loads(urllib2.urlopen(url_1).read())
  66. allot = json_1['allot']
  67. prot = json_1['prot']
  68. clipsURL = json_1['data']['clipsURL']
  69. su = json_1['data']['su']
  70. num_of_parts = json_1['data']['totalBlocks']
  71. logging.info('Total parts: %d' % num_of_parts)
  72. base_url_3 = 'http://allot/?prot=prot&file=clipsURL[i]&new=su[i]'
  73. files_info = []
  74. for i in range(num_of_parts):
  75. middle_url = 'http://%s/?prot=%s&file=%s&new=%s' % (allot, prot, clipsURL[i], su[i])
  76. logging.info('middle url part %d: %s' % (i, middle_url))
  77. middle_info = urllib2.urlopen(middle_url).read().split('|')
  78. middle_part_1 = middle_info[0]
  79. download_url = '%s%s?key=%s' % (middle_info[0], su[i], middle_info[3])
  80. info = {
  81. 'id': '%s_part%02d' % (video_id, i + 1),
  82. 'title': title,
  83. 'url': download_url,
  84. 'ext': 'mp4',
  85. }
  86. files_info.append(info)
  87. time.sleep(1)
  88. return files_info