You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

get-character-description.py 1.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import hashlib
  2. import requests
  3. import shutil
  4. import time
  5. import textwrap
  6. import sys
  7. def main():
  8. # Get name from command line
  9. name = sys.argv[1]
  10. # Read api keys from the text file
  11. with open("api-keys.txt", "r") as file:
  12. lines = file.readlines()
  13. assert len(lines) == 2, "Incorrect api-keys file"
  14. public_key = lines[0].strip()
  15. private_key = lines[1].strip()
  16. # base url
  17. base_url = "http://gateway.marvel.com/v1/public"
  18. # Authorizsation stuff
  19. params = dict()
  20. params["apikey"] = public_key
  21. ts = str(time.time())
  22. to_hash = ts + private_key + public_key
  23. hasher = hashlib.md5()
  24. hasher.update(to_hash.encode())
  25. digest = hasher.hexdigest()
  26. params["ts"] = ts
  27. params["hash"] = digest
  28. # Perform the request
  29. params["name"] = name
  30. url = base_url + "/characters"
  31. response = requests.get(url, params=params)
  32. status_code = response.status_code
  33. assert status_code == 200, "got status: " + str(status_code)
  34. body = response.json()
  35. description = body["data"]["results"][0]["description"]
  36. # Print the description
  37. terminal_size = shutil.get_terminal_size()
  38. columns = terminal_size.columns
  39. print("\n".join(textwrap.wrap(description, width=columns)))
  40. # Print attribution (comply with API agreement)
  41. print("---")
  42. print(body["attributionText"])
  43. if __name__ == "__main__":
  44. main()