Major advance + start adapting my webpic script for a graphic version
This commit is contained in:
parent
16794bf488
commit
a93a88d344
@ -15,10 +15,12 @@ class HomeController:
|
|||||||
# Variables
|
# Variables
|
||||||
__main_controller = None
|
__main_controller = None
|
||||||
__view = None
|
__view = None
|
||||||
|
__webpic = None
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
def __init__(self, controller: MainController) -> None:
|
def __init__(self, controller: MainController, webpic) -> None:
|
||||||
self.__main_controller = controller
|
self.__main_controller = controller
|
||||||
|
self.__webpic = webpic
|
||||||
|
|
||||||
# START View methods
|
# START View methods
|
||||||
def set_view(self, view) -> None:
|
def set_view(self, view) -> None:
|
||||||
@ -40,19 +42,18 @@ class HomeController:
|
|||||||
#self.__main_controller.change_frame(frame)
|
#self.__main_controller.change_frame(frame)
|
||||||
self.__view.add_log("Salut comment vas tu ? 1")
|
self.__view.add_log("Salut comment vas tu ? 1")
|
||||||
self.__view.add_log("Salut comment vas tu ? 2")
|
self.__view.add_log("Salut comment vas tu ? 2")
|
||||||
self.__view.add_log("Salut comment vas tu ? 3")
|
self.__view.show_success_message("Super tu as réussi la quête !")
|
||||||
self.__view.add_log("Salut comment vas tu ? 4")
|
|
||||||
self.__view.add_log("Salut comment vas tu ? 5")
|
|
||||||
self.__view.add_log("Salut comment vas tu ? 6")
|
|
||||||
self.__view.add_log("Salut comment vas tu ? 7")
|
|
||||||
self.__view.add_log("Salut comment vas tu ? 8")
|
|
||||||
|
|
||||||
def on_download_started(self, url: str, name: str) -> None:
|
def on_download_requested(self, url: str, name: str) -> None:
|
||||||
"""
|
"""
|
||||||
[event function for view]
|
[event function for view]
|
||||||
|
|
||||||
:url: -> The url of the website to use for pic-download.\n
|
:url: -> The url of the website to use for pic-download.\n
|
||||||
:name: -> The name of the folder in which put pictures.
|
:name: -> The name of the folder in which put pictures.
|
||||||
"""
|
"""
|
||||||
pass
|
if url.strip() and name.strip() :
|
||||||
|
# TODO use webpic here to download files
|
||||||
|
print(f"url : {url} -- name : {name}")
|
||||||
|
else:
|
||||||
|
self.__view.show_error_message("Opss, the url or folder name are not valid!")
|
||||||
# END View events
|
# END View events
|
||||||
|
6
main.py
6
main.py
@ -2,6 +2,7 @@ from controller.HomeController import HomeController
|
|||||||
from controller.InfoController import InfoController
|
from controller.InfoController import InfoController
|
||||||
from controller.MainController import MainController
|
from controller.MainController import MainController
|
||||||
from controller.Frames import Frames
|
from controller.Frames import Frames
|
||||||
|
from model.webpic import WebPicDownloader
|
||||||
from view.HomeView import HomeView
|
from view.HomeView import HomeView
|
||||||
from view.InfoView import InfoView
|
from view.InfoView import InfoView
|
||||||
from view.MainWindow import MainWindow
|
from view.MainWindow import MainWindow
|
||||||
@ -17,9 +18,12 @@ if __name__ == '__main__':
|
|||||||
@version 1.0.0
|
@version 1.0.0
|
||||||
@since 2022-08-30
|
@since 2022-08-30
|
||||||
"""
|
"""
|
||||||
|
# Create utli/model
|
||||||
|
webpic = WebPicDownloader()
|
||||||
|
|
||||||
# Create app controllers
|
# Create app controllers
|
||||||
main_controller = MainController()
|
main_controller = MainController()
|
||||||
home_controller = HomeController(main_controller)
|
home_controller = HomeController(main_controller, webpic)
|
||||||
info_controller = InfoController(main_controller)
|
info_controller = InfoController(main_controller)
|
||||||
|
|
||||||
# Create app views
|
# Create app views
|
||||||
|
@ -11,30 +11,33 @@ class WebPicDownloader():
|
|||||||
find and download all pictures on a webpage.
|
find and download all pictures on a webpage.
|
||||||
|
|
||||||
@author EndMove <contact@endmove.eu>
|
@author EndMove <contact@endmove.eu>
|
||||||
@version 1.0.0
|
@version 1.1.0
|
||||||
"""
|
"""
|
||||||
# Variables
|
# Variables
|
||||||
path: str
|
path: str = None
|
||||||
headers: dict = {'User-Agent': "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"}
|
message_callback: function = None
|
||||||
|
headers: dict = None
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
def __init__(self, path: str = os.getcwd()) -> None:
|
def __init__(self, path: str = os.getcwd()) -> None:
|
||||||
"""Constructor"""
|
"""Constructor"""
|
||||||
self.path = path
|
self.path = path
|
||||||
|
self.message_callback = 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
|
# Internal functions
|
||||||
def __getHtml(self, url: str) -> str:
|
def __get_html(self, url: str) -> str:
|
||||||
"""Allow to retrieve the HTML content of a website"""
|
"""Allow to retrieve the HTML content of a website"""
|
||||||
req = request.Request(url, headers=self.headers)
|
req = request.Request(url, headers=self.headers)
|
||||||
response = request.urlopen(req)
|
response = request.urlopen(req)
|
||||||
return response.read().decode('utf-8')
|
return response.read().decode('utf-8')
|
||||||
|
|
||||||
def __findAllImg(self, html: str) -> ResultSet:
|
def __find_all_img(self, html: str) -> ResultSet:
|
||||||
"""Allow to retrieve all images of an html page"""
|
"""Allow to retrieve all images of an html page"""
|
||||||
soup = BeautifulSoup(html, 'html.parser')
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
return soup.find_all('img')
|
return soup.find_all('img')
|
||||||
|
|
||||||
def __findImgLink(self, img: Tag) -> str:
|
def __find_img_link(self, img: Tag) -> str:
|
||||||
"""Allow to retrieve the link of a picture"""
|
"""Allow to retrieve the link of a picture"""
|
||||||
if img.get('src'):
|
if img.get('src'):
|
||||||
link = img.get('src')
|
link = img.get('src')
|
||||||
@ -50,48 +53,56 @@ class WebPicDownloader():
|
|||||||
raise ValueError("Bad image link")
|
raise ValueError("Bad image link")
|
||||||
return link
|
return link
|
||||||
|
|
||||||
def __findImageType(self, imgLink: str) -> str:
|
def __find_image_type(self, img_link: str) -> str:
|
||||||
"""Allow to retrieve the right image type"""
|
"""Allow to retrieve the right image type"""
|
||||||
type = imgLink.split('.')[-1]
|
type = img_link.split('.')[-1]
|
||||||
if '?' in type:
|
if '?' in type:
|
||||||
type = type.split('?')[0]
|
type = type.split('?')[0]
|
||||||
return type
|
return type
|
||||||
|
|
||||||
def __downloadImg(self, url: str, filename: str) -> None:
|
def __download_img(self, url: str, filename: str) -> None:
|
||||||
"""Allow to download a picture from internet"""
|
"""Allow to download a picture from internet"""
|
||||||
req = request.Request(url, headers=self.headers)
|
req = request.Request(url, headers=self.headers)
|
||||||
rawImg = request.urlopen(req).read()
|
raw_img = request.urlopen(req).read()
|
||||||
with open(filename, 'wb') as img:
|
with open(filename, 'wb') as img:
|
||||||
img.write(rawImg)
|
img.write(raw_img)
|
||||||
|
|
||||||
def __initializeFolder(self, folderPath: str) -> None:
|
def __initialize_folder(self, folder_path: str) -> None:
|
||||||
"""Init the folder on which put downloaded images"""
|
"""Init the folder on which put downloaded images"""
|
||||||
if not os.path.exists(folderPath):
|
if not os.path.exists(folder_path):
|
||||||
os.mkdir(folderPath)
|
os.mkdir(folder_path)
|
||||||
else:
|
else:
|
||||||
raise ValueError("the folder already exists, it may already contain images")
|
raise ValueError("the folder already exists, it may already contain images")
|
||||||
|
|
||||||
# Public functions
|
# Public functions
|
||||||
def download(self, url: str, folderName: str) -> None:
|
def set_message_callback(self, callback) -> None:
|
||||||
|
"""
|
||||||
|
Setter to define the callback function in case of new messages.
|
||||||
|
|
||||||
|
:callback: -> the callback function to call when a message event is emited.
|
||||||
|
"""
|
||||||
|
self.message_callback = callback
|
||||||
|
|
||||||
|
def download(self, url: str, folder_name: str) -> None:
|
||||||
"""
|
"""
|
||||||
Start downloading all pictures of a website
|
Start downloading all pictures of a website
|
||||||
|
|
||||||
:url: -> The url of the website to annalyse.\n
|
:url: -> The url of the website to annalyse.\n
|
||||||
:folderName: -> The name of the folder in which to upload the photos.
|
:folder_name: -> The name of the folder in which to upload the photos.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
count = 0
|
count = 0
|
||||||
folderPath = f"{self.path}/{folderName}/"
|
folder_path = f"{self.path}/{folder_name}/"
|
||||||
html = self.__getHtml(url)
|
html = self.__get_html(url)
|
||||||
images = self.__findAllImg(html)
|
images = self.__find_all_img(html)
|
||||||
|
|
||||||
self.__initializeFolder(folderPath)
|
self.__initialize_folder(folder_path)
|
||||||
print(f"\nWebPicDownload found {len(images)} images on the website.")
|
print(f"\nWebPicDownload found {len(images)} images on the website.")
|
||||||
|
|
||||||
for i, img in enumerate(images):
|
for i, img in enumerate(images):
|
||||||
try:
|
try:
|
||||||
imgLink = self.__findImgLink(img)
|
img_link = self.__find_img_link(img)
|
||||||
self.__downloadImg(imgLink, f"{folderPath}image-{i}.{self.__findImageType(imgLink)}")
|
self.__download_img(img_link, f"{folder_path}image-{i}.{self.__find_image_type(img_link)}")
|
||||||
print(f"SUCCESS: File n°{i} successfuly downloaded.")
|
print(f"SUCCESS: File n°{i} successfuly downloaded.")
|
||||||
count += 1
|
count += 1
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
|
@ -24,28 +24,6 @@ class HomeView(ttk.Frame):
|
|||||||
|
|
||||||
# Init view
|
# Init view
|
||||||
self.__init_content()
|
self.__init_content()
|
||||||
|
|
||||||
# create widgets
|
|
||||||
# label
|
|
||||||
# self.label = ttk.Label(self, text='Email:')
|
|
||||||
# self.label.grid(row=1, column=0)
|
|
||||||
|
|
||||||
# email entry
|
|
||||||
# self.email_var = tk.StringVar()
|
|
||||||
# self.email_entry = ttk.Entry(self, textvariable=self.email_var, width=30)
|
|
||||||
# self.email_entry.grid(row=1, column=1, sticky=tk.NSEW)
|
|
||||||
|
|
||||||
# save button
|
|
||||||
# self.save_button = ttk.Button(self, text='Save', command=self.event_btn)
|
|
||||||
# self.save_button.grid(row=1, column=3, padx=10)
|
|
||||||
|
|
||||||
# message
|
|
||||||
# self.message_label = ttk.Label(self, text='coucou toi :D JE SUIS SUPER MAN héhéh', foreground='red')
|
|
||||||
# self.message_label.grid(row=2, column=1, sticky=tk.W)
|
|
||||||
|
|
||||||
# place this frame
|
|
||||||
# self.grid(row=0, column=0, padx=5, pady=5)
|
|
||||||
# self.pack(fill='both', expand=True)
|
|
||||||
|
|
||||||
# Save and setup main controller
|
# Save and setup main controller
|
||||||
self.__controller = controller
|
self.__controller = controller
|
||||||
@ -62,23 +40,32 @@ class HomeView(ttk.Frame):
|
|||||||
|
|
||||||
# Website link
|
# Website link
|
||||||
self.web_label = ttk.Label(self, text="Website URL:")
|
self.web_label = ttk.Label(self, text="Website URL:")
|
||||||
self.web_label.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)
|
self.web_label.grid(row=0, column=0, sticky=tk.W, padx=5, pady=5)
|
||||||
self.web_entry = ttk.Entry(self, width=50) # show="-"
|
self.web_entry = ttk.Entry(self, width=50) # show="-"
|
||||||
self.web_entry.grid(column=1, row=0, sticky=tk.E, padx=5, pady=5, ipadx=2, ipady=2)
|
self.web_entry.grid(row=0, column=1, sticky=tk.E, padx=5, pady=5, ipadx=2, ipady=2)
|
||||||
|
|
||||||
# Download name
|
# Download name
|
||||||
self.name_label = ttk.Label(self, text="Download Name:")
|
self.name_label = ttk.Label(self, text="Download Name:")
|
||||||
self.name_label.grid(column=0, row=1, sticky=tk.W, padx=5, pady=5)
|
self.name_label.grid(row=1, column=0, sticky=tk.W, padx=5, pady=5)
|
||||||
self.name_entry = ttk.Entry(self, width=50)
|
self.name_entry = ttk.Entry(self, width=50)
|
||||||
self.name_entry.grid(column=1, row=1, sticky=tk.E, padx=5, pady=5, ipadx=2, ipady=2)
|
self.name_entry.grid(row=1, column=1, sticky=tk.E, padx=5, pady=5, ipadx=2, ipady=2)
|
||||||
|
|
||||||
# Logs area
|
# Logs area
|
||||||
self.log_area = st.ScrolledText(self, wrap=tk.WORD, state = tk.DISABLED, width=40, height=8)#, font=("Times New Roman", 15))
|
self.log_textarea = st.ScrolledText(self, wrap=tk.WORD, state=tk.DISABLED, width=40, height=8)#, font=("Times New Roman", 15))
|
||||||
self.log_area.grid(column=0, row=3, sticky=tk.EW, columnspan=2, pady=10, padx=10)
|
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')
|
||||||
|
|
||||||
# Download button
|
# Download button
|
||||||
self.download_button = ttk.Button(self, text="Start downloading", command=self.event_btn)
|
self.download_button = ttk.Button(self, text="Start downloading", command=self.__event_button_download)
|
||||||
self.download_button.grid(column=1, row=4, sticky=tk.E, padx=5, pady=5, ipadx=5, ipady=2)
|
self.download_button.grid(row=5, column=1, sticky=tk.E, padx=5, pady=5, ipadx=5, ipady=2)
|
||||||
|
|
||||||
|
def __event_button_download(self) -> None:
|
||||||
|
"""
|
||||||
|
[internal function]
|
||||||
|
=> Function called when a download is requested.
|
||||||
|
"""
|
||||||
|
self.__controller.on_download_requested(self.web_entry.get(), self.name_entry.get())
|
||||||
# END Internal function
|
# END Internal function
|
||||||
|
|
||||||
# START Controller methods
|
# START Controller methods
|
||||||
@ -87,20 +74,39 @@ class HomeView(ttk.Frame):
|
|||||||
[function for controller]
|
[function for controller]
|
||||||
TODO desc
|
TODO desc
|
||||||
"""
|
"""
|
||||||
print("ADD: ", line)
|
self.log_textarea.configure(state=tk.NORMAL)
|
||||||
self.log_area.configure(state=tk.NORMAL)
|
self.log_textarea.insert(tk.END, f"{line}\n")
|
||||||
self.log_area.insert(tk.END, f"{line}\n")
|
self.log_textarea.configure(state=tk.DISABLED)
|
||||||
self.log_area.configure(state=tk.DISABLED)
|
|
||||||
|
|
||||||
def clear_log(self) -> None:
|
def clear_log(self) -> None:
|
||||||
"""
|
"""
|
||||||
[function for controller]
|
[function for controller]
|
||||||
TODO desc
|
TODO desc
|
||||||
"""
|
"""
|
||||||
self.log_area.delete(0, tk.END)
|
self.log_textarea.delete(0, tk.END)
|
||||||
|
|
||||||
# END Controller methods
|
def show_error_message(self, message) -> None:
|
||||||
|
"""
|
||||||
|
[function for controller]
|
||||||
|
TODO desc
|
||||||
|
"""
|
||||||
|
self.message_label.configure(text=message, foreground='red')
|
||||||
|
self.message_label.grid(row=4, column=0, columnspan=2, sticky=tk.NS, padx=2, pady=2)
|
||||||
|
self.message_label.after(30000, self.hide_message)
|
||||||
|
|
||||||
def event_btn(self) -> None:# TODO remove
|
def show_success_message(self, message) -> None:
|
||||||
print("you clicked ! And now you are on info page ... :D")
|
"""
|
||||||
self.__controller.on_change_view(Frames.Info)
|
[function for controller]
|
||||||
|
TODO desc
|
||||||
|
"""
|
||||||
|
self.message_label.configure(text=message, foreground='green')
|
||||||
|
self.message_label.grid(row=4, column=0, columnspan=2, sticky=tk.NS, padx=2, pady=2)
|
||||||
|
self.message_label.after(30000, self.hide_message)
|
||||||
|
|
||||||
|
def hide_message(self) -> None:
|
||||||
|
"""
|
||||||
|
[function for controller]
|
||||||
|
TODO desc
|
||||||
|
"""
|
||||||
|
self.message_label.grid_forget()
|
||||||
|
# END Controller methods
|
||||||
|
Reference in New Issue
Block a user