1
0
Fork 0

Merge branch 'pr-14'

This commit is contained in:
Ash Wilson 2017-10-20 21:03:34 -04:00
commit 2e2588175e
4 changed files with 37 additions and 25 deletions

View File

@ -1,6 +1,4 @@
export SLACK_TEAM= export SLACK_TEAM=
export SLACK_COOKIE= export SLACK_COOKIE=
export SLACK_API_TOKEN=
export EMOJI_DIR=
export EMOJI_NAME_PREFIX= export EMOJI_NAME_PREFIX=
export EMOJI_NAME_SUFFIX= export EMOJI_NAME_SUFFIX=

View File

@ -12,6 +12,8 @@ Prepare a directory that contains an image for each emoji you want to create. Re
Clone the project, create a new virtualenv, and install the prereqs: Clone the project, create a new virtualenv, and install the prereqs:
`libxml` is required on your system, if you'd like to use the bulk export script.
```bash ```bash
git clone https://github.com/smashwilson/slack-emojinator.git git clone https://github.com/smashwilson/slack-emojinator.git
cd slack-emojinator cd slack-emojinator
@ -44,9 +46,9 @@ python upload.py ${EMOJI_DIR}/*.png
## Exporting Emoji ## Exporting Emoji
A Slack API token and an export director are needed for the export. They can be added to the `.env` file and sourced. To export emoji, simply use `export.py` and specify an emoji directory:
```bash ```bash
source .env source .env
python export.py ${EMOJI_DIR}/ python export.py path-to-destination/
``` ```

View File

@ -4,47 +4,59 @@
# https://github.com/smashwilson/slack-emojinator # https://github.com/smashwilson/slack-emojinator
from __future__ import print_function from __future__ import print_function
from slacker import Slacker
import requests
import lxml.html
import argparse import argparse
import os import os
import requests
import shutil import shutil
from upload import _session
def _argparse(): def _argparse():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Bulk import of emoji froma slack team' description='Bulk import of emoji from a slack team'
) )
parser.add_argument( parser.add_argument(
'--directory', '-d', 'directory',
default=os.getenv('EMOJI_DIR'), help='Where do we store downloaded emoji?'
help='Defaults to the $EMOJI_DIR environment variable.'
) )
parser.add_argument( parser.add_argument(
'--slack-api-token', '-s', '--team-name', '-t',
default=os.getenv('SLACK_API_TOKEN'), default=os.getenv('SLACK_TEAM'),
help='Defaults to the $SLACK_API_TOKEN environment variable.' help='Defaults to the $SLACK_TEAM environment variable.'
)
parser.add_argument(
'--cookie', '-c',
default=os.getenv('SLACK_COOKIE'),
help='Defaults to the $SLACK_COOKIE environment variable.'
) )
args = parser.parse_args() args = parser.parse_args()
return args return args
def main(): def main():
args = _argparse() args = _argparse()
download_emoji(args.directory, args.slack_api_token)
def download_emoji(directory, slack_api_token): if not os.path.exists(args.directory):
print("slack API token: %s" % slack_api_token) os.makedirs(args.directory)
slack = Slacker(slack_api_token)
if not os.path.exists(directory): session = _session(args)
os.makedirs(directory) resp = session.get(session.url)
emojis = slack.emoji.list() tree = lxml.html.fromstring(resp.text)
for emoji_name, emoji_url in emojis.body['emoji'].items(): urls = tree.xpath(r'//td[@headers="custom_emoji_image"]/span/@data-original')
if "alias" not in emoji_url: names = [u.split('/')[-2] for u in urls]
for emoji_name, emoji_url in zip(names, urls):
if "alias" not in emoji_url: # this does not seem necessary ...
file_extension = emoji_url.split(".")[-1] file_extension = emoji_url.split(".")[-1]
request = requests.get(emoji_url, stream=True) request = session.get(emoji_url, stream=True)
if request.status_code == 200: if request.status_code == 200:
with open('%s/%s.%s' % (directory, emoji_name, file_extension), 'wb') as out_file: filename = '%s/%s.%s' % (args.directory, emoji_name,
file_extension)
with open(filename, 'wb') as out_file:
shutil.copyfileobj(request.raw, out_file) shutil.copyfileobj(request.raw, out_file)
del request del request

View File

@ -1,3 +1,3 @@
beautifulsoup4>=4.4, <5.0 beautifulsoup4>=4.4, <5.0
requests>=2.5.3, <3.0 requests>=2.5.3, <3.0
slacker==0.9.30 lxml==3.7.3