Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.
 
 
 
 
 
 

85 Zeilen
2.1 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):
  9. self.public_key = None
  10. self.private_key = None
  11. def get_keys(self):
  12. with open("api-keys.txt", "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. def __init__(self, base_url, auth):
  31. self.base_url = base_url
  32. self.auth = auth
  33. def get_character_description(self,name):
  34. params = self.auth.generate_params()
  35. params["name"] = name
  36. response = requests.get(self.base_url+"/characters", 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. description = body["data"]["results"][0]["description"]
  42. attribution = body["attributionText"]
  43. return (description, attribution)
  44. class Display:
  45. def __init__(self, width=80):
  46. self.width = width
  47. def display(self, text):
  48. print("\n".join(textwrap.wrap(text, width=self.width)))
  49. def main():
  50. name = sys.argv[1]
  51. base_url = "http://gateway.marvel.com/v1/public"
  52. auth = Authorization()
  53. auth.get_keys()
  54. client = Client(base_url, auth)
  55. description, attribution = client.get_character_description(name)
  56. terminal_size = shutil.get_terminal_size()
  57. columns = terminal_size.columns
  58. terminal = Display(width=columns)
  59. terminal.display(description)
  60. terminal.display("---")
  61. terminal.display(attribution)
  62. if __name__ == "__main__":
  63. main()