Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

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