Skip to content

Commit

Permalink
added exponential backoff for rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
denbeigh2000 committed Sep 2, 2019
1 parent 0d773f2 commit fdd77eb
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import argparse
import os
import re
import requests
from time import sleep

from bs4 import BeautifulSoup

import requests

try:
raw_input
except NameError:
Expand Down Expand Up @@ -101,6 +103,7 @@ def _fetch_api_token(session):

return match_group.group(1)

raise ParseError("No api_token found in page")


def main():
Expand Down Expand Up @@ -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"]:
Expand All @@ -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__':
Expand Down

0 comments on commit fdd77eb

Please sign in to comment.