neouploader.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import sys, os, subprocess, getpass, pycurl, urllib.parse
  2. if __name__ == "__main__":
  3. def get_proxy():
  4. proxy = ""
  5. if "http_proxy" in os.environ:
  6. proxy = os.environ["http_proxy"]
  7. elif "https_proxy" in os.environ:
  8. proxy = os.environ["https_proxy"]
  9. host = proxy[proxy.rfind('/') + 1: proxy.rfind(':')]
  10. port = proxy[proxy.rfind(':') + 1:]
  11. foo = proxy.find("socks://") >= 0 or proxy.find("socks5h://")
  12. return host, int(port), foo
  13. def api_upload(endpoint, dest_fmt = "/microblog/%s"):
  14. pages = []
  15. with open("updatedfiles.txt") as f:
  16. pages = f.readlines()
  17. c = pycurl.Curl()
  18. if "http_proxy" in os.environ or "https_proxy" in os.environ:
  19. hostname, port_no, is_socks = get_proxy()
  20. c.setopt(pycurl.PROXY, hostname)
  21. c.setopt(pycurl.PROXYPORT, port_no)
  22. if is_socks:
  23. c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME)
  24. c.setopt(c.URL, endpoint)
  25. c.setopt(c.POST, 1)
  26. for page in pages:
  27. p = page.strip('\n')
  28. #i = p.rfind('/')
  29. # folder = p[1:i]
  30. # file = p[i]
  31. destination = dest_fmt % p
  32. source = p
  33. print("sending @%s to %s" % (source, destination))
  34. exists = True
  35. try:
  36. with open(source, 'r') as f:
  37. pass
  38. except FileNotFoundError as e:
  39. exists = False
  40. print(e)
  41. if (exists):
  42. c.setopt(c.HTTPPOST, [(destination, (c.FORM_FILE, source))])
  43. try:
  44. c.perform()
  45. except pycurl.error as e:
  46. print(e)
  47. c.close()
  48. def main():
  49. if len(sys.argv) < 2:
  50. print("Usage: neouploader.py [neocities username]")
  51. return
  52. try:
  53. pw = getpass.getpass(prompt="Password: ")
  54. except KeyboardInterrupt:
  55. print("Aborted.")
  56. return
  57. if len(pw) == 0:
  58. print("Empty input. Exiting.")
  59. return
  60. p = urllib.parse.quote(pw, safe='')
  61. target = "https://%s:%s@neocities.org/api/upload" % (sys.argv[1], p)
  62. del pw
  63. del p
  64. api_upload(target)
  65. del target
  66. main()