2016-03-15 08:46:37 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#coding:utf-8
|
|
|
|
# Author: Beining --<i@cnbeining.com>
|
|
|
|
# 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 *
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-03-15 09:41:23 +03:00
|
|
|
def ckplayer_get_info_by_xml(ckinfo):
|
2016-03-15 08:46:37 +03:00
|
|
|
"""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
|
|
|
|
|
2016-03-15 09:41:23 +03:00
|
|
|
#----------------------------------------------------------------------
|
2016-03-15 08:46:37 +03:00
|
|
|
#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
|
|
|
|
|
2016-03-15 09:41:23 +03:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def ckplayer_download_by_xml(ckinfo, output_dir = '.', merge = False, info_only = False, **kwargs):
|
2016-03-15 08:46:37 +03:00
|
|
|
#Info XML
|
2016-03-15 09:41:23 +03:00
|
|
|
video_info = ckplayer_get_info_by_xml(ckinfo)
|
2016-03-15 08:46:37 +03:00
|
|
|
|
2016-03-15 09:41:23 +03:00
|
|
|
try:
|
|
|
|
title = kwargs['title']
|
|
|
|
except:
|
|
|
|
title = ''
|
2016-03-15 08:46:37 +03:00
|
|
|
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:
|
2016-03-15 09:41:23 +03:00
|
|
|
download_urls(video_info['links'], title, _ext, size, output_dir=output_dir, merge=merge)
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def ckplayer_download(url, output_dir = '.', merge = False, info_only = False, is_xml = True, **kwargs):
|
|
|
|
if is_xml: #URL is XML URL
|
|
|
|
try:
|
|
|
|
title = kwargs['title']
|
|
|
|
except:
|
|
|
|
title = ''
|
|
|
|
try:
|
|
|
|
headers = kwargs['headers'] #headers provided
|
|
|
|
ckinfo = get_content(url, headers = headers)
|
|
|
|
except NameError:
|
|
|
|
ckinfo = get_content(url)
|
|
|
|
|
|
|
|
ckplayer_download_by_xml(ckinfo, output_dir, merge,
|
|
|
|
info_only, title = title)
|
2016-03-15 08:46:37 +03:00
|
|
|
|
|
|
|
site_info = "CKPlayer General"
|
|
|
|
download = ckplayer_download
|
|
|
|
download_playlist = playlist_not_supported('ckplayer')
|