microblog.py/neouploader.py

54 lines
1.6 KiB
Python

import sys, subprocess, getpass, pycurl, urllib.parse
if __name__ == "__main__":
def api_upload(endpoint, dest_fmt = "/microblog%s%s"):
pages = []
with open("updatedfiles.txt") as f:
pages = f.readlines()
c = pycurl.Curl()
c.setopt(c.URL, endpoint)
c.setopt(c.POST, 1)
for page in pages:
p = page.strip('\n')
i = p.rfind('/')
# folder = p[1:i]
# file = p[i]
destination = dest_fmt % (p[1:i], p[i:])
source = p[2:] # omit './'
print("sending @%s to %s" % (source, destination))
exists = True
try:
with open(source, 'r') as f:
pass
except FileNotFoundError as e:
exists = False
print(e)
if (exists):
c.setopt(c.HTTPPOST, [(destination, (c.FORM_FILE, source))])
try:
c.perform()
except pycurl.error as e:
print(e)
c.close()
def main():
if len(sys.argv) < 2:
print("Usage: neouploader.py [neocities username]")
return
try:
pw = getpass.getpass(prompt="Password: ")
except KeyboardInterrupt:
print("Aborted.")
return
finally:
if len(pw) == 0:
print("Empty input. Exiting.")
return
p = urllib.parse.quote(pw, safe='')
target = "https://%s:%s@neocities.org/api/upload" % (sys.argv[1], p)
del pw
del p
api_upload(target)
del target
main()