Minor advance

This commit is contained in:
Jérémi N ‘EndMove’ 2022-08-31 23:53:24 +02:00
parent a93a88d344
commit 03e35c18a9
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
3 changed files with 40 additions and 21 deletions

View File

@ -1,3 +1,4 @@
import time
from controller.MainController import MainController
@ -30,6 +31,7 @@ class HomeController:
:view: -> The view that this controller manage.
"""
self.__view = view
self.__webpic.set_messenger_callback(view.add_log)
# END View method
# START View events
@ -54,6 +56,16 @@ class HomeController:
if url.strip() and name.strip() :
# TODO use webpic here to download files
print(f"url : {url} -- name : {name}")
self.__view.clear_logs() # BUG this process must be asynchrone ...
if self.__webpic.download(url, name):
self.__view.show_success_message("The download has been successfully completed.")
else:
self.__view.show_error_message("A critical error preventing the download occurred, check the logs.")
else:
self.__view.show_error_message("Opss, the url or folder name are not valid!")
# END View events
# START Controller methods
# END Controller methods

View File

@ -3,6 +3,7 @@ from urllib import request
from urllib.error import HTTPError, URLError
from bs4 import BeautifulSoup, Tag, ResultSet
class WebPicDownloader():
"""
WebPicDownloader
@ -15,14 +16,14 @@ class WebPicDownloader():
"""
# Variables
path: str = None
message_callback: function = None
messenger = None
headers: dict = None
# Constructor
def __init__(self, path: str = os.getcwd()) -> None:
"""Constructor"""
self.path = path
self.message_callback = lambda message: print(message)
self.messenger = lambda message: print(message)
self.headers = {'User-Agent': "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"}
# Internal functions
@ -50,7 +51,7 @@ class WebPicDownloader():
else:
raise ValueError("Unable to find image url")
if not 'http' in link:
raise ValueError("Bad image link")
raise ValueError("Bad image url")
return link
def __find_image_type(self, img_link: str) -> str:
@ -75,15 +76,15 @@ class WebPicDownloader():
raise ValueError("the folder already exists, it may already contain images")
# Public functions
def set_message_callback(self, callback) -> None:
def set_messenger_callback(self, callback) -> None:
"""
Setter to define the callback function in case of new messages.
Setter to define the callback function called when new messages arrive.
:callback: -> the callback function to call when a message event is emited.
"""
self.message_callback = callback
self.messenger = callback
def download(self, url: str, folder_name: str) -> None:
def download(self, url: str, folder_name: str) -> bool:
"""
Start downloading all pictures of a website
@ -97,29 +98,31 @@ class WebPicDownloader():
images = self.__find_all_img(html)
self.__initialize_folder(folder_path)
print(f"\nWebPicDownload found {len(images)} images on the website.")
self.messenger(f"WebPicDownloader found {len(images)} images on the website.")
for i, img in enumerate(images):
try:
self.messenger(f"Start downloading image {i}.")
img_link = self.__find_img_link(img)
self.__download_img(img_link, f"{folder_path}image-{i}.{self.__find_image_type(img_link)}")
print(f"SUCCESS: File n°{i} successfuly downloaded.")
self.messenger(f"Download of image {i}, done!")
count += 1
except ValueError as err:
print(f"ERROR: Unable to process image n°{i} -> [{err}].")
except Exception as err:
print(f"ERROR: Unable to process image n°{i}, an unknown error occured -> [{err}].")
self.messenger(f"ERROR: Unable to process image {i} -> err[{err}].")
print(f"WebPicDownloader has processed {count} images out of {len(images)}.")
self.messenger(f"WebPicDownloader has processed {count} images out of {len(images)}.")
return True
except HTTPError as err:
print(f"ERROR: An http error occured -> [{err}].")
self.messenger(f"ERROR: An http error occured -> err[{err}].")
except (ValueError, URLError) as err:
print(f"ERROT: An error occured with the url -> [{err}].")
self.messenger(f"ERROT: An error occured with the url -> err[{err}].")
except Exception as err:
print(f"ERROR: An unknown error occured -> [{err}]")
self.messenger(f"ERROR: An unknown error occured -> err[{err}]")
return False
if __name__ == "__main__":
wpd = WebPicDownloader()
wpd.set_messenger_callback(lambda msg: print(f"--> {msg}"))
while True:
url = input("Website URL ? ")
name = input("Folder name ? ")

View File

@ -1,6 +1,7 @@
import tkinter as tk
import tkinter.font as tfont
from tkinter import ttk
from tkinter import scrolledtext as st
from tkinter import scrolledtext as tst
from controller.Frames import Frames
from controller.HomeController import HomeController
@ -51,7 +52,8 @@ class HomeView(ttk.Frame):
self.name_entry.grid(row=1, column=1, sticky=tk.E, padx=5, pady=5, ipadx=2, ipady=2)
# Logs area
self.log_textarea = st.ScrolledText(self, wrap=tk.WORD, state=tk.DISABLED, width=40, height=8)#, font=("Times New Roman", 15))
log_textarea_font = tfont.Font(size=10)
self.log_textarea = tst.ScrolledText(self, font=log_textarea_font, wrap=tk.WORD, state=tk.DISABLED, width=40, height=8)#, font=("Times New Roman", 15))
self.log_textarea.grid(row=3, column=0, columnspan=2, sticky=tk.EW, pady=10, padx=10)
self.message_label = ttk.Label(self, text='message label')
@ -75,15 +77,17 @@ class HomeView(ttk.Frame):
TODO desc
"""
self.log_textarea.configure(state=tk.NORMAL)
self.log_textarea.insert(tk.END, f"{line}\n")
self.log_textarea.insert(tk.END, f"~ {line}\n")
self.log_textarea.configure(state=tk.DISABLED)
def clear_log(self) -> None:
def clear_logs(self) -> None:
"""
[function for controller]
TODO desc
"""
self.log_textarea.delete(0, tk.END)
self.log_textarea.configure(state=tk.NORMAL)
self.log_textarea.delete("1.0", tk.END)
self.log_textarea.configure(state=tk.DISABLED)
def show_error_message(self, message) -> None:
"""