-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commands * using objects * DownloadCard.force is False by default * oopsie * some commands * something that doesn't work IDK * CommandHandler Finally a strategy that works * docstrings * makefile and readme fix * CommandHandler.commands from list to dict * /all
- Loading branch information
Showing
19 changed files
with
309 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ pics/ | |
build/ | ||
dist/ | ||
bin/ | ||
workpath_pyinstaller/ | ||
|
||
hd_cards_downloader_tracker | ||
hd_fields_downloader_tracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
FILENAME = EDOPro-HD-Downloader | ||
LICENSE = HdDownloader.LICENSE.txt | ||
DISTPATH = ./bin | ||
WORKPATH = ./workpath_pyinstaller | ||
|
||
build: | ||
pyinstaller main.py -y --distpath "$(DISTPATH)" -F --specpath "$(DISTPATH)" -n "$(FILENAME)" -c --clean | ||
cp $(LICENSE) $(DISTPATH)/$(LICENSE) | ||
pyinstaller main.py -y --distpath "$(DISTPATH)" -F --specpath "$(DISTPATH)" -n "$(FILENAME)" -c --clean --workpath "$(WORKPATH)" | ||
cp "$(LICENSE)" "$(DISTPATH)/$(LICENSE)" | ||
|
||
rm -rf build | ||
rm -rf "$(WORKPATH)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Optional | ||
from commands.typing import DownloaderCommand | ||
from commands.utils import get_first_word | ||
|
||
|
||
class CommandHandler: | ||
"""Class to manage the use of commands""" | ||
|
||
commands: dict[str, DownloaderCommand] = dict() | ||
"""Dict of all available commands (maps command name to command). | ||
If you add a command you should put it here using | ||
`CommandHandler.add_command` and adding the import on `commands/setup.py`. | ||
""" | ||
|
||
@staticmethod | ||
def find_command(user_input: str) -> Optional[DownloaderCommand]: | ||
"""Gets the `DownloaderCommand` that matches user_input. For example, | ||
`force` command for `"/force /allcards"` input. | ||
""" | ||
|
||
if not user_input: # If empty string or None | ||
return None | ||
|
||
command_used = get_first_word(user_input) | ||
if command_used.startswith("/"): | ||
return CommandHandler.commands.get(command_used[1:]) | ||
else: | ||
return None | ||
|
||
@staticmethod | ||
def add_command(command: DownloaderCommand): | ||
"""Adds a DownloaderCommand to be available to use on user input""" | ||
|
||
CommandHandler.commands[command.name] = command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from commands.typing import CommandReturn, DownloaderCommand | ||
from command_handler import CommandHandler | ||
|
||
|
||
def __cmd_all(_: str) -> CommandReturn: | ||
allcards = CommandHandler.commands.get("allcards") | ||
allfields = CommandHandler.commands.get("allfields") | ||
|
||
return allcards.action(_) + allfields.action(_) # type: ignore | ||
|
||
|
||
CommandHandler.add_command(DownloaderCommand( | ||
name="all", | ||
help_text="downloads all cards images and all fields artworks", | ||
action=__cmd_all | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from command_handler import CommandHandler | ||
from commands.typing import CommandReturn, DownloadCard, DownloaderCommand | ||
from apiaccess import get_all_cards | ||
|
||
|
||
def __cmd_all_cards_action(_: str) -> CommandReturn: | ||
return [ | ||
DownloadCard(c, False) | ||
for c in get_all_cards() | ||
] | ||
|
||
|
||
CommandHandler.add_command(DownloaderCommand( | ||
name="allcards", | ||
help_text="downloads all cards", | ||
action=__cmd_all_cards_action | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from command_handler import CommandHandler | ||
from commands.typing import DownloadCard, DownloaderCommand, CommandReturn | ||
from apiaccess import get_all_fields | ||
|
||
|
||
def __cmd_all_fields_action(_: str) -> CommandReturn: | ||
return [ | ||
DownloadCard(c, True) | ||
for c in get_all_fields() | ||
] | ||
|
||
|
||
CommandHandler.add_command(DownloaderCommand( | ||
name="allfields", | ||
help_text="downloads all fields artworks", | ||
action=__cmd_all_fields_action | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from command_handler import CommandHandler | ||
from commands.typing import CommandReturn, DownloaderCommand | ||
|
||
|
||
def __cmd_exit_action(_: str) -> CommandReturn: | ||
print("Bye bye <3") | ||
exit(0) | ||
|
||
|
||
CommandHandler.add_command(DownloaderCommand( | ||
name="exit", | ||
help_text="closes the program", | ||
action=__cmd_exit_action | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from command_handler import CommandHandler | ||
from commands.typing import CommandReturn, DownloadCard, DownloaderCommand | ||
from commands.utils import get_args | ||
from input_handler import handle_input | ||
|
||
def __cmd_force_action(user_input: str) -> CommandReturn: | ||
args = get_args(user_input) | ||
cards = handle_input(args) | ||
if cards is None: | ||
return None | ||
|
||
return [ | ||
DownloadCard(c.card_id, c.artwork, True) | ||
for c in cards | ||
] | ||
|
||
CommandHandler.add_command(DownloaderCommand( | ||
name="force", | ||
shown_name="force <input>", | ||
help_text="executes <input> ignoring trackers (example: /force /allcards)", | ||
action=__cmd_force_action | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from commands.typing import CommandReturn, DownloaderCommand | ||
from command_handler import CommandHandler | ||
|
||
|
||
def __cmd_help_action(_: str) -> CommandReturn: | ||
cmd_column_len = 0 | ||
lines: list[tuple[str, str]] = list() | ||
|
||
command_list = sorted( | ||
CommandHandler.commands.values(), | ||
key=lambda cmd: cmd.name | ||
) | ||
|
||
for cmd in command_list: | ||
sn = cmd.get_shown_name() | ||
|
||
lines.append((sn, cmd.help_text)) | ||
cmd_column_len = max(cmd_column_len, len(sn)) | ||
|
||
print("\n".join( | ||
f"/{sn.ljust(cmd_column_len)} - {ht}" | ||
for sn, ht in lines | ||
)) | ||
|
||
return [] | ||
|
||
|
||
CommandHandler.add_command(DownloaderCommand( | ||
name="help", | ||
help_text="see this text", | ||
action=__cmd_help_action | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Yes, this is correct | ||
# I'm sorry | ||
def setup_commands(): | ||
import commands.cmd_allcards as _ | ||
import commands.cmd_allfields as _ | ||
import commands.cmd_exit as _ | ||
import commands.cmd_force as _ | ||
import commands.cmd_help as _ | ||
import commands.cmd_all as _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Callable, NamedTuple, Optional | ||
|
||
|
||
class DownloadCard(NamedTuple): | ||
"""Represents a card to be downloaded""" | ||
|
||
card_id: int | ||
artwork: bool | ||
force: bool = False | ||
|
||
|
||
CommandReturn = Optional[list[DownloadCard]] | ||
"""Type a `CommandAction` should return""" | ||
|
||
CommandAction = Callable[[str], CommandReturn] | ||
"""Type a `DownloaderCommand.action` should be. | ||
Should only return `None` in case the command fails. If the command does not | ||
download cards, return an empty list. | ||
""" | ||
|
||
|
||
class DownloaderCommand(NamedTuple): | ||
name: str | ||
"""The name of the command, should be unique""" | ||
|
||
help_text: str | ||
"""Text the command shows on `/help`""" | ||
|
||
action: CommandAction | ||
"""Function that defines command execution""" | ||
|
||
shown_name: Optional[str] = None | ||
"""Name that will be shown on `/help`. If it's `None` then shows `name`""" | ||
|
||
def match_string(self) -> str: | ||
"""Returns `/command.name`""" | ||
return f"/{self.name}" | ||
|
||
def get_shown_name(self) -> str: | ||
"""Returns `command.shown_name` if it's not None. Otherwise returns | ||
`command.name` | ||
""" | ||
|
||
if self.shown_name is None: | ||
return self.name | ||
else: | ||
return self.shown_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def get_args(user_input: str) -> str: | ||
"""Receives an user input and returns everything after the first | ||
space character replacing double spaces with single spaces | ||
""" | ||
|
||
args = [ | ||
a for a in user_input.split(" ") | ||
if a | ||
] | ||
|
||
return " ".join(args[1:]) | ||
|
||
|
||
def get_first_word(user_input: str) -> str: | ||
"""Receives an user input and returns everything before the first | ||
space character | ||
""" | ||
|
||
if not user_input: | ||
return "" | ||
|
||
return user_input.split(" ")[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
from urllib import request as __request | ||
from os.path import join as __join | ||
|
||
def download_image(card_id: int, is_artwork: bool = False): | ||
from commands.typing import DownloadCard | ||
|
||
def download_image(card: DownloadCard): | ||
"""Downloads the card image or artwork and | ||
puts in the specified folder""" | ||
|
||
img_url = "https://storage.googleapis.com/ygoprodeck.com/pics" | ||
if not is_artwork: | ||
img_url += f"/{card_id}.jpg" | ||
if not card.artwork: | ||
img_url += f"/{card.card_id}.jpg" | ||
store_at = "./pics/" | ||
else: | ||
img_url += f"_artgame/{card_id}.jpg" | ||
img_url += f"_artgame/{card.card_id}.jpg" | ||
store_at = "./pics/field/" | ||
|
||
file_path = __join(store_at, f"{card_id}.jpg") | ||
file_path = __join(store_at, f"{card.card_id}.jpg") | ||
try: | ||
__request.urlretrieve(img_url, file_path) | ||
return True | ||
|
||
except Exception as e: | ||
print(f"Error downloading '{card_id}': {e}") | ||
print(f"Error downloading '{card.card_id}': {e}") | ||
return False |
Oops, something went wrong.