From bb8f1cee060dc1c7e6426159ebadbdb05bf8e901 Mon Sep 17 00:00:00 2001 From: Chaigidel Date: Mon, 24 Feb 2020 18:43:40 +0800 Subject: [PATCH 1/4] support filters in json --- danmaku2ass.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/danmaku2ass.py b/danmaku2ass.py index 67e7348..8a0b399 100755 --- a/danmaku2ass.py +++ b/danmaku2ass.py @@ -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): - if filter_regex and filter_regex.search(i[3]): + 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,14 +731,18 @@ 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): - try: - if comment_filter: - filter_regex = re.compile(comment_filter) - else: - filter_regex = None - except: - raise ValueError(_('Invalid regular expression: %s') % comment_filter) +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_json=None, is_reduce_comments=False, progress_callback=None): + comment_filters = [comment_filter] + if comment_filters_json: + with open(comment_filters_json, 'r') as f: + comment_filters.extend(json.load(f)) + filters_regex = [] + for comment_filter in comment_filters: + try: + if comment_filter: + filters_regex.append(re.compile(comment_filter)) + except: + raise ValueError(_('Invalid regular expression: %s') % comment_filter) fo = None comments = ReadComments(input_files, input_format, font_size) try: @@ -741,7 +750,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 +809,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 json file 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 +820,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__': From ce41af1468f4bf012b4f5f32b816b96608861bab Mon Sep 17 00:00:00 2001 From: Chaigidel Date: Mon, 24 Feb 2020 18:52:39 +0800 Subject: [PATCH 2/4] readable help --- README.md | 2 ++ danmaku2ass.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5191f9..b968a55 100644 --- a/README.md +++ b/README.md @@ -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 json file (contain a regex list) 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 diff --git a/danmaku2ass.py b/danmaku2ass.py index 8a0b399..e8f4ce0 100755 --- a/danmaku2ass.py +++ b/danmaku2ass.py @@ -809,7 +809,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 json file to filter comments')) + parser.add_argument('-flf', '--filter-file', help=_('Regular expressions from json file (contain a regex list) 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')) From 1add7b3b52bfcc821625c7ccb9c4d4a7303827d4 Mon Sep 17 00:00:00 2001 From: Chaigidel Date: Mon, 24 Feb 2020 23:00:10 +0800 Subject: [PATCH 3/4] simplify --- README.md | 2 +- danmaku2ass.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b968a55..a1f7e10 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ optional arguments: -fl FILTER, --filter FILTER Regular expression to filter comments -flf FILTER_FILE, --filter-file FILTER_FILE - Regular expressions from json file (contain a regex list) to filter comments + 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 diff --git a/danmaku2ass.py b/danmaku2ass.py index e8f4ce0..f9043c2 100755 --- a/danmaku2ass.py +++ b/danmaku2ass.py @@ -735,7 +735,8 @@ def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_heigh comment_filters = [comment_filter] if comment_filters_json: with open(comment_filters_json, 'r') as f: - comment_filters.extend(json.load(f)) + d = f.readlines() + comment_filters.extend([i.strip() for i in d]) filters_regex = [] for comment_filter in comment_filters: try: @@ -809,7 +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 json file (contain a regex list) 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')) From 4c4de516a1f1d14aca4e41f80b1ed553da6ee344 Mon Sep 17 00:00:00 2001 From: Chaigidel Date: Tue, 25 Feb 2020 00:01:11 +0800 Subject: [PATCH 4/4] change arg name --- danmaku2ass.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/danmaku2ass.py b/danmaku2ass.py index f9043c2..1eb91d4 100755 --- a/danmaku2ass.py +++ b/danmaku2ass.py @@ -731,10 +731,10 @@ 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, comment_filters_json=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_json: - with open(comment_filters_json, 'r') as f: + 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 = []