103 lines
2 KiB
Python
103 lines
2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Simple Emoji Grabber
|
|
by Izalia Mae
|
|
|
|
Installation: pip3 install mastodon.py
|
|
"""
|
|
import requests, sys, tarfile, tempfile
|
|
|
|
from mastodon import Mastodon
|
|
from pathlib import Path
|
|
|
|
|
|
version = '0.1'
|
|
name = f'SimpleEmojiGrabber/{version}'
|
|
|
|
|
|
if len(sys.argv) < 2 or 'help' in sys.argv:
|
|
filename = Path(__file__)
|
|
print(name+'\n')
|
|
print(f'Usage:\n {filename.name} <domain> [path]\n')
|
|
print('domain:\n The web domain to connect to')
|
|
print('path:\n The base path for the tar file. It will be saved as [path]/[domain].tar.gz')
|
|
sys.exit()
|
|
|
|
|
|
host = sys.argv[1]
|
|
failed = []
|
|
options = {
|
|
'api_base_url': 'https://'+host
|
|
}
|
|
|
|
try:
|
|
tokens = json.load('emoji_tokens.json')
|
|
except:
|
|
tokens = {}
|
|
|
|
token = tokens.get(host)
|
|
|
|
if token:
|
|
options['access_token'] = token
|
|
|
|
mastodon = Mastodon(**options)
|
|
|
|
|
|
if len(sys.argv) < 3:
|
|
basepath = Path.cwd()
|
|
|
|
else:
|
|
patharg = sys.argv[2]
|
|
|
|
if patharg.startswith('~'):
|
|
basepath = Path.expanduser(patharg)
|
|
|
|
elif patharg.startswith('/'):
|
|
basepath = Path(patharg)
|
|
|
|
else:
|
|
basepath = Path.cwd().joinpath(patharg)
|
|
|
|
|
|
basepath.mkdir(parents=True, exist_ok=True)
|
|
path = basepath.joinpath(host+'.tar.gz')
|
|
|
|
|
|
data = mastodon.custom_emojis()
|
|
|
|
if not data:
|
|
sys.exit()
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
with tarfile.open(str(path), 'w:gz') as tar:
|
|
tempdir = Path(tmpdir)
|
|
|
|
for emoji in data:
|
|
emojipath = tempdir.joinpath(f'{emoji.shortcode}.png')
|
|
|
|
if emojipath.exists():
|
|
continue
|
|
|
|
response = requests.get(emoji.url, headers={'User-Agent': name})
|
|
|
|
if not response:
|
|
print('Failed to fetch emoji:', emoji.shortcode)
|
|
failed.append(emoji.shortcode)
|
|
continue
|
|
|
|
with emojipath.open('wb') as fd:
|
|
#print(f'Saving emoji: {emoji.shortcode}, {emojipath}')
|
|
for chunk in response.iter_content(chunk_size=1024):
|
|
fd.write(chunk)
|
|
|
|
print(f'Adding emoji: {emojipath.name}')
|
|
tar.add(emojipath, emojipath.name)
|
|
|
|
|
|
print('Saved emojis in an archive at', path)
|
|
|
|
|
|
if len(failed) > 0:
|
|
print('Failed to save emojis:', ', '.join(failed))
|