Skip to content
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

Refactor and improve project structure #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*.log
/src/imageDownloader/downloads/*
/src/imageDownloader/downloads/*

# Visual Studio settings
.vscode
200 changes: 200 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "pokemon"
version = "0.1.0"
description = "The highest quality Pokemon images."
authors = ["Your Name <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"
requests = "2.32.3"
beautifulsoup4 = "4.12.3"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
51 changes: 0 additions & 51 deletions src/imageDownloader/Downloader.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
69 changes: 69 additions & 0 deletions src/pokemon/imageDownloader/Downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import requests
import re
import os.path
from pathlib import Path

FILE_PATH = "URLs/URLs.txt"

# This script will download all files from the URLs/URLs.txt and put them in Downloads directory
downloadDir = "downloads/"


def Download(FileName, response, url):
# If the downloads folder does not exist, it is created
if not Path(downloadDir).exists():
Path(downloadDir).mkdir()

with open(FileName, "wb") as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print("Downloaded: " + url)


def get_urls(file_path: str):
f = open(file_path, "r")
Lines = f.readlines()
URLs = []
for line in Lines:
URLs.append(line.strip()) # Stripping the newline character
f.close()
return URLs


def main():
while True:
option = input("Re download All files ?(Y,N): ")
if option in ["Y", "y"]:
ReDownloadOnlyCorruptedFiles = False
break
if option in ["N", "n"]:
ReDownloadOnlyCorruptedFiles = True
# Re-download only corrupted files (sometimes <1kb corrupted files are downloaded from Bulbapedia)
print("Only new/ corrupted files will be downloaded")
break

URLs = get_urls(FILE_PATH)
# Downloading
for url in URLs:
try:
pokemon_id = re.search(r"/\d\d\d\d", url).group(0)
pokemon_id = pokemon_id[1:]
fileToDownload = downloadDir + pokemon_id + ".png"
response = requests.get(url, stream=True)
if not ReDownloadOnlyCorruptedFiles:
Download(
fileToDownload, response, url
) # (Re-)Download all files unconditionally
elif os.path.exists(fileToDownload):
file_stat = os.stat(fileToDownload)
if file_stat.st_size < 1000:
Download(fileToDownload) # Re-download only corrupted files
else:
Download(fileToDownload) # Download new file
except AttributeError:
print("An Error Occured for: " + pokemon_id)


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.