Browse Source

better skeleton

master
Dimitri Merejkowsky 5 years ago
parent
commit
4a85c25f51
2 changed files with 27 additions and 5 deletions
  1. +1
    -1
      marvel/.gitignore
  2. +26
    -4
      marvel/hello.py

+ 1
- 1
marvel/.gitignore View File

@@ -1 +1 @@
api-keys
api-keys.txt

+ 26
- 4
marvel/hello.py View File

@@ -1,17 +1,23 @@
import hashlib import hashlib
import requests import requests
import shutil
import time import time
import textwrap




def main(): def main():
with open("api-keys", "r") as file:
# Read api keys from the text file
with open("api-keys.txt", "r") as file:
lines = file.readlines() lines = file.readlines()
assert len(lines) == 2, "Incorrect api-keys file" assert len(lines) == 2, "Incorrect api-keys file"
public_key = lines[0].strip() public_key = lines[0].strip()
private_key = lines[1].strip() private_key = lines[1].strip()




url = "http://gateway.marvel.com/v1/public/comics"
# base url
base_url = "http://gateway.marvel.com/v1/public"

# Authorizsation stuff
params = dict() params = dict()
params["apikey"] = public_key params["apikey"] = public_key
ts = str(time.time()) ts = str(time.time())
@@ -21,9 +27,25 @@ def main():
digest = hasher.hexdigest() digest = hasher.hexdigest()
params["ts"] = ts params["ts"] = ts
params["hash"] = digest params["hash"] = digest


# Other parameters:
name = "Spider-Man"
params["name"] = name


# Perform the request
url = base_url + "/characters"
response = requests.get(url, params=params) response = requests.get(url, params=params)
print(response.status_code)
print(response.text)
status_code = response.status_code
assert status_code == 200, "got status: " + str(status_code)
body = response.json()
description = body["data"]["results"][0]["description"]

# Print the description
terminal_size = shutil.get_terminal_size()
columns = terminal_size.columns
print("\n".join(textwrap.wrap(description, width=columns)))




if __name__ == "__main__": if __name__ == "__main__":