You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

109 lines
3.3 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 CharacterDescriptionGetter:
  27. base_url = "http://gateway.marvel.com/v1/public"
  28. def __init__(self, auth):
  29. self.auth = auth
  30. def get_character_description(self, name):
  31. params = self.auth.generate_params()
  32. params["name"] = name
  33. url = CharacterDescriptionGetter.base_url + "/characters"
  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. description = body["data"]["results"][0]["description"]
  40. attribution = body["attributionText"]
  41. return (description, attribution)
  42. class CreatorNumberOfSeriesGetter:
  43. base_url = "http://gateway.marvel.com/v1/public"
  44. def __init__(self, auth):
  45. self.auth = auth
  46. def get_number_of_series(self, first_name, last_name):
  47. params = self.auth.generate_params()
  48. params["firstName"] = first_name
  49. params["lastName"] = last_name
  50. url = CreatorNumberOfSeriesGetter.base_url + "/creators"
  51. response = requests.get(url, params=params)
  52. status_code = response.status_code
  53. if status_code != 200:
  54. sys.exit("got status: " + str(status_code))
  55. body = response.json()
  56. result = body["data"]["results"][0]
  57. attribution = body["attributionText"]
  58. return 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()