Merge pull request #47 from Chaigidel/master

支持用 JSON 文件添加多个正则过滤
This commit is contained in:
Star Brilliant 2020-02-25 00:05:43 +08:00 committed by GitHub
commit 416aa04c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View File

@ -97,6 +97,8 @@ optional arguments:
Duration of still comment display [default: 5]
-fl FILTER, --filter FILTER
Regular expression to filter comments
-flf FILTER_FILE, --filter-file FILTER_FILE
Regular expressions from file (one line one regex) to filter comments
-p HEIGHT, --protect HEIGHT
Reserve blank on the bottom of the stage
-r, --reduce Reduce the amount of comments if stage is full

View File

@ -512,7 +512,7 @@ def ConvertFlashRotation(rotY, rotZ, X, Y, width, height):
return (trX, trY, WrapAngle(outX), WrapAngle(outY), WrapAngle(outZ), scaleXY * 100, scaleXY * 100)
def ProcessComments(comments, f, width, height, bottomReserved, fontface, fontsize, alpha, duration_marquee, duration_still, filter_regex, reduced, progress_callback):
def ProcessComments(comments, f, width, height, bottomReserved, fontface, fontsize, alpha, duration_marquee, duration_still, filters_regex, reduced, progress_callback):
styleid = 'Danmaku2ASS_%04x' % random.randint(0, 0xffff)
WriteASSHead(f, width, height, fontface, fontsize, alpha, styleid)
rows = [[None] * (height - bottomReserved + 1) for i in range(4)]
@ -520,7 +520,12 @@ def ProcessComments(comments, f, width, height, bottomReserved, fontface, fontsi
if progress_callback and idx % 1000 == 0:
progress_callback(idx, len(comments))
if isinstance(i[4], int):
skip = False
for filter_regex in filters_regex:
if filter_regex and filter_regex.search(i[3]):
skip = True
break
if skip:
continue
row = 0
rowmax = height - bottomReserved - i[7]
@ -726,12 +731,17 @@ def export(func):
@export
def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_height, reserve_blank=0, font_face=_('(FONT) sans-serif')[7:], font_size=25.0, text_opacity=1.0, duration_marquee=5.0, duration_still=5.0, comment_filter=None, is_reduce_comments=False, progress_callback=None):
def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_height, reserve_blank=0, font_face=_('(FONT) sans-serif')[7:], font_size=25.0, text_opacity=1.0, duration_marquee=5.0, duration_still=5.0, comment_filter=None, comment_filters_file=None, is_reduce_comments=False, progress_callback=None):
comment_filters = [comment_filter]
if comment_filters_file:
with open(comment_filters_file, 'r') as f:
d = f.readlines()
comment_filters.extend([i.strip() for i in d])
filters_regex = []
for comment_filter in comment_filters:
try:
if comment_filter:
filter_regex = re.compile(comment_filter)
else:
filter_regex = None
filters_regex.append(re.compile(comment_filter))
except:
raise ValueError(_('Invalid regular expression: %s') % comment_filter)
fo = None
@ -741,7 +751,7 @@ def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_heigh
fo = ConvertToFile(output_file, 'w', encoding='utf-8-sig', errors='replace', newline='\r\n')
else:
fo = sys.stdout
ProcessComments(comments, fo, stage_width, stage_height, reserve_blank, font_face, font_size, text_opacity, duration_marquee, duration_still, filter_regex, is_reduce_comments, progress_callback)
ProcessComments(comments, fo, stage_width, stage_height, reserve_blank, font_face, font_size, text_opacity, duration_marquee, duration_still, filters_regex, is_reduce_comments, progress_callback)
finally:
if output_file and fo != output_file:
fo.close()
@ -800,6 +810,7 @@ def main():
parser.add_argument('-dm', '--duration-marquee', metavar=_('SECONDS'), help=_('Duration of scrolling comment display [default: %s]') % 5, type=float, default=5.0)
parser.add_argument('-ds', '--duration-still', metavar=_('SECONDS'), help=_('Duration of still comment display [default: %s]') % 5, type=float, default=5.0)
parser.add_argument('-fl', '--filter', help=_('Regular expression to filter comments'))
parser.add_argument('-flf', '--filter-file', help=_('Regular expressions from file (one line one regex) to filter comments'))
parser.add_argument('-p', '--protect', metavar=_('HEIGHT'), help=_('Reserve blank on the bottom of the stage'), type=int, default=0)
parser.add_argument('-r', '--reduce', action='store_true', help=_('Reduce the amount of comments if stage is full'))
parser.add_argument('file', metavar=_('FILE'), nargs='+', help=_('Comment file to be processed'))
@ -810,7 +821,7 @@ def main():
height = int(height)
except ValueError:
raise ValueError(_('Invalid stage size: %r') % args.size)
Danmaku2ASS(args.file, args.format, args.output, width, height, args.protect, args.font, args.fontsize, args.alpha, args.duration_marquee, args.duration_still, args.filter, args.reduce)
Danmaku2ASS(args.file, args.format, args.output, width, height, args.protect, args.font, args.fontsize, args.alpha, args.duration_marquee, args.duration_still, args.filter, args.filter_file, args.reduce)
if __name__ == '__main__':