@@ -1,61 +0,0 @@ | |||
import hashlib | |||
import requests | |||
import shutil | |||
import time | |||
import textwrap | |||
import sys | |||
def main(): | |||
# Get name from command line | |||
name = sys.argv[1] | |||
# Read api keys from the text file | |||
with open("api-keys.txt", "r") as file: | |||
lines = file.readlines() | |||
if len(lines) != 2: | |||
sys.exit("Incorrect api-keys file") | |||
public_key = lines[0].strip() | |||
private_key = lines[1].strip() | |||
# base url | |||
base_url = "http://gateway.marvel.com/v1/public" | |||
# Authorization stuff | |||
params = dict() | |||
params["apikey"] = public_key | |||
ts = str(time.time()) | |||
to_hash = ts + private_key + public_key | |||
hasher = hashlib.md5() | |||
hasher.update(to_hash.encode()) | |||
digest = hasher.hexdigest() | |||
params["ts"] = ts | |||
params["hash"] = digest | |||
# Perform the request | |||
params["name"] = name | |||
url = base_url + "/characters" | |||
response = requests.get(url, params=params) | |||
status_code = response.status_code | |||
if status_code != 200: | |||
sys.exit("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))) | |||
# Print attribution (comply with API agreement) | |||
print("---") | |||
print(body["attributionText"]) | |||
if __name__ == "__main__": | |||
main() |
@@ -1,84 +0,0 @@ | |||
import hashlib | |||
import requests | |||
import shutil | |||
import time | |||
import textwrap | |||
import sys | |||
class Authorization: | |||
def __init__(self): | |||
self.public_key = None | |||
self.private_key = None | |||
def get_keys(self): | |||
with open("api-keys.txt", "r") as file: | |||
lines = file.readlines() | |||
if len(lines) != 2: | |||
sys.exit("Incorrect api-keys file") | |||
self.public_key = lines[0].strip() | |||
self.private_key = lines[1].strip() | |||
def generate_params(self): | |||
params = dict() | |||
params["apikey"] = self.public_key | |||
ts = str(time.time()) | |||
to_hash = ts + self.private_key + self.public_key | |||
hasher = hashlib.md5() | |||
hasher.update(to_hash.encode()) | |||
digest = hasher.hexdigest() | |||
params["ts"] = ts | |||
params["hash"] = digest | |||
return params | |||
class Client: | |||
def __init__(self, base_url, auth): | |||
self.base_url = base_url | |||
self.auth = auth | |||
def get_character_description(self,name): | |||
params = self.auth.generate_params() | |||
params["name"] = name | |||
response = requests.get(self.base_url+"/characters", params=params) | |||
status_code = response.status_code | |||
if status_code != 200: | |||
sys.exit("got status: " + str(status_code)) | |||
body = response.json() | |||
description = body["data"]["results"][0]["description"] | |||
attribution = body["attributionText"] | |||
return (description, attribution) | |||
class Display: | |||
def __init__(self, width=80): | |||
self.width = width | |||
def display(self, text): | |||
print("\n".join(textwrap.wrap(text, width=self.width))) | |||
def main(): | |||
name = sys.argv[1] | |||
base_url = "http://gateway.marvel.com/v1/public" | |||
auth = Authorization() | |||
auth.get_keys() | |||
client = Client(base_url, auth) | |||
description, attribution = client.get_character_description(name) | |||
terminal_size = shutil.get_terminal_size() | |||
columns = terminal_size.columns | |||
terminal = Display(width=columns) | |||
terminal.display(description) | |||
terminal.display("---") | |||
terminal.display(attribution) | |||
if __name__ == "__main__": | |||
main() |
@@ -1,80 +0,0 @@ | |||
import hashlib | |||
import requests | |||
import shutil | |||
import time | |||
import textwrap | |||
import sys | |||
class Authorization: | |||
def __init__(self, filename): | |||
with open(filename, "r") as file: | |||
lines = file.readlines() | |||
if len(lines) != 2: | |||
sys.exit("Incorrect api-keys file") | |||
self.public_key = lines[0].strip() | |||
self.private_key = lines[1].strip() | |||
def generate_params(self): | |||
params = dict() | |||
params["apikey"] = self.public_key | |||
ts = str(time.time()) | |||
to_hash = ts + self.private_key + self.public_key | |||
hasher = hashlib.md5() | |||
hasher.update(to_hash.encode()) | |||
digest = hasher.hexdigest() | |||
params["ts"] = ts | |||
params["hash"] = digest | |||
return params | |||
class Client: | |||
base_url = "http://gateway.marvel.com/v1/public" | |||
def __init__(self, auth): | |||
self.auth = auth | |||
def get_character_description(self, name): | |||
params = self.auth.generate_params() | |||
params["name"] = name | |||
url = Client.base_url + "/characters" | |||
response = requests.get(url, params=params) | |||
status_code = response.status_code | |||
if status_code != 200: | |||
sys.exit("got status: " + str(status_code)) | |||
body = response.json() | |||
description = body["data"]["results"][0]["description"] | |||
attribution = body["attributionText"] | |||
return (description, attribution) | |||
class Display: | |||
def __init__(self, width=80): | |||
self.width = width | |||
def display(self, text): | |||
print("\n".join(textwrap.wrap(text, width=self.width))) | |||
def main(): | |||
name = sys.argv[1] | |||
auth = Authorization("api-keys.txt") | |||
client = Client(auth) | |||
description, attribution = client.get_character_description(name) | |||
terminal_size = shutil.get_terminal_size() | |||
columns = terminal_size.columns | |||
terminal = Display(width=columns) | |||
terminal.display(description) | |||
terminal.display("---") | |||
terminal.display(attribution) | |||
if __name__ == "__main__": | |||
main() |
@@ -1,108 +0,0 @@ | |||
import hashlib | |||
import requests | |||
import shutil | |||
import time | |||
import textwrap | |||
import sys | |||
class Authorization: | |||
def __init__(self, filename): | |||
with open(filename, "r") as file: | |||
lines = file.readlines() | |||
if len(lines) != 2: | |||
sys.exit("Incorrect api-keys file") | |||
self.public_key = lines[0].strip() | |||
self.private_key = lines[1].strip() | |||
def generate_params(self): | |||
params = dict() | |||
params["apikey"] = self.public_key | |||
ts = str(time.time()) | |||
to_hash = ts + self.private_key + self.public_key | |||
hasher = hashlib.md5() | |||
hasher.update(to_hash.encode()) | |||
digest = hasher.hexdigest() | |||
params["ts"] = ts | |||
params["hash"] = digest | |||
return params | |||
class CharacterDescriptionGetter: | |||
base_url = "http://gateway.marvel.com/v1/public" | |||
def __init__(self, auth): | |||
self.auth = auth | |||
def get_character_description(self, name): | |||
params = self.auth.generate_params() | |||
params["name"] = name | |||
url = CharacterDescriptionGetter.base_url + "/characters" | |||
response = requests.get(url, params=params) | |||
status_code = response.status_code | |||
if status_code != 200: | |||
sys.exit("got status: " + str(status_code)) | |||
body = response.json() | |||
description = body["data"]["results"][0]["description"] | |||
attribution = body["attributionText"] | |||
return (description, attribution) | |||
class CreatorNumberOfSeriesGetter: | |||
base_url = "http://gateway.marvel.com/v1/public" | |||
def __init__(self, auth): | |||
self.auth = auth | |||
def get_number_of_series(self, first_name, last_name): | |||
params = self.auth.generate_params() | |||
params["firstName"] = first_name | |||
params["lastName"] = last_name | |||
url = CreatorNumberOfSeriesGetter.base_url + "/creators" | |||
response = requests.get(url, params=params) | |||
status_code = response.status_code | |||
if status_code != 200: | |||
sys.exit("got status: " + str(status_code)) | |||
body = response.json() | |||
result = body["data"]["results"][0] | |||
attribution = body["attributionText"] | |||
return result["series"]["available"], attribution | |||
class Display: | |||
def __init__(self, width=80): | |||
self.width = width | |||
def display(self, text): | |||
print("\n".join(textwrap.wrap(text, width=self.width))) | |||
def main(): | |||
auth = Authorization("api-keys.txt") | |||
query = sys.argv[1] | |||
if query == "character-description": | |||
name = sys.argv[2] | |||
character_description_getter = CharacterDescriptionGetter(auth) | |||
description, attribution = character_description_getter.get_character_description(name) | |||
text = description | |||
elif query == "creator-number-of-series": | |||
first_name = sys.argv[2] | |||
last_name = sys.argv[3] | |||
creator_number_of_series_getter = CreatorNumberOfSeriesGetter(auth) | |||
result, attribution = creator_number_of_series_getter.get_number_of_series(first_name, last_name) | |||
text = first_name + " " + last_name + " worked on " + str(result) + " series" | |||
terminal_size = shutil.get_terminal_size() | |||
columns = terminal_size.columns | |||
terminal = Display(width=columns) | |||
terminal.display(text) | |||
terminal.display("---") | |||
terminal.display(attribution) | |||
if __name__ == "__main__": | |||
main() |
@@ -1,134 +0,0 @@ | |||
# WARNING: Don't read this unless you want to get spoiled! | |||
import abc | |||
import hashlib | |||
import requests | |||
import shutil | |||
import time | |||
import textwrap | |||
import sys | |||
class Authorization: | |||
def __init__(self, filename): | |||
with open(filename, "r") as file: | |||
lines = file.readlines() | |||
if len(lines) != 2: | |||
sys.exit("Incorrect api-keys file") | |||
self.public_key = lines[0].strip() | |||
self.private_key = lines[1].strip() | |||
def generate_params(self): | |||
params = dict() | |||
params["apikey"] = self.public_key | |||
ts = str(time.time()) | |||
to_hash = ts + self.private_key + self.public_key | |||
hasher = hashlib.md5() | |||
hasher.update(to_hash.encode()) | |||
digest = hasher.hexdigest() | |||
params["ts"] = ts | |||
params["hash"] = digest | |||
return params | |||
class Client(): | |||
base_url = "http://gateway.marvel.com/v1/public" | |||
def __init__(self, auth): | |||
self.auth = auth | |||
def make_request(self, query): | |||
params = self.auth.generate_params() | |||
params.update(query.params()) | |||
url = Client.base_url + query.path | |||
response = requests.get(url, params=params) | |||
status_code = response.status_code | |||
if status_code != 200: | |||
sys.exit("got status: " + str(status_code)) | |||
body = response.json() | |||
result = query.extract(body) | |||
return (query.to_text(result), body["attributionText"]) | |||
class Query(metaclass=abc.ABCMeta): | |||
@abc.abstractmethod | |||
def params(self): | |||
pass | |||
@abc.abstractmethod | |||
def path(self): | |||
pass | |||
@abc.abstractmethod | |||
def extract(self, body): | |||
pass | |||
def to_text(self, result): | |||
return result | |||
class CharacterDescription(Query): | |||
path = "/characters" | |||
def __init__(self, name): | |||
self.name = name | |||
def params(self): | |||
return { "name": self.name } | |||
def extract(self, body): | |||
return body["data"]["results"][0]["description"] | |||
class CreatorNumberOfSeries(Query): | |||
path = "/creators" | |||
def __init__(self, first_name, last_name): | |||
self.first_name = first_name | |||
self.last_name = last_name | |||
def params(self): | |||
return { "firstName": self.first_name, "lastName": self.last_name } | |||
def extract(self, body): | |||
return body["data"]["results"][0]["series"]["available"] | |||
def to_text(self, result): | |||
return f"{self.first_name} {self.last_name} worked on {result} series" | |||
class Display: | |||
def __init__(self, width=80): | |||
self.width = width | |||
def display(self, text): | |||
print("\n".join(textwrap.wrap(text, width=self.width))) | |||
def main(): | |||
query_type = sys.argv[1] | |||
if query_type == "character-description": | |||
name = sys.argv[2] | |||
query = CharacterDescription(name) | |||
elif query_type == "creator-number-of-series": | |||
first_name = sys.argv[2] | |||
last_name = sys.argv[3] | |||
query = CreatorNumberOfSeries(first_name, last_name) | |||
auth = Authorization("api-keys.txt") | |||
client = Client(auth) | |||
text, attribution = client.make_request(query) | |||
terminal_size = shutil.get_terminal_size() | |||
columns = terminal_size.columns | |||
terminal = Display(width=columns) | |||
terminal.display(text) | |||
terminal.display("---") | |||
terminal.display(attribution) | |||
if __name__ == "__main__": | |||
main() |