Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API to download files
#1
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
Reply
#2
(Apr-13-2018, 06:10 PM)ahantge Wrote: print r.text
#try:
j = json.loads(r.text)

So what's in the json?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Opinion: how should my scripts cache web download files? stevendaprano 0 736 Dec-17-2022, 12:19 AM
Last Post: stevendaprano
  How to download a list of files from FTP? schnarkle 0 1,018 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  Download mp4 files from an url q988988 2 6,404 Mar-07-2022, 10:11 AM
Last Post: snippsat
  download with internet download manager coral_raha 0 2,972 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  How can I download Python files from GitHub? bitcoin10mil 2 2,852 Aug-26-2020, 09:03 PM
Last Post: Axel_Erfurt
  Download multiple large json files at once halcynthis 0 2,794 Feb-14-2019, 08:41 AM
Last Post: halcynthis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020