kusi.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import random
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. float_or_none,
  6. int_or_none,
  7. timeconvert,
  8. update_url_query,
  9. xpath_text,
  10. )
  11. class KUSIIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?kusi\.com/(?P<path>story/.+|video\?clipId=(?P<clipId>\d+))'
  13. _TESTS = [{
  14. 'url': 'http://www.kusi.com/story/32849881/turko-files-refused-to-help-it-aint-right',
  15. 'md5': '4e76ce8e53660ce9697d06c0ba6fc47d',
  16. 'info_dict': {
  17. 'id': '12689020',
  18. 'ext': 'mp4',
  19. 'title': "Turko Files: Refused to Help, It Ain't Right!",
  20. 'duration': 223.586,
  21. 'upload_date': '20160826',
  22. 'timestamp': 1472233118,
  23. 'thumbnail': r're:^https?://.*\.jpg$'
  24. },
  25. }, {
  26. 'url': 'http://kusi.com/video?clipId=12203019',
  27. 'only_matching': True,
  28. }]
  29. def _real_extract(self, url):
  30. mobj = self._match_valid_url(url)
  31. clip_id = mobj.group('clipId')
  32. video_id = clip_id or mobj.group('path')
  33. webpage = self._download_webpage(url, video_id)
  34. if clip_id is None:
  35. video_id = clip_id = self._html_search_regex(
  36. r'"clipId"\s*,\s*"(\d+)"', webpage, 'clip id')
  37. affiliate_id = self._search_regex(
  38. r'affiliateId\s*:\s*\'([^\']+)\'', webpage, 'affiliate id')
  39. # See __Packages/worldnow/model/GalleryModel.as of WNGallery.swf
  40. xml_url = update_url_query('http://www.kusi.com/build.asp', {
  41. 'buildtype': 'buildfeaturexmlrequest',
  42. 'featureType': 'Clip',
  43. 'featureid': clip_id,
  44. 'affiliateno': affiliate_id,
  45. 'clientgroupid': '1',
  46. 'rnd': int(round(random.random() * 1000000)),
  47. })
  48. doc = self._download_xml(xml_url, video_id)
  49. video_title = xpath_text(doc, 'HEADLINE', fatal=True)
  50. duration = float_or_none(xpath_text(doc, 'DURATION'), scale=1000)
  51. description = xpath_text(doc, 'ABSTRACT')
  52. thumbnail = xpath_text(doc, './THUMBNAILIMAGE/FILENAME')
  53. creation_time = timeconvert(xpath_text(doc, 'rfc822creationdate'))
  54. quality_options = doc.find('{http://search.yahoo.com/mrss/}group').findall('{http://search.yahoo.com/mrss/}content')
  55. formats = []
  56. for quality in quality_options:
  57. formats.append({
  58. 'url': urllib.parse.unquote_plus(quality.attrib['url']),
  59. 'height': int_or_none(quality.attrib.get('height')),
  60. 'width': int_or_none(quality.attrib.get('width')),
  61. 'vbr': float_or_none(quality.attrib.get('bitratebits'), scale=1000),
  62. })
  63. return {
  64. 'id': video_id,
  65. 'title': video_title,
  66. 'description': description,
  67. 'duration': duration,
  68. 'formats': formats,
  69. 'thumbnail': thumbnail,
  70. 'timestamp': creation_time,
  71. }