summaryrefslogtreecommitdiff
path: root/im/cgi-bin/downloader.py
blob: 7083b1137b1f594f4c561401263d26fd4fd1598d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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()