import xml.etree.ElementTree as ET
import requests
########################################################################
class BilibiliDanmaku:
""""""
cid = 0
danmaku_list = []
danmaku_url = 'http://comment.bilibili.com/{cid}.xml'
danmaku_xml = ''
danmaku_xml_tree = {}
danmaku_pmf_data = {}
headers = {
'accept-encoding': 'gzip, deflate, sdch',
'accept-language': 'en-CA,en;q=0.8,en-US;q=0.6,zh-CN;q=0.4,zh;q=0.2',
'user-agent': '(Python-urllib/2.7, like libcurl/1.0 NSS-Mozilla/2.0)',
'accept': '*/*',}
chatserver = ''
#self.chatid = 0
mission = 0
maxlimit = 0
source = ''
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
pass
#----------------------------------------------------------------------
def init(self):
"""great init"""
if self.cid <= 0:
raise ValueError('invalid cid')
self.get()
self.parse()
pass
@classmethod
#----------------------------------------------------------------------
def setcid(self, cid):
""""""
self.cid = cid
self.danmaku_url = self.danmaku_url.format(cid = cid)
#----------------------------------------------------------------------
def get(self):
""""""
if not self.cid:
raise ValueError('Cid not set')
responce = requests.get(self.danmaku_url, headers= self.headers)
self.danmaku_xml = responce.content
@classmethod
#----------------------------------------------------------------------
def load(self, file_this):
""""""
if type(file_this) == str:
self.danmaku_xml = file_this
elif type(file_this) == file:
f = open(file_this, 'r')
self.danmaku_xml = f.read()
f.close()
#----------------------------------------------------------------------
def parse(self):
""""""
if not self.danmaku_xml:
raise ValueError('No Danmaku Data')
tree = ET.ElementTree(ET.fromstring(self.danmaku_xml))
root = tree.getroot()
for child_of_root in root:
if child_of_root.tag == 'chatserver':
self.chatserver = child_of_root.text
if child_of_root.tag == 'chatid':
self.cid = int(child_of_root.text)
if child_of_root.tag == 'mission':
self.mission = child_of_root.text
if child_of_root.tag == 'maxlimit':
self.maxlimit = int(child_of_root.text)
if child_of_root.tag == 'source':
self.source = child_of_root.text
if child_of_root.tag == 'd':
info = str(child_of_root.attrib.get('p')).split(',')
self.danmaku_xml_tree[info[7]] = {'stime': info[0],
'mode': info[1],
'size': info[2],
'color': info[3],
'timestamp': info[4],
'pool': info[5],
'sender': info[6],
'msg': child_of_root.text
}
@classmethod
#----------------------------------------------------------------------
def get_pmf(self, period):
""""""
timelist = [(k, v['stime']) for k, v in self.danmaku_xml_tree.iteritems()]
timelist.sort(key = lambda tup:tup[1])
if period == 0:
return timelist
start = 0
#not 0
self.danmaku_pmf_data = {0: [], }
for i in timelist:
if i[1] <= int(start + period):
self.danmaku_pmf_data[start].append(i[0])
elif i[1] > int(start + period):
start += period
self.danmaku_pmf_data[start] = []
self.danmaku_pmf_data[start].append(i[0])