Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Pode ver ficheiros e cloná-lo, mas não pode fazer envios ou lançar questões ou pedidos de integração.
 
 
 
 
 
 

62 linhas
1.4 KiB

  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. if len(lines) != 2:
  14. sys.exit("Incorrect api-keys file")
  15. public_key = lines[0].strip()
  16. private_key = lines[1].strip()
  17. # base url
  18. base_url = "http://gateway.marvel.com/v1/public"
  19. # Authorization stuff
  20. params = dict()
  21. params["apikey"] = public_key
  22. ts = str(time.time())
  23. to_hash = ts + private_key + public_key
  24. hasher = hashlib.md5()
  25. hasher.update(to_hash.encode())
  26. digest = hasher.hexdigest()
  27. params["ts"] = ts
  28. params["hash"] = digest
  29. # Perform the request
  30. params["name"] = name
  31. url = base_url + "/characters"
  32. response = requests.get(url, params=params)
  33. status_code = response.status_code
  34. if status_code != 200:
  35. sys.exit("got status: " + str(status_code))
  36. body = response.json()
  37. description = body["data"]["results"][0]["description"]
  38. # Print the description
  39. terminal_size = shutil.get_terminal_size()
  40. columns = terminal_size.columns
  41. print("\n".join(textwrap.wrap(description, width=columns)))
  42. # Print attribution (comply with API agreement)
  43. print("---")
  44. print(body["attributionText"])
  45. if __name__ == "__main__":
  46. main()