-
Notifications
You must be signed in to change notification settings - Fork 0
exercise #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
exercise #1
Changes from all commits
f1b4b5a
7b05b82
fb079a1
9aa6ef5
9511e85
1206f2a
270ff6a
7c62e8e
d86568c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| 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") | ||
|
Comment on lines
+11
to
+12
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of keeping your environment variables secure! Good DevOps practice! |
||
|
|
||
| SEARCH_URL = "https://gateway.marvel.com:443/v1/public/characters" | ||
| DETAILS_URL = "https://gateway.marvel.com:443/v1/public/characters/{id}" | ||
|
Comment on lines
+14
to
+15
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way you could have just added the |
||
|
|
||
| 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, | ||
| } | ||
|
|
||
| name = input("What Marvel character? ") | ||
|
|
||
| keep_searching = True | ||
| ID = None | ||
| offset = 0 | ||
|
|
||
| while keep_searching: | ||
|
Comment on lines
+19
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A nested while statement could be suggested to not be best practice as it can get messy (see comment below on nested if/else statements) |
||
|
|
||
| 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 | ||
|
|
||
| data = res['data'] | ||
| total = data['total'] | ||
| offset = data['offset'] | ||
|
|
||
| characters = data['results'] | ||
|
Comment on lines
+49
to
+53
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you thought about using .get() instead of []. This would just ensure that an error wouldn't be thrown if that specific section wasn't there. |
||
|
|
||
| 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']) | ||
|
|
||
| choice = input(f"Which ID? (1-{len(characters)}), or 'n' for next page ") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty cool functionality! Awesome job! |
||
|
|
||
| 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 | ||
|
Comment on lines
+55
to
+82
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point there is many nested if/else if statements, on top of the nested while statements, this makes following the logic difficult, especially to understand which branch you end up in. If there are nested if/else statements, then sometimes it is best to put everything into a function and use return statements. |
||
|
|
||
| if ID: # if any previous branch resulted in a result | ||
| 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']}") | ||
|
Comment on lines
+91
to
+92
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice functionality to print both the name and the description! |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the gap here? It would be good to keep all imports together unless there is some specific reason not to