Last active
March 29, 2018 18:12
-
-
Save m-butterfield/ca9b10b4c7e449c52b97 to your computer and use it in GitHub Desktop.
hipchatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
hipchatter | |
Import ALL THE EMOJI (allthethings) from Hipchat into Slack! | |
Usage: | |
hipchatter <slack-team> <slack-cookie> | |
Options: | |
-h --help Show this screen. | |
""" | |
import os | |
import shutil | |
from tempfile import NamedTemporaryFile | |
from bs4 import BeautifulSoup | |
from docopt import docopt | |
import requests | |
CMDARGS = docopt(__doc__) | |
EMOJI_URL = "https://www.hipchat.com/emoticons" | |
SLACK_BASE_URL = "https://{}.slack.com/customize/emoji" | |
def hipchatter(slack_team, slack_cookie): | |
headers = {'Cookie': slack_cookie} | |
slack_url = SLACK_BASE_URL.format(slack_team) | |
for div in _get_img_divs(): | |
_copy_emoji(div.find("img")["src"], slack_url, headers) | |
def _get_img_divs(): | |
soup = BeautifulSoup(requests.get(EMOJI_URL).text, 'html.parser') | |
return soup.findAll("div", {"class": "emoticon-block"}) | |
def _copy_emoji(url, slack_url, headers): | |
file_name = _download(url) | |
emoji_name = os.path.basename(url).split("-")[0] | |
_upload(emoji_name, file_name, slack_url, headers) | |
os.remove(file_name) | |
def _download(url): | |
print("downloading: {}".format(url)) | |
with NamedTemporaryFile(delete=False) as fp: | |
shutil.copyfileobj(requests.get(url, stream=True).raw, fp) | |
return fp.name | |
def _upload(name, file_name, slack_url, headers): | |
print("uploading: {}".format(name)) | |
data = { | |
'add': 1, | |
'crumb': _get_crumb(slack_url, headers), | |
'name': name, | |
'mode': 'data', | |
} | |
with open(file_name, 'rb') as fp: | |
files = {'img': fp} | |
response = requests.post(slack_url, | |
headers=headers, | |
data=data, | |
files=files, | |
allow_redirects=False) | |
response.raise_for_status() | |
def _get_crumb(slack_url, headers): | |
response = requests.get(slack_url, headers=headers) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.text, "html.parser") | |
return soup.find("input", attrs={"name": "crumb"})["value"] | |
if __name__ == '__main__': | |
hipchatter(CMDARGS["<slack-team>"], CMDARGS["<slack-cookie>"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beautifulsoup4==4.6.0 | |
certifi==2018.1.18 | |
chardet==3.0.4 | |
docopt==0.6.2 | |
idna==2.6 | |
requests==2.18.4 | |
urllib3==1.22 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment