Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NiiMiyo committed Apr 13, 2022
1 parent b1c5494 commit 6f95a46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
2 changes: 1 addition & 1 deletion apiaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_all_cards() -> list[int]:
request = __request.Request(api_url, headers=__headers)
response = __request.urlopen(request)
except Exception as e:
print(e)
print(f"Error fetching db.ygoprodeck.com: {e}")
return list()
else:
return __get_ids_from_api_response(response)
Expand Down
2 changes: 1 addition & 1 deletion downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def download_image(card_id: int, is_artwork: bool = False):
return True

except Exception as e:
print(e)
print(f"Error downloading '{card_id}': {e}")
return False
50 changes: 19 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from os.path import exists
from time import sleep
from traceback import format_exc as __format_exc
from traceback import print_exc
from typing import Optional

from apiaccess import get_all_cards, get_all_fields
Expand Down Expand Up @@ -53,7 +53,7 @@ def handle_input(_input: str) -> tuple[Optional[list[int]], bool, bool]:
"/force <input> - executes <input> ignoring trackers",
"/exit - closes the program",
"/help - see this text",
]))
]), end="")

# Force command
elif _input.startswith("/force "):
Expand Down Expand Up @@ -87,43 +87,31 @@ def main():

try:
while True:
user_input = input(INPUT_STRING)
cards, is_artwork, force = handle_input(user_input)
cards, is_artwork, force = handle_input( input(INPUT_STRING) )

# If found what to download
if cards is not None:
total_cards = len(cards)
# If couldn't find what to download
if cards is None:
print("Deck or command not found.")
continue

# For each card, download
for i in range(total_cards):
c = cards[i]
to_download(c, is_artwork, force)
total_cards = len(cards)

# Prints progress
print(
f"Downloaded {i+1}/{total_cards} -",
f"{(((i+1)/total_cards) * 100):.2f}" + "%",
end="\r"
)
# For each card, download
for index, card_id in enumerate(cards, 1):
to_download(card_id, is_artwork, force)

# Help command had 2 newlines at end
if total_cards > 0:
print()
# Prints progress
raw_progress = f"{index}/{total_cards}"
percentage = f"{((index * 100) / total_cards):.2f}%"
print(f"Downloaded {raw_progress} - {percentage}", end="\r")

# If couldn't find what to download
else:
print("Deck or command not found.")
print()
print("\n")

# In case of interrupting the program with Ctrl+C
except KeyboardInterrupt:
print("\n\nForcing program interruption...")
except KeyboardInterrupt: print("\n\nForcing program interruption...")

# In case of a not expected exception
except Exception:
print(__format_exc())
exit()
except Exception: print_exc()


if __name__ == "__main__":
main()
if __name__ == "__main__": main()

0 comments on commit 6f95a46

Please sign in to comment.