sH5V project: https://github.com/910JQK/sH5V

This commit merges pull request #1
This commit is contained in:
910JQK 2013-10-06 14:30:26 +08:00 committed by Star Brilliant
parent 6926b88eab
commit f6d4e06c34

View File

@ -91,6 +91,13 @@ def ProbeCommentFormat(f):
if tmp == '[': if tmp == '[':
f.seek(0) f.seek(0)
return 'Acfun' return 'Acfun'
elif tmp == '{':
tmp = f.read(17)
f.seek(0)
if tmp == '"root":{"total":"':
return 'sH5V'
else:
return None
elif tmp == '<': elif tmp == '<':
tmp = f.read(39) tmp = f.read(39)
f.seek(0) f.seek(0)
@ -172,6 +179,26 @@ def ReadCommentsBilibili(f, fontsize):
continue continue
def ReadCommentsSH5V (f, fontsize):
'Output format: [(timeline, timestamp, no, comment, pos, color, size, height, width)]'
comment_element = json.load(f)
i = 0
for comment in comment_element["root"]["bgs"] :
try:
c_at = str(comment['at'])
c_type = str(comment['type'])
c_date = str(comment['timestamp'])
c_color = str(comment['color'])
c = str(comment['text'])
size = fontsize
#print(c_at,' ',round(float(c_at),2),' ',c)
yield (float(c_at), int(c_date), i, c, {'0': 0, '1': 0, '4': 2, '5': 1}[c_type], int(c_color[1:],16), size, (c.count('\n')+1)*size, CalculateLength(c)*size)
i += 1
except (AssertionError, AttributeError, IndexError, TypeError, ValueError):
logging.warning(_('Invalid comment: %r') % comment)
continue
def ConvertTimestamp(timestamp): def ConvertTimestamp(timestamp):
hour, minute = divmod(timestamp, 3600) hour, minute = divmod(timestamp, 3600)
minute, second = divmod(minute, 60) minute, second = divmod(minute, 60)
@ -232,7 +259,7 @@ if __name__ == '__main__':
comments = [] comments = []
for i in args.file: for i in args.file:
with open(i, 'r', encoding='utf-8') as f: with open(i, 'r', encoding='utf-8') as f:
CommentProcesser = {None: None, 'Niconico': ReadCommentsNiconico, 'Acfun': ReadCommentsAcfun, 'Bilibili': ReadCommentsBilibili}[ProbeCommentFormat(f)] CommentProcesser = {None: None, 'Niconico': ReadCommentsNiconico, 'Acfun': ReadCommentsAcfun, 'Bilibili': ReadCommentsBilibili, 'sH5V': ReadCommentsSH5V}[ProbeCommentFormat(f)]
if not CommentProcesser: if not CommentProcesser:
raise ValueError(_('Unknown comment file format: %s') % i) raise ValueError(_('Unknown comment file format: %s') % i)
for comment in CommentProcesser(f, args.fontsize): for comment in CommentProcesser(f, args.fontsize):