No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
Este repositorio está archivado. Puede ver los archivos y clonarlo, pero no puede subir cambios o reportar incidencias ni pedir Pull Requests.
 
 
 
 
 
 

81 líneas
2.0 KiB

  1. import hashlib
  2. import requests
  3. import shutil
  4. import time
  5. import textwrap
  6. import sys
  7. class Authorization:
  8. def __init__(self, filename):
  9. with open(filename, "r") as file:
  10. lines = file.readlines()
  11. if len(lines) != 2:
  12. sys.exit("Incorrect api-keys file")
  13. self.public_key = lines[0].strip()
  14. self.private_key = lines[1].strip()
  15. def generate_params(self):
  16. params = dict()
  17. params["apikey"] = self.public_key
  18. ts = str(time.time())
  19. to_hash = ts + self.private_key + self.public_key
  20. hasher = hashlib.md5()
  21. hasher.update(to_hash.encode())
  22. digest = hasher.hexdigest()
  23. params["ts"] = ts
  24. params["hash"] = digest
  25. return params
  26. class Client:
  27. base_url = "http://gateway.marvel.com/v1/public"
  28. def __init__(self, auth):
  29. self.auth = auth
  30. def get_character_description(self, name):
  31. params = self.auth.generate_params()
  32. params["name"] = name
  33. url = Client.base_url + "/characters"
  34. response = requests.get(url, params=params)
  35. status_code = response.status_code
  36. if status_code != 200:
  37. sys.exit("got status: " + str(status_code))
  38. body = response.json()
  39. description = body["data"]["results"][0]["description"]
  40. attribution = body["attributionText"]
  41. return (description, attribution)
  42. class Display:
  43. def __init__(self, width=80):
  44. self.width = width
  45. def display(self, text):
  46. print("\n".join(textwrap.wrap(text, width=self.width)))
  47. def main():
  48. name = sys.argv[1]
  49. auth = Authorization("api-keys.txt")
  50. client = Client(auth)
  51. description, attribution = client.get_character_description(name)
  52. terminal_size = shutil.get_terminal_size()
  53. columns = terminal_size.columns
  54. terminal = Display(width=columns)
  55. terminal.display(description)
  56. terminal.display("---")
  57. terminal.display(attribution)
  58. if __name__ == "__main__":
  59. main()