Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from techcore import ftp
- import os , getpass , requests , json
- def checkCategory(category , server , folder) :
- obj = getCategoriesList(server , folder)
- return category in list(obj.keys())
- def checkImage(image , category , server , folder) :
- imgs = getImagesList(category , server , folder)
- return image in imgs
- def getImageList(category , server , folder) :
- if checkCategory(category, server , folder) :
- cts = getCategoriesList(server , folder)
- request = requests.get(server + folder + cts[category] + "/list.json")
- request.raise_for_status()
- obj = json.loads(request.text)
- return obj
- def getCategoriesList(server , folder) :
- request = requests.get(server + folder + "categories.json")
- request.raise_for_status()
- obj = json.loads(request.text)
- return obj
- print("SfondiPaper Server Management Tool [V.1.0.0]")
- print("Select option:")
- print("1) Add Image")
- print("2) Remove Image")
- print("3) List Images")
- print("4) Set Wallpaper of the Month")
- op = None
- while op == None :
- try :
- op = int(input("Option: "))
- except ValueError :
- print("Inserisci un numero!")
- if op == 1 :
- path = ""
- while True :
- path = input("Insert path of the image: ")
- if not os.path.isfile(path) or not (path.endswith(".png") or path.endswith(".jpg")) :
- print("Insert valid path for a PNG/JPG image!")
- continue
- break
- server = input("Insert server: ")
- if not server.startswith("http://") :
- server = "http://" + server
- folder = input("Insert folder: ")
- category = ""
- while not checkCategory(category , server , folder) :
- category = input("Insert category: ")
- user = input("Username: ")
- psw = input("Password: ")
- #print(server,user,psw)
- conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
- print("Connected! Uploading Image...")
- filename = ""
- cts = getCategoriesList(server , folder)
- rawCategory = cts[category]
- with open(path , "rb") as file :
- filename = file.name.split("\\")[-1]
- ftp.uploadFile(conn , file , folder + rawCategory)
- print("Upload complete! Updating JSON files...")
- os.makedirs("work" , exist_ok=True)
- os.chdir("work")
- request = requests.get(server + folder + "all.json")
- request.raise_for_status()
- if request.text == "" :
- obj = {}
- else :
- obj = json.loads(request.text)
- obj[category].append(filename)
- text = json.dumps(obj)
- with open("all.json" , "w") as file :
- file.write(text)
- with open("all.json" , "rb") as file :
- ftp.uploadFile(conn , file , folder)
- print("General JSON file updated, updating category-based file...")
- request = requests.get(server + folder + rawCategory + "/list.json")
- request.raise_for_status()
- if request.text == "" :
- obj = []
- else :
- obj = json.loads(request.text)
- obj.append(filename)
- text = json.dumps(obj)
- with open("list.json" , "w") as file :
- file.write(text)
- with open("list.json" , "rb") as file :
- ftp.uploadFile(conn , file , folder + rawCategory)
- print("All files updated! Closing connection...")
- conn.quit()
- print("Connection closed, operations finished!")
- if op == 2 :
- server = input("Insert server: ")
- if not server.startswith("http://") :
- server = "http://" + server
- folder = input("Insert folder: ")
- category = ""
- while not checkCategory(category , server , folder) :
- category = input("Insert category: ")
- img = ""
- while not checkImage(img , category , server , folder) :
- img = input("Insert Image(With extension .png/.jpg): ")
- user = input("Username: ")
- psw = input("Password: ")
- conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
- print("Connected! Removing Image...")
- cts = getCategoriesList(server , folder)
- ftp.deleteFile(conn , "/" + folder + "/" + cts[category] + "/" + image)
- print("Image deleted! Updating JSON Files")
- os.makedirs("work" , exist_ok=True)
- os.chdir("work")
- request = requests.get(server + folder + "all.json")
- request.raise_for_status()
- if request.text == "" :
- obj = {}
- else :
- obj = json.loads(request.text)
- obj[category].remove(image)
- text = json.dumps(obj)
- with open("all.json" , "w") as file :
- file.write(text)
- with open("all.json" , "rb") as file :
- ftp.uploadFile(conn , file , folder)
- print("General JSON file updated, updating category-based file...")
- request = requests.get(server + folder + rawCategory + "/list.json")
- request.raise_for_status()
- if request.text == "" :
- obj = []
- else :
- obj = json.loads(request.text)
- obj.remove(image)
- text = json.dumps(obj)
- with open("list.json" , "w") as file :
- file.write(text)
- with open("list.json" , "rb") as file :
- ftp.uploadFile(conn , file , folder + rawCategory)
- print("All files updated! Closing connection...")
- conn.quit()
- print("Connection closed, operations finished!")
- if op == 3 :
- server = input("Insert server: ")
- if not server.startswith("http://") :
- server = "http://" + server
- folder = input("Insert folder: ")
- category = ""
- while not checkCategory(category , server , folder) :
- category = input("Insert category: ")
- user = input("Username: ")
- psw = input("Password: ")
- print("Retrieving images...")
- obj = getImageList(category , server , folder)
- for o in obj :
- print("Image: " + o)
- if op == 4 :
- server = input("Insert server: ")
- if not server.startswith("http://") :
- server = "http://" + server
- folder = input("Insert folder: ")
- category = ""
- while not checkCategory(category , server , folder) :
- category = input("Insert category: ")
- img = ""
- while not checkImage(img , category , server , folder) :
- img = input("Insert Image(With extension .png/.jpg): ")
- user = input("Username: ")
- psw = input("Password: ")
- print("Updating JSON File...")
- conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
- obj = {}
- obj[category] = img
- text = json.dumps(obj)
- with open("month.json" , "w") as file :
- file.write(text)
- with open("month.json" , "rb") as file :
- ftp.uploadFile(conn , file , folder + rawCategory)
- print("All files updated! Closing connection...")
- conn.quit()
- print("Connection closed, operations finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement