Add regular expression filter

This commit is contained in:
Alibek Omarov 2020-03-26 23:09:49 +03:00
parent f5225a6008
commit 4611e148ee

31
meow.py
View File

@ -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))