123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import os
- import json
- import subprocess
- def check_login():
- raw_json=""
- try:
- raw_json = str(open("files.json").read())
- except FileNotFoundError:
-
- os.system("touch fail")
- return -2
- try:
- files = json.loads(raw_json)
- except ValueError:
- print("Ok, there's a problem with your password (it's not supported). Don't worry! You can try:")
- print(" - Using an API key instead of your password (you can generate an API key on your Neocities site settings: https://neocities.org/settings/<your_username>#api_key)")
- print(" * To use an API key, use \"./push -k\" instead of just \"./push\"")
- print(" - Changing your password and avoid using characters like '!', '@' or ':'")
- print("If you tried every option listed above and this message is still shown, please report this issue: https://notabug.org/nokoru/nci/issues")
- os.system("touch fail")
- exit()
- if "error_type" in files:
- if (files["error_type"] == "invalid_auth"):
-
- os.system("touch fail")
- return -1
- else:
-
- os.system("touch fail")
- return -3
- elif "result" in files and files["result"] == "success":
-
- return 0
- else:
-
- os.system("touch fail")
- return -3
- def init_comparison():
-
- remote_files = json.loads(open("files.json").read())["files"]
- for rf in remote_files:
-
-
- if rf["path"] != "index.html" and rf["path"] != "not_found.html":
- to_delete.append(rf["path"])
-
- compare_files("public_html/",remote_files)
-
- os.system("rm -f files.json")
- def compare_files(dir,remote_files):
- for f in os.listdir(dir):
-
- if dir+f == "public_html/articles/article_template.html" or f.find('.') == 0:
- continue
-
- if os.path.isdir(dir+f):
- if (dir+f).replace("public_html/","") in to_delete:
- to_delete.remove((dir+f).replace("public_html/",""))
- compare_files(dir+f+"/",remote_files)
- continue
-
- f_sha1sum = str(subprocess.check_output(["sha1sum",dir+f])).split(" ")[0].split("b'")[1]
-
- is_new = True
-
- for rf in remote_files:
- if rf["path"] == (dir+f).replace("public_html/",""):
- is_new=False
- if rf["path"] in to_delete:
- to_delete.remove(rf["path"])
- if rf["sha1_hash"] != f_sha1sum:
- to_upload.append((dir+f).replace("public_html/",""))
- if is_new:
- to_upload.append((dir+f).replace("public_html/",""))
- def write_lists():
- os.system("touch files.upload.list")
- os.system("touch files.delete.list")
- f_upload = open("files.upload.list","w")
- for f in to_upload:
- f_upload.write(f+'\n')
- f_upload.close()
- f_delete = open("files.delete.list","w")
- for f in to_delete:
- f_delete.write(f+'\n')
- f_delete.close()
- to_upload=[]
- to_delete=[]
- login_result = check_login()
- if login_result == -1:
- os.system("rm -f files.json")
- print("Invalid username, key or password. Please check your credentials and try again. To change your username, edit the first line of the \"nci.conf\" file.")
- elif login_result == -2:
- os.system("rm -f files.json")
- print("No response file from https://neocities.org/ servers. Please try again.")
- elif login_result == -3:
- os.sysytem("rm -f files.json")
- print("Unknown error. Please check the response of 'curl https://<your_username>:<your_password>@neocities.org/api/list' if you want to know what's wrong.")
- else:
-
- print("Successfully logged in!")
- print("Comparing files...")
- init_comparison()
- print(str(to_upload.__len__()) + " file(s) will be uploaded to Neocities.")
- print(str(to_delete.__len__()) + " file(s) will be deleted from Neocities.")
- write_lists()
|