Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from techcore import ftp
- import os , getpass , requests , json , time , sys
- server = ""
- user = ""
- psw = ""
- def checkCategory(category , server , folder) :
- obj = getCategoriesList(server , folder)
- return category in list(obj.keys())
- def checkImage(image , category , server , folder) :
- imgs = getImageList(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 checkExit(x) :
- return False #x == "abort"
- def getPackList(server , folder) :
- request = requests.get(server + folder + "list.json")
- request.raise_for_status()
- obj = json.loads(request.text)
- return obj
- def checkPack(pack , server , folder) :
- l = getPackList(server , folder)
- return pack in l
- def getCategoriesList(server , folder) :
- request = requests.get(server + folder + "categories.json")
- request.raise_for_status()
- obj = json.loads(request.text)
- return obj
- def newInput(message) :
- t = newInput(message)
- if checkExit(t) :
- raise ImportError()
- else :
- return t
- newInput , input = input , newInput
- def testConnection() :
- global server,user,psw
- ftp.FTP(host=server[7:] , user=user , passwd=psw)
- return
- def login() :
- global server,user,psw
- temps = input("Insert server: ")
- if not temps.startswith("http://") :
- temps = "http://" + temps
- server = temps
- print()
- user = input("login as: ")
- psw = getpass.getpass("password: ")
- testConnection()
- print("Connection Started!\n")
- return
- def menu() :
- print("SfondiPaper Server Management Tool [V.3.0.0]")
- print("Select option:")
- print("1) Add Image")
- print("2) Remove Image")
- print("3) List Images")
- print("4) Set Wallpaper of the Month")
- print("5) Add Wallpaper Pack")
- print("6) Remove Wallpaper Pack")
- print("7) List Wallapaper Packs")
- print("9) Exit")
- def start() :
- global server,user,psw
- while True :
- op = None
- while op == None :
- try :
- op = int(input("\nOption: "))
- except ValueError :
- print("Inserisci un numero!")
- if op == 1 :
- path = ""
- while True :
- path = input("Insert path of the image: ")
- checkExit(path)
- 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
- folder = "/categories/"
- category = ""
- while not checkCategory(category , server , folder) :
- category = input("Insert category: ")
- #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 :
- folder = "/categories/"
- 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): ")
- conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
- print("Connected! Removing Image...")
- cts = getCategoriesList(server , folder)
- ftp.deleteFile(conn , folder + cts[category] + "/" + img)
- 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(img)
- 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 :
- folder = "/categories/"
- category = ""
- while not checkCategory(category , server , folder) :
- category = input("Insert category: ")
- print("Retrieving images...")
- obj = getImageList(category , server , folder)
- for o in obj :
- print("Image: " + o)
- if op == 4 :
- folder = "/categories/"
- 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): ")
- print("Updating JSON File...")
- #print(server , user , psw)
- 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)
- print("All files updated! Closing connection...")
- conn.quit()
- print("Connection closed, operations finished!")
- if op == 5 :
- folder = "/packs/"
- path = ""
- while not (path.endswith(".zip") and os.path.isfile(path)) :
- path = input("Insert path of zip file: ")
- conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
- print("Connected! Uploading zip pack...")
- with open(path , "rb") as file :
- ftp.uploadFile(conn , file , folder)
- print("Upload complete! Updating JSON Files...")
- os.makedirs("work" , exist_ok=True)
- os.chdir("work")
- request = requests.get(server + folder + "list.json")
- request.raise_for_status()
- if request.text == "" :
- obj = []
- else :
- obj = json.loads(request.text)
- obj.append(os.path.basename(path))
- 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)
- print("All files updated! Closing connection...")
- conn.quit()
- print("Connection closed, operations finished!")
- if op == 6 :
- folder = "/packs/"
- name = ""
- while not checkPack(name , server , folder) :
- name = input("Insert name of pack to delete: ")
- conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
- print("Connected! Deleting zip pack...")
- ftp.deleteFile(conn , folder + name)
- print("Zip file deleted, updating JSON Files...")
- os.makedirs("work" , exist_ok=True)
- os.chdir("work")
- request = requests.get(server + folder + "list.json")
- request.raise_for_status()
- if request.text == "" :
- obj = [name]
- else :
- obj = json.loads(request.text)
- obj.remove(name)
- 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)
- print("All files updated! Closing connection...")
- conn.quit()
- print("Connection closed, operations finished!")
- if op == 7 :
- folder = "/packs/"
- l = getPackList(server , folder)
- if l == [] :
- print("There aren't wallpaper packs now")
- else :
- print("List of packs in " + server)
- for i in l :
- print(i)
- if op == 9 :
- print("Exiting...")
- time.sleep(1)
- sys.exit(0)
- try :
- if __name__ == "__main__" :
- login()
- menu()
- start()
- except KeyboardInterrupt :
- print("Exiting...")
- time.sleep(1)
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement