您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

marvel_06.py 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # WARNING: Don't read this unless you want to get spoiled!
  2. import abc
  3. import hashlib
  4. import requests
  5. import shutil
  6. import time
  7. import textwrap
  8. import sys
  9. class Authorization:
  10. def __init__(self, filename):
  11. with open(filename, "r") as file:
  12. lines = file.readlines()
  13. if len(lines) != 2:
  14. sys.exit("Incorrect api-keys file")
  15. self.public_key = lines[0].strip()
  16. self.private_key = lines[1].strip()
  17. def generate_params(self):
  18. params = dict()
  19. params["apikey"] = self.public_key
  20. ts = str(time.time())
  21. to_hash = ts + self.private_key + self.public_key
  22. hasher = hashlib.md5()
  23. hasher.update(to_hash.encode())
  24. digest = hasher.hexdigest()
  25. params["ts"] = ts
  26. params["hash"] = digest
  27. return params
  28. class Client():
  29. base_url = "http://gateway.marvel.com/v1/public"
  30. def __init__(self, auth):
  31. self.auth = auth
  32. def make_request(self, query):
  33. params = self.auth.generate_params()
  34. params.update(query.params())
  35. url = Client.base_url + query.path
  36. response = requests.get(url, params=params)
  37. status_code = response.status_code
  38. if status_code != 200:
  39. sys.exit("got status: " + str(status_code))
  40. body = response.json()
  41. result = query.extract(body)
  42. return (query.to_text(result), body["attributionText"])
  43. class Query(metaclass=abc.ABCMeta):
  44. @abc.abstractmethod
  45. def params(self):
  46. pass
  47. @abc.abstractmethod
  48. def path(self):
  49. pass
  50. @abc.abstractmethod
  51. def extract(self, body):
  52. pass
  53. def to_text(self, result):
  54. return result
  55. class CharacterDescription(Query):
  56. path = "/characters"
  57. def __init__(self, name):
  58. self.name = name
  59. def params(self):
  60. return { "name": self.name }
  61. def extract(self, body):
  62. return body["data"]["results"][0]["description"]
  63. class CreatorNumberOfSeries(Query):
  64. path = "/creators"
  65. def __init__(self, first_name, last_name):
  66. self.first_name = first_name
  67. self.last_name = last_name
  68. def params(self):
  69. return { "firstName": self.first_name, "lastName": self.last_name }
  70. def extract(self, body):
  71. return body["data"]["results"][0]["series"]["available"]
  72. def to_text(self, result):
  73. return f"{self.first_name} {self.last_name} worked on {result} series"
  74. class Display:
  75. def __init__(self, width=80):
  76. self.width = width
  77. def display(self, text):
  78. print("\n".join(textwrap.wrap(text, width=self.width)))
  79. def main():
  80. query_type = sys.argv[1]
  81. if query_type == "character-description":
  82. name = sys.argv[2]
  83. query = CharacterDescription(name)
  84. elif query_type == "creator-number-of-series":
  85. first_name = sys.argv[2]
  86. last_name = sys.argv[3]
  87. query = CreatorNumberOfSeries(first_name, last_name)
  88. auth = Authorization("api-keys.txt")
  89. client = Client(auth)
  90. text, attribution = client.make_request(query)
  91. terminal_size = shutil.get_terminal_size()
  92. columns = terminal_size.columns
  93. terminal = Display(width=columns)
  94. terminal.display(text)
  95. terminal.display("---")
  96. terminal.display(attribution)
  97. if __name__ == "__main__":
  98. main()