Last active
August 7, 2016 19:18
-
-
Save hakatashi/9323388 to your computer and use it in GitHub Desktop.
simple utility to process niconico comment XML file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -* coding: utf-8 -*- | |
import sys | |
import os | |
import re | |
import argparse | |
import shutil | |
from xml.etree.ElementTree import * | |
parser = argparse.ArgumentParser(description='simple utility to process niconico comment XML file.') | |
parser.add_argument('file', type=file, nargs='+', help='XML file to process') | |
parser.add_argument('-s', '--shift', type=int, nargs=1, help='time to shift all comments (millisecond)') | |
parser.add_argument('-r', '--recent', type=int, nargs=1, help='delete recent RECENT comments') | |
parser.add_argument('-n', '--rename', action='store_true', help='search in directory and rename input file to proper name.') | |
args = parser.parse_args() | |
for argfile in args.file: | |
try: | |
filepath = argfile.name.decode('shift-jis') | |
argfile.close() | |
filename = os.path.basename(filepath) | |
if args.rename: | |
if not filename.endswith('.xml'): | |
raise IOError('%s: file extension must be .xml' % filepath) | |
idmatch = re.match(r'^.*\((.*)?\)\[.*?\]\.xml$', filename) | |
if idmatch is None: | |
idmatch = re.match(r'^.*-([^-]*)\.xml$', filename) | |
if idmatch is None: | |
raise IOError('%s: filename must include video id.' % filepath) | |
videoid = idmatch.group(1) | |
dirname = os.path.dirname(filepath) | |
dirfiles = os.listdir(dirname) | |
renamed = False | |
for dirfile in dirfiles: | |
dirfilename = os.path.basename(dirfile) | |
if dirfilename.endswith('.mp4'): | |
diridmatch = re.match(r'^.*-([^-]*)\.mp4', dirfilename) | |
if diridmatch is not None: | |
if diridmatch.group(1) == videoid: | |
dirfilename = re.sub(r'\.mp4$', r'.xml', dirfilename) | |
dstfilepath = filepath.replace(filename, dirfilename) | |
shutil.copy(filepath.encode('shift-jis'), dstfilepath.encode('shift-jis')) | |
filepath = dstfilepath | |
renamed = True | |
break | |
if not renamed: | |
raise IOError('%s: destination filename was not found' % filepath) | |
if filename.endswith('.backup.xml'): | |
backupfile = filepath | |
xmlfile = re.sub(r'\.backup\.xml$', r'.xml', backupfile) | |
tree = parse(backupfile.encode('shift-jis')) | |
elif filename.endswith('.xml'): | |
xmlfile = filepath | |
backupfile = re.sub(r'\.xml$', r'.backup.xml', xmlfile) | |
if os.path.isfile(backupfile.encode('shift-jis')): | |
tree = parse(backupfile.encode('shift-jis')) | |
else: | |
tree = parse(xmlfile.encode('shift-jis')) | |
os.rename(xmlfile.encode('shift-jis'), backupfile.encode('shift-jis')) | |
else: | |
raise IOError('%s: file extension must be .xml' % filepath) | |
root = tree.getroot() | |
if args.shift is not None: | |
for element in root.getiterator('chat'): | |
vpos = int(element.attrib['vpos']) | |
element.set('vpos', str(max(vpos - int(args.shift[0] / 10), 0))) | |
if args.recent is not None: | |
maxno = 0 | |
for element in root.getiterator('chat'): | |
if maxno < int(element.attrib['no']): | |
maxno = int(element.attrib['no']) | |
for element in root.getiterator('chat'): | |
no = int(element.attrib['no']) | |
if no > maxno - args.recent[0]: | |
root.remove(element) | |
tree.write(xmlfile.encode('shift-jis')) | |
except: | |
print 'error:', sys.exc_info()[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment