Advertisement
ijontichy

getwad.py

Dec 24th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import io, os, sys, zipfile
  4. from urllib.request import urlopen
  5. from urllib.error   import HTTPError
  6.  
  7. BASENAME = os.path.basename(sys.argv[0])
  8. USAGE = "{} <pk3> [dest]".format(BASENAME)
  9.  
  10. def errorExit(reason):
  11.     print("{}: error: {}".format(BASENAME, reason))
  12.     sys.exit(1)
  13.  
  14. def usageExit(reason):
  15.     print("{}: error: {}".format(BASENAME, reason))
  16.     print(USAGE)
  17.     sys.exit(128)
  18.  
  19.  
  20. def main(file, dest):
  21.     try:
  22.         url = urlopen("http://wadhost.fathax.com/files/{}".format(file))
  23.     except HTTPError:
  24.         print("\"{}\" could not be retrieved".format(file), file=sys.stderr)
  25.         return
  26.    
  27.     if file.endswith(".zip"):
  28.         urlB = io.BytesIO(url.read())
  29.         z    = zipfile.ZipFile(urlB)
  30.        
  31.         print("files in \"{}\": {}".format(file, " ".join(z.namelist())))
  32.  
  33.         z.extractall(dest)
  34.     else:
  35.        print("hi")
  36.        end = open(file, "wb").write(url.read())
  37.  
  38. if __name__ == "__main__":
  39.     if len(sys.argv) < 2:
  40.        
  41.         while True:
  42.             try:
  43.                 file = input()
  44.             except EOFError:
  45.                 break
  46.  
  47.             if file: main(file, ".")
  48.     else:
  49.         file = sys.argv[1]
  50.         dest = sys.argv[2:3] or "."
  51.  
  52.         main(file, dest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement