ModpackUpdater/FTP.py

139 lines
4.1 KiB
Python
Raw Normal View History

2024-06-21 14:40:14 -06:00
from ftplib import FTP_TLS as FTP
from customtkinter import *
import time
import datetime
import math
from tkinter import simpledialog
import os
def ftpDownload(file, stream):
ftp = FTP("gitea.piwalker.net")
ftp.login()
ftp.prot_p()
#create tkinter window
print("downloading file: "+file)
dialog = CTk()
if os.name == 'posix':
dialog.attributes('-type', 'dialog')
dialog.title("Downloading Files")
set_appearance_mode("dark")
set_default_color_theme("blue")
label = CTkLabel(master=dialog, text="Downloading "+file.split("/")[-1])
label.pack()
pbar = CTkProgressBar(master=dialog)
pbar.pack(padx=20, pady=20)
progress = StringVar()
ETA = StringVar()
progress_label = CTkLabel(master=dialog, textvariable=progress)
progress_label.pack()
eta_label = CTkLabel(master=dialog, textvariable=ETA)
eta_label.pack()
size = ftp.size(file)
if size < 1024*1024:
dialog.withdraw()
total = 0
timer = 0
start = time.time()
dialog.update()
def downloadCallback(data):
nonlocal total
nonlocal pbar
nonlocal start
nonlocal progress
nonlocal ETA
nonlocal timer
stream.write(data)
total += len(data)
if time.time() - timer >= 1:
progress.set(str(round(total/1048576, 1))+" MB / "+str(round(size/1048576, 1))+" MB @ " + str(round((total/1048576)/(time.time()-start), 3))+" MB/s")
time_left = (size-total)/(total/(time.time()-start))
ETA.set("ETA: " + str(datetime.timedelta(seconds=math.ceil(time_left))))
pbar.set(total/size)
timer = time.time()
dialog.update()
ftp.retrbinary("RETR " + file, downloadCallback)
dialog.destroy()
ftp.close()
def ftpUpload(file, stream, username, password):
ftp = FTP("gitea.piwalker.net", username, password)
ftp.prot_p()
up_dialog = CTk()
if os.name == 'posix':
up_dialog.attributes('-type', 'dialog')
up_dialog.title("Uploading Files")
set_appearance_mode("dark")
set_default_color_theme("blue")
label = CTkLabel(master=up_dialog, text="Uploading "+file.split("/")[-1])
label.pack()
pbar = CTkProgressBar(master=up_dialog)
pbar.pack(padx=20, pady=20)
prog = StringVar(master=up_dialog)
eta = StringVar(master=up_dialog)
progress_label = CTkLabel(master=up_dialog, textvariable=prog)
progress_label.pack()
eta_label = CTkLabel(master=up_dialog, textvariable=eta)
eta_label.pack()
stream.seek(0, os.SEEK_END)
size = stream.tell()
stream.seek(0)
if size < 1024*1024:
up_dialog.withdraw()
total = 0
timer = 0
start = time.time()
up_dialog.update()
def uploadCallback(data):
nonlocal total
nonlocal pbar
nonlocal start
nonlocal prog
nonlocal eta
nonlocal timer
nonlocal up_dialog
total += len(data)
if time.time() - timer >= 1:
prog.set(str(round(total/1048576, 1))+" MB / "+str(round(size/1048576, 1))+" MB @ " + str(round((total/1048576)/(time.time()-start), 3))+" MB/s")
time_left = (size-total)/(total/(time.time()-start))
eta.set("ETA: " + str(datetime.timedelta(seconds=math.ceil(time_left))))
pbar.set(total/size)
timer = time.time()
up_dialog.update()
up_dialog.update()
ftp.storbinary("STOR "+file,stream, callback=uploadCallback)
up_dialog.destroy()
ftp.close()
def ftpMakeDirectory(path, username, password):
ftp = FTP("gitea.piwalker.net", username, password)
ftp.prot_p()
ftp.mkd(path)
ftp.close()
def ftpDeleteDirectory(path, username, password):
def deleteFolder(ftp, path):
print("Deleting folder: "+path)
ftp.cwd(path)
for item in ftp.nlst():
try:
print("deleting file: "+path)
ftp.delete(item)
except:
deleteFolder(ftp, item)
ftp.cwd("..")
ftp.rmd(path)
ftp = FTP("gitea.piwalker.net", username, password)
ftp.prot_p()
deleteFolder(ftp, path)
ftp.close()