From 0d773f2d68894ff6a6705c026fa670f8088895f4 Mon Sep 17 00:00:00 2001 From: Denbeigh Stevens Date: Mon, 2 Sep 2019 11:04:49 -0700 Subject: [PATCH] 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():