diff options
| author | yo mama <pepper@scannerjammer.com> | 2015-02-13 01:33:31 -0800 |
|---|---|---|
| committer | yo mama <pepper@scannerjammer.com> | 2015-02-13 01:33:31 -0800 |
| commit | 4100860d10e2fb015db01d22bbf3f4735bcf10ec (patch) | |
| tree | 995b51fe3f8a6fe7fb58938c83463f3b09b5ee7c /impattern/im/cgi-bin/downloader.py | |
first
Diffstat (limited to 'impattern/im/cgi-bin/downloader.py')
| -rwxr-xr-x | impattern/im/cgi-bin/downloader.py | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/impattern/im/cgi-bin/downloader.py b/impattern/im/cgi-bin/downloader.py new file mode 100755 index 0000000..7083b11 --- /dev/null +++ b/impattern/im/cgi-bin/downloader.py @@ -0,0 +1,114 @@ +#!/usr/bin/python +import random +import urllib, urllib2 +urlopen = urllib2.urlopen +Request = urllib2.Request +import sys + +MAX_SIZE = 1024 * 1024 * 1.2 + +###___this somewhat poorly written program basically performs a get request +#you can specify three arguments, the url, the destination path and the filename#that you'll want your file to be called +#these can be specified as argument values from the commandline or as +#values in the mainfunction which is downloader.download(url,filename,destination) +#the destination and filename arguments are optional + + + +url = "" +destination = "" +filename = "" + +def argval(): + global destination + global url + global filename + stuff = sys.argv + lenstuff = len(stuff) + if lenstuff >= 4: + getvalues() + if lenstuff ==3: + print str(lenstuff) + url = sys.argv[-2] + filename = sys.argv[-1] + if lenstuff ==2: + url = sys.argv[-1] + destination = "" + partz = url.split('/') + filename = partz[-1] + if lenstuff ==1: + print 'please provide url' + return None + return url + +def getvalues(): + global url + global destination + global filename + url = sys.argv[-3] + destination = sys.argv[-1] + filename = sys.argv[-2] + if len(filename) == 0: + partz = url.split('/') + filename = partz[-1] + if filename == "" or filename == None: + partz = url.split('/') + filename = partz[-1] + return url + +def browser_request (url,data=None): + headers = { + 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', + 'Accept': '*/*', + } + try: + req = Request(url, data, headers) + response = urlopen(req) + return response + except: + print "ERROR" + sys.stdout.write('there is a problem with the url or an I/O error') + return None + sys.exit() + + +def download(url, filename="",destination=""): + response = browser_request(url) + rawimg = response.read() + if len(rawimg) == 0: + print error("got zero-length file") + sys.stdout.write("file did not exist or was zero-length") + sys.exit() + if len(rawimg) > MAX_SIZE and "asdf.us" not in url: + error = "file too big: max size " + str(MAX_SIZE/1024) + " KB / " + filename + " is " + str(len(rawimg)/1024) + " KB" + print error + sys.stdout.write(error) + sys.exit() + if filename == "": + partz = url.split('/') + filename = partz[-1] + path = "" + if destination == "" or destination == '': + path = filename + else: + path = destination+filename + f = open(path, "w") + f.write(rawimg) + print 'success' + f.close() + finalitems = {} + finalitems['url'] = url + finalitems['filename'] = filename + finalitems['destination'] = destination + return finalitems + +def downloader(): + global url + global destination + global filename + argval() + finalitems = download(url, filename, destination) + return finalitems + +if __name__ == '__main__': + downloader() |
