From 0d773f2d68894ff6a6705c026fa670f8088895f4 Mon Sep 17 00:00:00 2001 From: Denbeigh Stevens Date: Mon, 2 Sep 2019 11:04:49 -0700 Subject: [PATCH 1/2] fixed scraping for slack ui changes, improve failure messaging --- upload.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/upload.py b/upload.py index 5980700..7777875 100755 --- a/upload.py +++ b/upload.py @@ -21,9 +21,11 @@ URL_CUSTOMIZE = "https://{team_name}.slack.com/customize/emoji" URL_ADD = "https://{team_name}.slack.com/api/emoji.add" URL_LIST = "https://{team_name}.slack.com/api/emoji.adminList" -API_TOKEN_REGEX = r"api_token: \"(.*)\"," +API_TOKEN_REGEX = r'.*(?:\"?api_token\"?):\s*\"([^"]+)\".*' API_TOKEN_PATTERN = re.compile(API_TOKEN_REGEX) +class ParseError(Exception): + pass def _session(args): assert args.cookie, "Cookie required" @@ -89,9 +91,16 @@ def _fetch_api_token(session): for line in script.text.splitlines(): if 'api_token' in line: # api_token: "xoxs-12345-abcdefg....", - return API_TOKEN_PATTERN.match(line.strip()).group(1) + # "api_token":"xoxs-12345-abcdefg....", + match_group = API_TOKEN_PATTERN.match(line.strip()) + if not match_group: + raise ParseError( + "Could not parse API token from remote data! " + "Regex requires updating." + ) + + return match_group.group(1) - raise Exception('api_token not found. response status={}'.format(r.status_code)) def main(): From fdd77eb853623daaee9172e85eabdd4b32ca5059 Mon Sep 17 00:00:00 2001 From: Denbeigh Stevens Date: Mon, 2 Sep 2019 11:40:30 -0700 Subject: [PATCH 2/2] added exponential backoff for rate limiting --- upload.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/upload.py b/upload.py index 7777875..42b057e 100755 --- a/upload.py +++ b/upload.py @@ -8,10 +8,12 @@ from __future__ import print_function import argparse import os import re -import requests +from time import sleep from bs4 import BeautifulSoup +import requests + try: raw_input except NameError: @@ -101,6 +103,7 @@ def _fetch_api_token(session): return match_group.group(1) + raise ParseError("No api_token found in page") def main(): @@ -136,9 +139,9 @@ def get_current_emoji_list(session): 'count': 1000, 'token': session.api_token } - r = session.post(session.url_list, data=data) - r.raise_for_status() - response_json = r.json() + resp = session.post(session.url_list, data=data) + resp.raise_for_status() + response_json = resp.json() result.extend(map(lambda e: e["name"], response_json["emoji"])) if page >= response_json["paging"]["pages"]: @@ -154,14 +157,28 @@ def upload_emoji(session, emoji_name, filename): 'name': emoji_name, 'token': session.api_token } - files = {'image': open(filename, 'rb')} - r = session.post(session.url_add, data=data, files=files, allow_redirects=False) - r.raise_for_status() - # Slack returns 200 OK even if upload fails, so check for status. - response_json = r.json() - if not response_json['ok']: - print("Error with uploading %s: %s" % (emoji_name, response_json)) + i = 0 + while True: + i += 1 + with open(filename, 'rb') as f: + files = {'image': f} + resp = session.post(session.url_add, data=data, files=files, allow_redirects=False) + + if resp.status_code == 429: + wait = 2**i + print("429 Too Many Requests!, sleeping for %d seconds" % wait) + sleep(wait) + continue + + resp.raise_for_status() + + # Slack returns 200 OK even if upload fails, so check for status. + response_json = resp.json() + if not response_json['ok']: + print("Error with uploading %s: %s" % (emoji_name, response_json)) + + break if __name__ == '__main__':