From f1b4b5a2057af189af4a0be8ba4213e62cc680e2 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 14:49:35 +0100 Subject: [PATCH 1/9] can now make a valid request --- main.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/main.py b/main.py index e69de29..ed1aec9 100644 --- a/main.py +++ b/main.py @@ -0,0 +1,27 @@ +import datetime +from hashlib import md5 +from os import getenv +from time import time_ns + +from requests import get +from dotenv import load_dotenv + +load_dotenv() + +public_api_key = getenv("MARVEL_PUBLIC_API_KEY") +private_api_key = getenv("MARVEL_PRIVATE_API_KEY") + +URL = "https://gateway.marvel.com:443/v1/public/characters/1" + +# name = input("What Marvel character? ") + +timestamp = str(time_ns()) +hash = md5(f"{timestamp}{private_api_key}{public_api_key}".encode("utf-8")).hexdigest() +params = { + 'apikey': public_api_key, + 'ts': timestamp, + 'hash': hash +} + +res = get(URL, params=params).json() +print(res) From 7b05b821adb64eff76356bdd4c31316a7f5d37ba Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 14:50:50 +0100 Subject: [PATCH 2/9] stop shadowing --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index ed1aec9..074aa92 100644 --- a/main.py +++ b/main.py @@ -16,11 +16,11 @@ # name = input("What Marvel character? ") timestamp = str(time_ns()) -hash = md5(f"{timestamp}{private_api_key}{public_api_key}".encode("utf-8")).hexdigest() +key_hash = md5(f"{timestamp}{private_api_key}{public_api_key}".encode("utf-8")).hexdigest() params = { 'apikey': public_api_key, 'ts': timestamp, - 'hash': hash + 'hash': key_hash } res = get(URL, params=params).json() From fb079a10d087bf80ed7d279280500d6ebc1d4d0f Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:04:04 +0100 Subject: [PATCH 3/9] can now lookup a character --- main.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 074aa92..c6e471c 100644 --- a/main.py +++ b/main.py @@ -8,20 +8,30 @@ load_dotenv() -public_api_key = getenv("MARVEL_PUBLIC_API_KEY") -private_api_key = getenv("MARVEL_PRIVATE_API_KEY") +PUBLIC_API_KEY = getenv("MARVEL_PUBLIC_API_KEY") +PRIVATE_API_KEY = getenv("MARVEL_PRIVATE_API_KEY") -URL = "https://gateway.marvel.com:443/v1/public/characters/1" - -# name = input("What Marvel character? ") +SEARCH_URL = "https://gateway.marvel.com:443/v1/public/characters" +DETAILS_URL = "https://gateway.marvel.com:443/v1/public/characters/{id}" timestamp = str(time_ns()) -key_hash = md5(f"{timestamp}{private_api_key}{public_api_key}".encode("utf-8")).hexdigest() -params = { - 'apikey': public_api_key, +key_hash = md5(f"{timestamp}{PRIVATE_API_KEY}{PUBLIC_API_KEY}".encode("utf-8")).hexdigest() +default_params = { + 'apikey': PUBLIC_API_KEY, 'ts': timestamp, 'hash': key_hash } -res = get(URL, params=params).json() -print(res) +# name = input("What Marvel character? ") +name = "Spider-Man" + +print("trying to find that character . . .") + +search_params = { + **default_params, + 'nameStartsWith': name +} + +res = get(SEARCH_URL, search_params).json() + +print(res['data']['results'][0]['name']) From 9aa6ef5dbe00c1771b7a771bd53edc2b61d8a38b Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:14:52 +0100 Subject: [PATCH 4/9] can now search for a character --- main.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index c6e471c..704245f 100644 --- a/main.py +++ b/main.py @@ -22,8 +22,7 @@ 'hash': key_hash } -# name = input("What Marvel character? ") -name = "Spider-Man" +name = input("What Marvel character? ") print("trying to find that character . . .") @@ -34,4 +33,29 @@ res = get(SEARCH_URL, search_params).json() -print(res['data']['results'][0]['name']) +# refer to https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 for data shape +characters = res['data']['results'] + +if len(characters) == 0: + print("Couldn't find any characters by this name...") +else: + i = 0 + IDs = [] + print(f"Please select a character)") + for character in characters: + i += 1 + print(f"- {i}:{character['name']}") + IDs.append(character['id']) + + index = int(input(f"Which ID? (1-{len(characters)}) ")) + id = IDs[index] + + print("Retrieving details . . .") + + res = get(DETAILS_URL.format(id=id), params=default_params).json() + + character = res['data']['results'][0] # should only be one returned + + print(f"Name: {character['name']}") + print(f"Bio: {character['description']}") + From 9511e855440ab9985d0d1bed363b133ec875a380 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:18:30 +0100 Subject: [PATCH 5/9] program now loops --- main.py | 67 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index 704245f..252cefd 100644 --- a/main.py +++ b/main.py @@ -14,48 +14,49 @@ SEARCH_URL = "https://gateway.marvel.com:443/v1/public/characters" DETAILS_URL = "https://gateway.marvel.com:443/v1/public/characters/{id}" -timestamp = str(time_ns()) -key_hash = md5(f"{timestamp}{PRIVATE_API_KEY}{PUBLIC_API_KEY}".encode("utf-8")).hexdigest() -default_params = { - 'apikey': PUBLIC_API_KEY, - 'ts': timestamp, - 'hash': key_hash -} +while True: + timestamp = str(time_ns()) + key_hash = md5(f"{timestamp}{PRIVATE_API_KEY}{PUBLIC_API_KEY}".encode("utf-8")).hexdigest() + default_params = { + 'apikey': PUBLIC_API_KEY, + 'ts': timestamp, + 'hash': key_hash + } -name = input("What Marvel character? ") + name = input("What Marvel character? ") -print("trying to find that character . . .") + print("trying to find that character . . .") -search_params = { - **default_params, - 'nameStartsWith': name -} + search_params = { + **default_params, + 'nameStartsWith': name + } -res = get(SEARCH_URL, search_params).json() + res = get(SEARCH_URL, search_params).json() -# refer to https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 for data shape -characters = res['data']['results'] + # refer to https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 for data shape + characters = res['data']['results'] -if len(characters) == 0: - print("Couldn't find any characters by this name...") -else: - i = 0 - IDs = [] - print(f"Please select a character)") - for character in characters: - i += 1 - print(f"- {i}:{character['name']}") - IDs.append(character['id']) + if len(characters) == 0: + print("Couldn't find any characters by this name...") + else: + i = 0 + IDs = [] + print(f"Please select a character)") + for character in characters: + i += 1 + print(f"- {i}:{character['name']}") + IDs.append(character['id']) - index = int(input(f"Which ID? (1-{len(characters)}) ")) - id = IDs[index] + index = int(input(f"Which ID? (1-{len(characters)}) ")) - 1 + id = IDs[index] - print("Retrieving details . . .") + print("Retrieving details . . .") - res = get(DETAILS_URL.format(id=id), params=default_params).json() + res = get(DETAILS_URL.format(id=id), params=default_params).json() - character = res['data']['results'][0] # should only be one returned + character = res['data']['results'][0] # should only be one returned - print(f"Name: {character['name']}") - print(f"Bio: {character['description']}") + print(f"Name: {character['name']}") + print(f"Bio: {character['description']}") From 1206f2a49597a838750759f5172a131b146ac578 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:20:24 +0100 Subject: [PATCH 6/9] will not prompt if one result --- main.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 252cefd..3327fb1 100644 --- a/main.py +++ b/main.py @@ -41,19 +41,22 @@ print("Couldn't find any characters by this name...") else: i = 0 - IDs = [] - print(f"Please select a character)") - for character in characters: - i += 1 - print(f"- {i}:{character['name']}") - IDs.append(character['id']) - - index = int(input(f"Which ID? (1-{len(characters)}) ")) - 1 - id = IDs[index] + if len(characters) > 1: + IDs = [] + print(f"Please select a character)") + for character in characters: + i += 1 + print(f"- {i}:{character['name']}") + IDs.append(character['id']) + + index = int(input(f"Which ID? (1-{len(characters)}) ")) - 1 + ID = IDs[index] + else: + ID = characters[0]['id'] print("Retrieving details . . .") - res = get(DETAILS_URL.format(id=id), params=default_params).json() + res = get(DETAILS_URL.format(id=ID), params=default_params).json() character = res['data']['results'][0] # should only be one returned From 270ff6a28cdf6d5bee81c626c9c8695e6b67c828 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:20:45 +0100 Subject: [PATCH 7/9] consistent capitalisation --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 3327fb1..719cccd 100644 --- a/main.py +++ b/main.py @@ -25,7 +25,7 @@ name = input("What Marvel character? ") - print("trying to find that character . . .") + print("Trying to find that character . . .") search_params = { **default_params, From 7c62e8ed5888a12da7a3278285335f1366d5a47c Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:22:45 +0100 Subject: [PATCH 8/9] space between number and result --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 719cccd..7977701 100644 --- a/main.py +++ b/main.py @@ -46,7 +46,7 @@ print(f"Please select a character)") for character in characters: i += 1 - print(f"- {i}:{character['name']}") + print(f"- {i}: {character['name']}") IDs.append(character['id']) index = int(input(f"Which ID? (1-{len(characters)}) ")) - 1 From d86568cf155237a2a06f0b712e82dca7126383ab Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 8 Jul 2022 15:28:52 +0100 Subject: [PATCH 9/9] pagination --- main.py | 66 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 7977701..7c41d65 100644 --- a/main.py +++ b/main.py @@ -14,46 +14,74 @@ SEARCH_URL = "https://gateway.marvel.com:443/v1/public/characters" DETAILS_URL = "https://gateway.marvel.com:443/v1/public/characters/{id}" +PAGE_SIZE = 20 + while True: timestamp = str(time_ns()) key_hash = md5(f"{timestamp}{PRIVATE_API_KEY}{PUBLIC_API_KEY}".encode("utf-8")).hexdigest() default_params = { 'apikey': PUBLIC_API_KEY, 'ts': timestamp, - 'hash': key_hash + 'hash': key_hash, } name = input("What Marvel character? ") - print("Trying to find that character . . .") + keep_searching = True + ID = None + offset = 0 - search_params = { - **default_params, - 'nameStartsWith': name - } + while keep_searching: + + print("Trying to find that character . . .") + + search_params = { + **default_params, + 'nameStartsWith': name, + 'limit': PAGE_SIZE, + 'offset': offset + } + + res = get(SEARCH_URL, search_params).json() + + # refer to https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 for data shape - res = get(SEARCH_URL, search_params).json() + data = res['data'] + total = data['total'] + offset = data['offset'] - # refer to https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0 for data shape - characters = res['data']['results'] + characters = data['results'] - if len(characters) == 0: - print("Couldn't find any characters by this name...") - else: - i = 0 - if len(characters) > 1: + if total == 0: + print("Couldn't find any characters by this name...") + keep_searching = False + elif total == 1: + ID = characters[0]['id'] + keep_searching = False + else: IDs = [] + i = 0 print(f"Please select a character)") for character in characters: i += 1 print(f"- {i}: {character['name']}") IDs.append(character['id']) - index = int(input(f"Which ID? (1-{len(characters)}) ")) - 1 - ID = IDs[index] - else: - ID = characters[0]['id'] - + choice = input(f"Which ID? (1-{len(characters)}), or 'n' for next page ") + + if choice == "n": + if offset + PAGE_SIZE >= total: + print("There are no more pages...") + keep_searching = False + else: + offset += PAGE_SIZE + else: + index = int(choice) - 1 + ID = IDs[index] + keep_searching = False + found_a_character = True + + if ID: # if any previous branch resulted in a result print("Retrieving details . . .") res = get(DETAILS_URL.format(id=ID), params=default_params).json()