Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import abc
  2. import hashlib
  3. import json
  4. import shutil
  5. import time
  6. import urllib.parse
  7. import urllib.request
  8. import textwrap
  9. import sys
  10. class Authorization:
  11. def __init__(self, filename):
  12. with open(filename, "r") as file:
  13. lines = file.readlines()
  14. if len(lines) != 2:
  15. sys.exit("Incorrect api-keys file")
  16. self.public_key = lines[0].strip()
  17. self.private_key = lines[1].strip()
  18. def generate_params(self):
  19. params = dict()
  20. params["apikey"] = self.public_key
  21. ts = str(time.time())
  22. to_hash = ts + self.private_key + self.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. return params
  29. class Client:
  30. base_url = "http://gateway.marvel.com/v1/public"
  31. def __init__(self, auth):
  32. self.auth = auth
  33. def make_request(self, query):
  34. params = self.auth.generate_params()
  35. params.update(query.params())
  36. query_string = urllib.parse.urlencode(params)
  37. full_url = Client.base_url + query.path() + "?" + query_string
  38. with urllib.request.urlopen(full_url) as response:
  39. status_code = response.getcode()
  40. if status_code != 200:
  41. sys.exit("got status: " + str(status_code))
  42. body = json.loads(response.read())
  43. attribution = body["attributionText"]
  44. response = query.extract(body)
  45. return (response, attribution)
  46. class Display:
  47. def __init__(self, width=80):
  48. self.width = width
  49. def display(self, text):
  50. print("\n".join(textwrap.wrap(text, width=self.width)))
  51. class Query(metaclass=abc.ABCMeta):
  52. @abc.abstractmethod
  53. def params(self):
  54. pass
  55. @abc.abstractmethod
  56. def path(self):
  57. pass
  58. @abc.abstractmethod
  59. def extract(self, body):
  60. pass
  61. @abc.abstractmethod
  62. def text(self, response):
  63. pass
  64. class CharacterDescription(Query):
  65. def __init__(self, name):
  66. self.name = name
  67. def params(self):
  68. return {"name": self.name}
  69. def path(self):
  70. return "/characters"
  71. def extract(self, body):
  72. first_result = body["data"]["results"][0]
  73. description = first_result["description"]
  74. return description
  75. def text(self, response):
  76. return response
  77. class CreatorNumberOfSeries(Query):
  78. def __init__(self, first_name, last_name):
  79. self.first_name = first_name
  80. self.last_name = last_name
  81. def params(self):
  82. return {"firstName": self.first_name, "lastName": self.last_name}
  83. def path(self):
  84. return "/creators"
  85. def extract(self, body):
  86. first_result = body["data"]["results"][0]
  87. return first_result["series"]["available"]
  88. def text(self, response):
  89. return "{} {} worked on {} series".format(
  90. self.first_name, self.last_name, response
  91. )
  92. def main():
  93. auth = Authorization("api-keys.txt")
  94. query_type = sys.argv[1]
  95. if query_type == "character-description":
  96. name = sys.argv[2]
  97. query = CharacterDescription(name)
  98. elif query_type == "creator-number-of-series":
  99. first_name = sys.argv[2]
  100. last_name = sys.argv[3]
  101. query = CreatorNumberOfSeries(first_name, last_name)
  102. else:
  103. sys.exit("Unkwnon query type: {}".format(query_type))
  104. client = Client(auth)
  105. response, attribution = client.make_request(query)
  106. text = query.text(response)
  107. terminal_size = shutil.get_terminal_size()
  108. columns = terminal_size.columns
  109. terminal = Display(width=columns)
  110. terminal.display(text)
  111. terminal.display("---")
  112. terminal.display(attribution)
  113. if __name__ == "__main__":
  114. main()