Advertisement
MRtecno98

SfondiPaper Server Management Tool RELEASE V.3.2.0

Dec 29th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.08 KB | None | 0 0
  1. from techcore import ftp
  2. import os , getpass , requests , json , time , sys
  3.  
  4. server = ""
  5. user = ""
  6. psw = ""
  7.  
  8. def checkCategory(category , server , folder) :
  9.     obj = getCategoriesList(server , folder)
  10.     return category in list(obj.keys())
  11.  
  12. def checkImage(image , category , server , folder) :
  13.     imgs = getImageList(category , server , folder)
  14.     return image in imgs
  15.  
  16. def getImageList(category , server , folder) :
  17.     if checkCategory(category, server , folder) :
  18.         cts = getCategoriesList(server , folder)
  19.         request = requests.get(server + folder + cts[category] + "/list.json")
  20.         request.raise_for_status()
  21.         obj = json.loads(request.text)
  22.         return obj
  23.  
  24. def checkExit(x) :
  25.     return False #x == "abort"
  26.  
  27. def getPackList(server , folder) :
  28.     request = requests.get(server + folder + "list.json")
  29.     request.raise_for_status()
  30.     obj = json.loads(request.text)
  31.     return obj
  32.  
  33. def checkPack(pack , server , folder) :
  34.     l = getPackList(server , folder)
  35.     return pack in l
  36.  
  37. def getCategoriesList(server , folder) :
  38.     request = requests.get(server + folder + "categories.json")
  39.     request.raise_for_status()
  40.     obj = json.loads(request.text)
  41.     return obj
  42.  
  43. def newInput(message) :
  44.     t = newInput(message)
  45.     if checkExit(t) :
  46.         raise ImportError()
  47.     else :
  48.         return t
  49. newInput , input = input , newInput
  50.  
  51. def testConnection() :
  52.     global server,user,psw
  53.     ftp.FTP(host=server[7:] , user=user , passwd=psw)
  54.     return
  55.  
  56. def login() :
  57.     global server,user,psw
  58.     temps = input("Insert server: ")
  59.     if not temps.startswith("http://") :
  60.         temps = "http://" + temps
  61.     server = temps
  62.     print()
  63.     user = input("login as: ")
  64.     psw = getpass.getpass("password: ")
  65.     testConnection()
  66.     print("Connection Started!\n")
  67.     return
  68.  
  69. def menu() :
  70.     print("SfondiPaper Server Management Tool [V.3.0.0]")
  71.     print("Select option:")
  72.     print("1) Add Image")
  73.     print("2) Remove Image")
  74.     print("3) List Images")
  75.     print("4) Set Wallpaper of the Month")
  76.     print("5) Add Wallpaper Pack")
  77.     print("6) Remove Wallpaper Pack")
  78.     print("7) List Wallapaper Packs")
  79.     print("9) Exit")
  80.  
  81. def start() :
  82.     global server,user,psw
  83.     while True :
  84.         op = None
  85.         while op == None :
  86.             try :
  87.                 op = int(input("\nOption: "))
  88.             except ValueError :
  89.                 print("Inserisci un numero!")
  90.         if op == 1 :
  91.             path = ""
  92.             while True :
  93.                 path = input("Insert path of the image: ")
  94.                 checkExit(path)
  95.                 if not os.path.isfile(path) or not (path.endswith(".png") or path.endswith(".jpg")) :
  96.                     print("Insert valid path for a PNG/JPG image!")
  97.                     continue
  98.                 break
  99.             folder = "/categories/"
  100.             category = ""
  101.             while not checkCategory(category , server , folder) :
  102.                 category = input("Insert category: ")
  103.             #print(server,user,psw)
  104.             conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
  105.             print("Connected! Uploading Image...")
  106.             filename = ""
  107.             cts = getCategoriesList(server , folder)
  108.             rawCategory = cts[category]
  109.             with open(path , "rb") as file :
  110.                 filename = file.name.split("\\")[-1]
  111.             ftp.uploadFile(conn , file , folder + rawCategory)
  112.             print("Upload complete! Updating JSON files...")
  113.             os.makedirs("work" , exist_ok=True)
  114.             os.chdir("work")
  115.             request = requests.get(server + folder + "all.json")
  116.             request.raise_for_status()
  117.             if request.text == "" :
  118.                 obj = {}
  119.             else :
  120.                 obj = json.loads(request.text)
  121.             obj[category].append(filename)
  122.             text = json.dumps(obj)
  123.             with open("all.json" , "w") as file :
  124.                 file.write(text)
  125.             with open("all.json" , "rb") as file :
  126.                 ftp.uploadFile(conn , file , folder)
  127.             print("General JSON file updated, updating category-based file...")
  128.             request = requests.get(server + folder + rawCategory + "/list.json")
  129.             request.raise_for_status()
  130.             if request.text == "" :
  131.                 obj = []
  132.             else :
  133.                 obj = json.loads(request.text)
  134.             obj.append(filename)
  135.             text = json.dumps(obj)
  136.             with open("list.json" , "w") as file :
  137.                 file.write(text)
  138.             with open("list.json" , "rb") as file :
  139.                 ftp.uploadFile(conn , file , folder + rawCategory)
  140.             print("All files updated! Closing connection...")
  141.             conn.quit()
  142.             print("Connection closed, operations finished!")
  143.  
  144.         if op == 2 :
  145.             folder = "/categories/"
  146.             category = ""
  147.             while not checkCategory(category , server , folder) :
  148.                 category = input("Insert category: ")
  149.             img = ""
  150.             while not checkImage(img , category , server , folder) :
  151.                 img = input("Insert Image(With extension .png/.jpg): ")
  152.                
  153.             conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
  154.             print("Connected! Removing Image...")
  155.  
  156.             cts = getCategoriesList(server , folder)
  157.             ftp.deleteFile(conn , folder + cts[category] + "/" + img)
  158.             print("Image deleted! Updating JSON Files")
  159.    
  160.             os.makedirs("work" , exist_ok=True)
  161.             os.chdir("work")
  162.             request = requests.get(server + folder + "all.json")
  163.             request.raise_for_status()
  164.             if request.text == "" :
  165.                 obj = {}
  166.             else :
  167.                 obj = json.loads(request.text)
  168.             obj[category].remove(img)
  169.             text = json.dumps(obj)
  170.             with open("all.json" , "w") as file :
  171.                 file.write(text)
  172.             with open("all.json" , "rb") as file :
  173.                 ftp.uploadFile(conn , file , folder)
  174.             print("General JSON file updated, updating category-based file...")
  175.             request = requests.get(server + folder + rawCategory + "/list.json")
  176.             request.raise_for_status()
  177.             if request.text == "" :
  178.                 obj = []
  179.             else :
  180.                 obj = json.loads(request.text)
  181.             obj.remove(image)
  182.             text = json.dumps(obj)
  183.             with open("list.json" , "w") as file :
  184.                 file.write(text)
  185.             with open("list.json" , "rb") as file :
  186.                 ftp.uploadFile(conn , file , folder + rawCategory)
  187.             print("All files updated! Closing connection...")
  188.             conn.quit()
  189.             print("Connection closed, operations finished!")
  190.         if op == 3 :
  191.             folder = "/categories/"
  192.             category = ""
  193.             while not checkCategory(category , server , folder) :
  194.                 category = input("Insert category: ")
  195.             print("Retrieving images...")
  196.             obj = getImageList(category , server , folder)
  197.             for o in obj :
  198.                 print("Image: " + o)
  199.  
  200.         if op == 4 :
  201.             folder = "/categories/"
  202.             category = ""
  203.             while not checkCategory(category , server , folder) :
  204.                 category = input("Insert category: ")
  205.             img = ""
  206.             while not checkImage(img , category , server , folder) :
  207.                 img = input("Insert Image(With extension .png/.jpg): ")
  208.             print("Updating JSON File...")
  209.             #print(server , user , psw)
  210.             conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
  211.             obj = {}
  212.             obj[category] = img
  213.             text = json.dumps(obj)
  214.             with open("month.json" , "w") as file :
  215.                 file.write(text)
  216.             with open("month.json" , "rb") as file :
  217.                 ftp.uploadFile(conn , file , folder)
  218.             print("All files updated! Closing connection...")
  219.             conn.quit()
  220.             print("Connection closed, operations finished!")
  221.  
  222.         if op == 5 :
  223.             folder = "/packs/"
  224.             path = ""
  225.             while not (path.endswith(".zip") and os.path.isfile(path)) :
  226.                 path = input("Insert path of zip file: ")
  227.             conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
  228.             print("Connected! Uploading zip pack...")
  229.             with open(path , "rb") as file :
  230.                 ftp.uploadFile(conn , file , folder)
  231.             print("Upload complete! Updating JSON Files...")
  232.        
  233.             os.makedirs("work" , exist_ok=True)
  234.             os.chdir("work")
  235.             request = requests.get(server + folder + "list.json")
  236.             request.raise_for_status()
  237.             if request.text == "" :
  238.                 obj = []
  239.             else :
  240.                 obj = json.loads(request.text)
  241.             obj.append(os.path.basename(path))
  242.             text = json.dumps(obj)
  243.             with open("list.json" , "w") as file :
  244.                 file.write(text)
  245.             with open("list.json" , "rb") as file :
  246.                 ftp.uploadFile(conn , file , folder)
  247.             print("All files updated! Closing connection...")
  248.             conn.quit()
  249.             print("Connection closed, operations finished!")
  250.  
  251.         if op == 6 :
  252.             folder = "/packs/"
  253.             name = ""
  254.             while not checkPack(name , server , folder) :
  255.                 name = input("Insert name of pack to delete: ")
  256.             conn = ftp.FTP(host=server[7:], user=user, passwd=psw)
  257.             print("Connected! Deleting zip pack...")
  258.             ftp.deleteFile(conn , folder + name)
  259.             print("Zip file deleted, updating JSON Files...")
  260.             os.makedirs("work" , exist_ok=True)
  261.             os.chdir("work")
  262.             request = requests.get(server + folder + "list.json")
  263.             request.raise_for_status()
  264.             if request.text == "" :
  265.                 obj = [name]
  266.             else :
  267.                 obj = json.loads(request.text)
  268.             obj.remove(name)
  269.             text = json.dumps(obj)
  270.             with open("list.json" , "w") as file :
  271.                 file.write(text)
  272.             with open("list.json" , "rb") as file :
  273.                 ftp.uploadFile(conn , file , folder)
  274.             print("All files updated! Closing connection...")
  275.             conn.quit()
  276.             print("Connection closed, operations finished!")
  277.  
  278.         if op == 7 :
  279.             folder = "/packs/"
  280.             l = getPackList(server , folder)
  281.             if l == [] :
  282.                 print("There aren't wallpaper packs now")
  283.             else :
  284.                 print("List of packs in " + server)
  285.                 for i in l :
  286.                     print(i)
  287.  
  288.         if op == 9 :
  289.             print("Exiting...")
  290.             time.sleep(1)
  291.             sys.exit(0)
  292.  
  293. try :
  294.     if __name__ == "__main__" :
  295.         login()
  296.         menu()
  297.         start()
  298. except KeyboardInterrupt :
  299.     print("Exiting...")
  300.     time.sleep(1)
  301.     sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement