Add regular expression filter
This commit is contained in:
parent
f5225a6008
commit
4611e148ee
31
meow.py
31
meow.py
|
@ -18,6 +18,7 @@ import requests
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
DISCORD_API_GUILD = 'https://discordapp.com/api/v6/guilds/%s'
|
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)
|
r = requests.get(url, allow_redirects=True)
|
||||||
open(out, 'wb').write(r.content)
|
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:
|
if out is None:
|
||||||
out = guild
|
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:
|
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']
|
animated = emoji['animated']
|
||||||
id = emoji['id']
|
id = emoji['id']
|
||||||
name = emoji['name']
|
|
||||||
|
|
||||||
ext = 'gif' if animated else 'png'
|
ext = 'gif' if animated else 'png'
|
||||||
|
|
||||||
discord_cdn_get(DISCORD_CDN_EMOJI % (id, ext), os.path.join(out, name + '.' + ext), force)
|
discord_cdn_get(DISCORD_CDN_EMOJI % (id, ext), os.path.join(out, name + '.' + ext), force)
|
||||||
packjson['files'][name] = name + '.' + ext
|
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
|
# always write pack.json for consistency
|
||||||
print('Writing pack.json...')
|
print('Writing pack.json...')
|
||||||
open(os.path.join(out, 'pack.json'), 'w').write(json.dumps(packjson))
|
open(os.path.join(out, 'pack.json'), 'w').write(json.dumps(packjson))
|
||||||
|
@ -99,6 +117,10 @@ if __name__ == '__main__':
|
||||||
help='result emojipack name')
|
help='result emojipack name')
|
||||||
parser.add_option('-f', '--force', action='store_true', dest='force',
|
parser.add_option('-f', '--force', action='store_true', dest='force',
|
||||||
help='ignore existing files')
|
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()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
@ -112,4 +134,7 @@ if __name__ == '__main__':
|
||||||
config.AUTHORIZATION_TOKEN,
|
config.AUTHORIZATION_TOKEN,
|
||||||
options.guild,
|
options.guild,
|
||||||
options.force,
|
options.force,
|
||||||
options.out, config.USER_AGENT))
|
options.out,
|
||||||
|
options.inc,
|
||||||
|
options.exc,
|
||||||
|
config.USER_AGENT))
|
||||||
|
|
Loading…
Reference in New Issue