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.

marvel_05.py 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 InformationRetriever:
  27. base_url = "http://gateway.marvel.com/v1/public"
  28. def __init__(self, auth):
  29. self.auth = auth
  30. def make_request(self, path, query_params):
  31. params = self.auth.generate_params()
  32. params.update(query_params)
  33. url = InformationRetriever.base_url + path
  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. attribution = body["attributionText"]
  40. return (body, attribution)
  41. class CharacterDescriptionGetter(InformationRetriever):
  42. def get_character_description(self, name):
  43. params = {"name": name}
  44. body, attribution = self.make_request("/characters",
  45. params)
  46. first_result = body["data"]["results"][0]
  47. description = first_result["description"]
  48. return (description, attribution)
  49. class CreatorNumberOfSeriesGetter(InformationRetriever):
  50. def get_number_of_series(self, first_name, last_name):
  51. params = {
  52. "firstName": first_name,
  53. "lastName": last_name,
  54. }
  55. body, attribution = self.make_request("/creators",
  56. params)
  57. first_result = body["data"]["results"][0]
  58. return (first_result["series"]["available"], attribution)
  59. class Display:
  60. def __init__(self, width=80):
  61. self.width = width
  62. def display(self, text):
  63. print("\n".join(textwrap.wrap(text, width=self.width)))
  64. def main():
  65. auth = Authorization("api-keys.txt")
  66. query = sys.argv[1]
  67. if query == "character-description":
  68. name = sys.argv[2]
  69. character_description_getter = CharacterDescriptionGetter(auth)
  70. description, attribution = character_description_getter.get_character_description(name)
  71. text = description
  72. elif query == "creator-number-of-series":
  73. first_name = sys.argv[2]
  74. last_name = sys.argv[3]
  75. creator_number_of_series_getter = CreatorNumberOfSeriesGetter(auth)
  76. result, attribution = creator_number_of_series_getter.get_number_of_series(first_name, last_name)
  77. text = first_name + " " + last_name + " worked on " + str(result) + " series"
  78. terminal_size = shutil.get_terminal_size()
  79. columns = terminal_size.columns
  80. terminal = Display(width=columns)
  81. terminal.display(text)
  82. terminal.display("---")
  83. terminal.display(attribution)
  84. if __name__ == "__main__":
  85. main()