Python Forum
API to download files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: API to download files (/thread-9515.html)



API to download files - ahantge - Apr-13-2018

Hello,

I am a beginner at python and want to modify this API to write both a kmz file and a tiff file to my computer. Currently if it posts as kmz, it only pulls kmz.

import requests, csv, sys, os, time, json
server="https://xxxxxxxx.com" 

if len(sys.argv) == 1:
	print "ERROR: Need a .csv file\neg. python coverage.py mydata.csv"
	quit()
	
if not os.path.exists("calculations"):
		os.makedirs("calculations")

# Open CSV file
csvfile = csv.DictReader(open(sys.argv[1]))
n=0
for row in csvfile:
	# Pause script. Important otherwise server will ban you.
	time.sleep(1)
	start_time = time.time() # Stopwatch start
	#print row
	r = requests.post(server+"/API/area", data=row)
	print r.text
	#try:
	j = json.loads(r.text)
	if 'kmz' in j:
		#print j['kmz']
		r = requests.get(j['kmz'])
		fn="calculations"+os.sep+str(row['nam'])+".kmz"
		file = open(fn,"wb")
		file.write(r.content)
		file.close()
		print "Saved to %s" % fn
	if 'shp' in j:
		#print j['kmz']
		r = requests.get(j['shp'])
		fn="calculations"+os.sep+str(row['nam'])+".shp.zip"
		file = open(fn,"wb")
		file.write(r.content)
		file.close()
		print "Saved to %s" % fn


	elapsed = round(time.time() - start_time,1) # Stopwatch
	print "Elapsed: "+str(elapsed)+"s"
	n=n+1
Let me know if you can guide me. Thanks,

Andrew


RE: API to download files - nilamo - Apr-13-2018

(Apr-13-2018, 06:10 PM)ahantge Wrote: print r.text
#try:
j = json.loads(r.text)

So what's in the json?