Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

marvel_01.py 1.4 KiB

pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import hashlib
  2. import requests
  3. import shutil
  4. import time
  5. import textwrap
  6. import sys
  7. def main():
  8. # Get name from command line
  9. name = sys.argv[1]
  10. # Read api keys from the text file
  11. with open("api-keys.txt", "r") as file:
  12. lines = file.readlines()
  13. if len(lines) != 2:
  14. sys.exit("Incorrect api-keys file")
  15. public_key = lines[0].strip()
  16. private_key = lines[1].strip()
  17. # base url
  18. base_url = "http://gateway.marvel.com/v1/public"
  19. # Authorization stuff
  20. params = dict()
  21. params["apikey"] = public_key
  22. ts = str(time.time())
  23. to_hash = ts + private_key + public_key
  24. hasher = hashlib.md5()
  25. hasher.update(to_hash.encode())
  26. digest = hasher.hexdigest()
  27. params["ts"] = ts
  28. params["hash"] = digest
  29. # Perform the request
  30. params["name"] = name
  31. url = base_url + "/characters"
  32. response = requests.get(url, params=params)
  33. status_code = response.status_code
  34. if status_code != 200:
  35. sys.exit("got status: " + str(status_code))
  36. body = response.json()
  37. description = body["data"]["results"][0]["description"]
  38. # Print the description
  39. terminal_size = shutil.get_terminal_size()
  40. columns = terminal_size.columns
  41. print("\n".join(textwrap.wrap(description, width=columns)))
  42. # Print attribution (comply with API agreement)
  43. print("---")
  44. print(body["attributionText"])
  45. if __name__ == "__main__":
  46. main()