neouploader.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import sys, subprocess, getpass, pycurl, urllib.parse
  2. if __name__ == "__main__":
  3. def api_upload(endpoint, dest_fmt = "/microblog/%s"):
  4. pages = []
  5. with open("updatedfiles.txt") as f:
  6. pages = f.readlines()
  7. c = pycurl.Curl()
  8. c.setopt(c.URL, endpoint)
  9. c.setopt(c.POST, 1)
  10. for page in pages:
  11. p = page.strip('\n')
  12. #i = p.rfind('/')
  13. # folder = p[1:i]
  14. # file = p[i]
  15. destination = dest_fmt % p
  16. source = p
  17. print("sending @%s to %s" % (source, destination))
  18. exists = True
  19. try:
  20. with open(source, 'r') as f:
  21. pass
  22. except FileNotFoundError as e:
  23. exists = False
  24. print(e)
  25. if (exists):
  26. c.setopt(c.HTTPPOST, [(destination, (c.FORM_FILE, source))])
  27. try:
  28. c.perform()
  29. except pycurl.error as e:
  30. print(e)
  31. c.close()
  32. def main():
  33. if len(sys.argv) < 2:
  34. print("Usage: neouploader.py [neocities username]")
  35. return
  36. try:
  37. pw = getpass.getpass(prompt="Password: ")
  38. except KeyboardInterrupt:
  39. print("Aborted.")
  40. return
  41. if len(pw) == 0:
  42. print("Empty input. Exiting.")
  43. return
  44. p = urllib.parse.quote(pw, safe='')
  45. target = "https://%s:%s@neocities.org/api/upload" % (sys.argv[1], p)
  46. del pw
  47. del p
  48. api_upload(target)
  49. del target
  50. main()