您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
此存儲庫已封存,您能瀏覽檔案及複製此存儲庫,但不能推送、建立問題及拉取請求。
 
 
 
 
 
 

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