#!/usr/bin/env python #coding:utf-8 # Author: Beining -- # Purpose: A general extractor for CKPlayer # Created: 03/15/2016 __all__ = ['ckplayer_download'] from xml.etree import cElementTree as ET from copy import copy from ..common import * #---------------------------------------------------------------------- def get_info_by_xml(ckinfo): """str->dict Information for CKPlayer API content.""" e = ET.XML(ckinfo) video_dict = {'title': '', #'duration': 0, 'links': [], 'size': 0, 'flashvars': '',} if '_text' in dictify(e)['ckplayer']['info'][0]['title'][0]: #title video_dict['title'] = dictify(e)['ckplayer']['info'][0]['title'][0]['_text'].strip() #if dictify(e)['ckplayer']['info'][0]['title'][0]['_text'].strip(): #duration #video_dict['title'] = dictify(e)['ckplayer']['info'][0]['title'][0]['_text'].strip() if '_text' in dictify(e)['ckplayer']['video'][0]['size'][0]: #size exists for 1 piece video_dict['size'] = sum([int(i['size'][0]['_text']) for i in dictify(e)['ckplayer']['video']]) if '_text' in dictify(e)['ckplayer']['video'][0]['file'][0]: #link exist video_dict['links'] = [i['file'][0]['_text'].strip() for i in dictify(e)['ckplayer']['video']] if '_text' in dictify(e)['ckplayer']['flashvars'][0]: video_dict['flashvars'] = dictify(e)['ckplayer']['flashvars'][0]['_text'].strip() return video_dict #helper #https://stackoverflow.com/questions/2148119/how-to-convert-an-xml-string-to-a-dictionary-in-python def dictify(r,root=True): if root: return {r.tag : dictify(r, False)} d=copy(r.attrib) if r.text: d["_text"]=r.text for x in r.findall("./*"): if x.tag not in d: d[x.tag]=[] d[x.tag].append(dictify(x,False)) return d def ckplayer_download(url, output_dir = '.', merge = False, info_only = False, **kwargs): #Info XML ckinfo = get_content(url) video_info = get_info_by_xml(ckinfo) title = '' type_ = '' size = 0 if len(video_info['links']) > 0: #has link type_, _ext, size = url_info(video_info['links'][0]) #use 1st to determine type, ext if 'size' in video_info: size = int(video_info['size']) else: for i in video_info['links'][1:]: #save 1st one size += url_info(i)[2] print_info(site_info, title, type_, size) if not info_only: download_urls(video_info['links'], title, ext, size, output_dir=output_dir, merge=merge) site_info = "CKPlayer General" download = ckplayer_download download_playlist = playlist_not_supported('ckplayer')