From 4611e148ee32223fda496c97064df90333b1d2f3 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 26 Mar 2020 23:09:49 +0300 Subject: [PATCH] Add regular expression filter --- meow.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/meow.py b/meow.py index 8988285..f44c732 100755 --- a/meow.py +++ b/meow.py @@ -18,6 +18,7 @@ import requests import json import sys import os +import re from optparse import OptionParser DISCORD_API_GUILD = 'https://discordapp.com/api/v6/guilds/%s' @@ -42,7 +43,7 @@ def discord_cdn_get(url, out, force): r = requests.get(url, allow_redirects=True) open(out, 'wb').write(r.content) -def main(token, guild, force = False, out = None, ua = None): +def main(token, guild, force = False, out = None, include = None, exclude = None, ua = None): if out is None: out = guild @@ -71,16 +72,33 @@ def main(token, guild, force = False, out = None, ua = None): } } + regex = None + if include != None: + is_include = True + regex = re.compile(include) + elif exclude != None: + is_include = False + regex = re.compile(exclude) + for emoji in emojilist: + name = emoji['name'] + + if regex != None: + if (regex.search(name) != None) != is_include: + print('Skipping %s, %s match regex' % (name, 'doesn\'t' if is_include else 'does')) + continue + animated = emoji['animated'] id = emoji['id'] - name = emoji['name'] ext = 'gif' if animated else 'png' discord_cdn_get(DISCORD_CDN_EMOJI % (id, ext), os.path.join(out, name + '.' + ext), force) packjson['files'][name] = name + '.' + ext + if len(packjson['files']) == 0: + die('Server doesn\'t have emojis or they were excluded by regular expression') + # always write pack.json for consistency print('Writing pack.json...') open(os.path.join(out, 'pack.json'), 'w').write(json.dumps(packjson)) @@ -99,6 +117,10 @@ if __name__ == '__main__': help='result emojipack name') parser.add_option('-f', '--force', action='store_true', dest='force', help='ignore existing files') + parser.add_option('-i', '--include', action='store', dest='inc', + help='download emojis matching regular expression(incompatible with -x)') + parser.add_option('-x', '--exclude', action='store', dest='exc', + help='exclude emojis matching regular expression(incompatible with -i)') (options, args) = parser.parse_args() @@ -112,4 +134,7 @@ if __name__ == '__main__': config.AUTHORIZATION_TOKEN, options.guild, options.force, - options.out, config.USER_AGENT)) + options.out, + options.inc, + options.exc, + config.USER_AGENT))