Skip to content

Commit

Permalink
v2.1
Browse files Browse the repository at this point in the history
* Using new ygoprodeck domain
* Download delay (waiting time between each download) increased from 0.1s to 0.5s
* Refactored a lot of the code, should work the same though
  • Loading branch information
NiiMiyo committed Nov 18, 2022
1 parent ad23e5c commit 14a21b6
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 153 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ workpath_pyinstaller/

hd_cards_downloader_tracker
hd_fields_downloader_tracker
HdDownloader.LICENSE.txt
19 changes: 0 additions & 19 deletions HdDownloader.LICENSE.txt

This file was deleted.

19 changes: 12 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
FILENAME = EDOPro-HD-Downloader
LICENSE = HdDownloader.LICENSE.txt
DISTPATH = bin
WORKPATH = workpath_pyinstaller
ZIP_WIN = windows.zip
FILENAME = EDOPro-HD-Downloader
LICENSE = LICENSE
LICENSE_BIN = HdDownloader.LICENSE.txt
DISTPATH = bin
WORKPATH = workpath_pyinstaller
ZIP_WIN = windows.zip

build:
pyinstaller main.py -y --distpath "$(DISTPATH)" -F --specpath "$(DISTPATH)" -n "$(FILENAME)" -c --clean --workpath "$(WORKPATH)"
cp "$(LICENSE)" "$(DISTPATH)/$(LICENSE)"
cp "$(LICENSE)" "$(DISTPATH)/$(LICENSE_BIN)"

rm -rf "$(WORKPATH)"

7z-win: build
cd "$(DISTPATH)" && 7z a "$(ZIP_WIN)" "$(FILENAME).exe" "$(LICENSE)"
cd "$(DISTPATH)" && 7z a "$(ZIP_WIN)" "$(FILENAME).exe" "$(LICENSE_BIN)"

clean:
rm -rf "hd_cards_downloader_tracker"
rm -rf "hd_fields_downloader_tracker"
52 changes: 0 additions & 52 deletions apiaccess.py

This file was deleted.

4 changes: 2 additions & 2 deletions commands/cmd_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def __cmd_all(_: str) -> CommandReturn:
return allcards.action(_) + allfields.action(_) # type: ignore


CommandHandler.add_command(DownloaderCommand(
COMMAND_ALL = DownloaderCommand(
name="all",
help_text="downloads all cards images and all fields artworks",
action=__cmd_all
))
)
7 changes: 3 additions & 4 deletions commands/cmd_allcards.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from command_handler import CommandHandler
from commands.typing import CommandReturn, DownloadCard, DownloaderCommand
from apiaccess import get_all_cards
from web_access.ygoprodeck_api import get_all_cards


def __cmd_all_cards_action(_: str) -> CommandReturn:
Expand All @@ -10,8 +9,8 @@ def __cmd_all_cards_action(_: str) -> CommandReturn:
]


CommandHandler.add_command(DownloaderCommand(
COMMAND_ALLCARDS = DownloaderCommand(
name="allcards",
help_text="downloads all cards",
action=__cmd_all_cards_action
))
)
8 changes: 3 additions & 5 deletions commands/cmd_allfields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from command_handler import CommandHandler
from commands.typing import DownloadCard, DownloaderCommand, CommandReturn
from apiaccess import get_all_fields

from web_access.ygoprodeck_api import get_all_fields

def __cmd_all_fields_action(_: str) -> CommandReturn:
return [
Expand All @@ -10,8 +8,8 @@ def __cmd_all_fields_action(_: str) -> CommandReturn:
]


CommandHandler.add_command(DownloaderCommand(
COMMAND_ALLFIELDS = DownloaderCommand(
name="allfields",
help_text="downloads all fields artworks",
action=__cmd_all_fields_action
))
)
5 changes: 2 additions & 3 deletions commands/cmd_exit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from command_handler import CommandHandler
from commands.typing import CommandReturn, DownloaderCommand


Expand All @@ -7,8 +6,8 @@ def __cmd_exit_action(_: str) -> CommandReturn:
exit(0)


CommandHandler.add_command(DownloaderCommand(
COMMAND_EXIT = DownloaderCommand(
name="exit",
help_text="closes the program",
action=__cmd_exit_action
))
)
5 changes: 2 additions & 3 deletions commands/cmd_force.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from command_handler import CommandHandler
from commands.typing import CommandReturn, DownloadCard, DownloaderCommand
from commands.utils import get_args
from input_handler import handle_input
Expand All @@ -14,9 +13,9 @@ def __cmd_force_action(user_input: str) -> CommandReturn:
for c in cards
]

CommandHandler.add_command(DownloaderCommand(
COMMAND_FORCE = DownloaderCommand(
name="force",
shown_name="force <input>",
help_text="executes <input> ignoring trackers (example: /force /allcards)",
action=__cmd_force_action
))
)
4 changes: 2 additions & 2 deletions commands/cmd_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def __cmd_help_action(_: str) -> CommandReturn:
return []


CommandHandler.add_command(DownloaderCommand(
COMMAND_HELP = DownloaderCommand(
name="help",
help_text="see this text",
action=__cmd_help_action
))
)
23 changes: 15 additions & 8 deletions commands/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Yes, this is correct
# I'm sorry
from command_handler import CommandHandler
from commands.cmd_allcards import COMMAND_ALLCARDS
from commands.cmd_all import COMMAND_ALL
from commands.cmd_allfields import COMMAND_ALLFIELDS
from commands.cmd_exit import COMMAND_EXIT
from commands.cmd_force import COMMAND_FORCE
from commands.cmd_help import COMMAND_HELP


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 _
CommandHandler.add_command(COMMAND_ALL)
CommandHandler.add_command(COMMAND_ALLCARDS)
CommandHandler.add_command(COMMAND_ALLFIELDS)
CommandHandler.add_command(COMMAND_EXIT)
CommandHandler.add_command(COMMAND_FORCE)
CommandHandler.add_command(COMMAND_HELP)
28 changes: 28 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DOWNLOADER_VERSION = "2.1"
"""Program version"""

REQUEST_HEADERS = {
"User-Agent": f"NiiMiyo-EDOPro-HD-Downloader/{DOWNLOADER_VERSION}"
}
"""Header to be used in an HTTP request"""

INPUT_STRING = "Insert deck name (without .ydk) or command: "
"""String that appears at user input"""

YGOPRODECK_CARDS_URL = "https://db.ygoprodeck.com/api/v7/cardinfo.php"
"""Base API URL for YGOProDeck"""

IMAGES_BASE_URL = "https://images.ygoprodeck.com/images/cards"
"""Base URL for images"""

CARD_CACHE_PATH = "./hd_cards_downloader_tracker"
"""Path to the cards cache file"""

FIELD_CACHE_PATH = "./hd_fields_downloader_tracker"
"""Path to the fields cache file"""

SETUP_CREATION_FILES = (CARD_CACHE_PATH, FIELD_CACHE_PATH)
"""Files needed on setup"""

SETUP_CREATION_FOLDERS = ("pics", "pics/field")
"""Folders needed on setup"""
26 changes: 0 additions & 26 deletions downloader.py

This file was deleted.

21 changes: 10 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
from time import sleep
from traceback import print_exc
from commands.setup import setup_commands
from constants import DOWNLOADER_VERSION, INPUT_STRING, SETUP_CREATION_FILES

from input_handler import handle_input
from commands.typing import DownloadCard
from downloader import download_image
from tracker import (already_downloaded, card_cache_path, field_cache_path,
mark_as_downloaded)
from web_access.downloader import download_image
from tracker import (already_downloaded, mark_as_downloaded)

# String that appears at user input
INPUT_STRING = "Insert deck name (without .ydk) or command: "

def initialize():
"""Creates tracker files if they do not exist, setups all commands and
introduces the program
"""

global card_cache_path, field_cache_path
for i in card_cache_path, field_cache_path:
if not exists(i):
open(i, "w+").close()
for f in SETUP_CREATION_FILES:
if not exists(f):
with open(f, "w+"):
# I only need that the files exist
pass

setup_commands()

print("\n".join([
"EDOPro HD Downloader",
f"EDOPro HD Downloader v{DOWNLOADER_VERSION}",
"Created by Nii Miyo",
"Type \"/help\" for help"
]))
Expand All @@ -37,7 +36,7 @@ def to_download(card: DownloadCard):
if (card.force) or (not already_downloaded(card)):
success = download_image(card)
if success: mark_as_downloaded(card)
sleep(.1)
sleep(.5)


def main():
Expand Down
18 changes: 7 additions & 11 deletions tracker.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from commands.typing import DownloadCard
from constants import CARD_CACHE_PATH, FIELD_CACHE_PATH


card_cache_path = "./hd_cards_downloader_tracker"
field_cache_path = "./hd_fields_downloader_tracker"

def __get_cached(is_artwork: bool):
"""Reads tracker file and returns a list of ids
that already were downloaded"""

cache = field_cache_path if is_artwork else card_cache_path
cache_file = open(cache, mode="r+", encoding="utf8")
cards = [c.strip() for c in cache_file.readlines()]
cache_file.close()
cache = FIELD_CACHE_PATH if is_artwork else CARD_CACHE_PATH
with open(cache, mode="r+", encoding="utf8") as cache_file:
cards = [c.strip() for c in cache_file.readlines()]
return cards

def already_downloaded(card: DownloadCard):
Expand All @@ -24,8 +21,7 @@ def already_downloaded(card: DownloadCard):
def mark_as_downloaded(card: DownloadCard):
"""Opens tracker file to add an id to the downloaded list"""

cache = card_cache_path if not card.artwork else field_cache_path
cache = FIELD_CACHE_PATH if card.artwork else CARD_CACHE_PATH

cache_file = open(cache, mode="a+", encoding="utf8")
cache_file.write(f"{card.card_id}\n")
cache_file.close()
with open(cache, mode="a+", encoding="utf8") as cache_file:
cache_file.write(f"{card.card_id}\n")
30 changes: 30 additions & 0 deletions web_access/downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import requests
from os.path import join

from commands.typing import DownloadCard
from constants import IMAGES_BASE_URL

def download_image(card: DownloadCard) -> bool:
"""
Downloads the card image or artwork and puts in the specified folder.
Returns `True` if downloads successfully, otherwise returns `False`.
"""

url = IMAGES_BASE_URL
store_at = "./pics/"

if card.artwork:
url += "_cropped"
store_at += "field/"
url += f"/{card.card_id}.jpg"

file_path = join(store_at, f"{card.card_id}.jpg")
try:
res = requests.get(url)
with open(file_path, 'wb+') as f:
f.write(res.content)
return True
except Exception as e:
print(f"Error downloading '{card.card_id}': {type(e).__name__}\n{e}")
return False
Loading

0 comments on commit 14a21b6

Please sign in to comment.