util.py 707 B

123456789101112131415161718192021222324
  1. from json.decoder import JSONDecodeError
  2. import requests
  3. from requests.exceptions import ConnectionError
  4. def upload_image(key, content):
  5. try:
  6. return True, requests.post(
  7. "https://api.imgur.com/3/image",
  8. headers={
  9. "Authorization": f"Client-ID {key}",
  10. },
  11. data=content
  12. ).json()["data"]["link"]
  13. except ConnectionError as ce:
  14. return False, {
  15. "explain": "Could not reach image service.",
  16. "exception": ce
  17. }
  18. except (KeyError, JSONDecodeError) as e:
  19. return False, {
  20. "explain": "Could not parse response from image service.",
  21. "exception": e
  22. }