Python Download Automation

207
5
Jump to solution
10-22-2021 06:58 AM
Labels (2)
DaveTenney
Regular Contributor

I need assistance with an interesting issue im experiencing when i try to automate the download process from agol to local drive. Im utilizing python3 to automate the download process, i can manually log in and manually download the fgdb of the data on agol. Once i attempt to do the same steps with the python script it fails when it attempts to download the data with an error code 403.

 

here is my script:

 

from time import strftime
print(strftime('%c'))

from arcgis.gis import GIS

#variables#
itemid = 'itemid number goes here'
output = r'C:\Temp\AGOLtest'
tempfile = strftime('Test_%m_%d_%y')

#variables for testing#
##itemid = 'testing itemid goes here'
##output = r'C:\Temp\AGOLtest'
##tempfile = strftime('Test_%m_%d_%y')

#file pull#
gis = GIS('URL', 'un', 'pw')
print(gis)
print('access to agol successful')
arcgisitem = gis.content.get(itemid)
print(arcgisitem)
print('access to agol item successful')
print(tempfile)
arcgisitem.export(tempfile, 'File Geodatabase', parameters=None, wait=True)
print(tempfile)
print('item export to agol complete')
myexport = gis.content.search(tempfile, item_type='File Geodatabase')
print('search for agol content item complete')
print(myexport)
print(myexport[0])
print(myexport[0].itemid)
fgdb = gis.content.get(myexport[0].itemid)
print('found fgdb export on agol content page')
fgdb.download(save_path=output)
print('export downloaded to folder location')
fgdb.delete()
print('export has been deleted from agol content page')

print('Script completed at {}'.format(strftime('%c')))

0 Kudos
1 Solution

Accepted Solutions
ABishop
Regular Contributor

@DaveTenney 

Are you using ArcGIS Pro?  Version?

Reason I ask is because they have unpacked functionality in the feature class to feature class tool which recognizes the transfer of feature or web map services to a local file.  I have provided a copy of a script that I used to download zoning data on a monthly basis.  The beginning of my script includes deleting old shapefiles, but its not necessary. Hope this helps.

#import arcpy module
import arcpy, os, sys
from arcpy import env
env.workspace = r"W:\MiscLayerData\Parcel\Monthly updates"
env.overwriteOutput = True

#start timer
startTime = time.perf_counter()

#Delete old CityZONING and CountyZONING shapefiles from K\GIS_UPDATE\MONTHLY folder
arcpy.management.Delete(r"K:\GIS_UPDATE\MONTHLY\CountyZONING.shp;K:\GIS_UPDATE\MONTHLY\CityZONING.shp", '')
print("old zoning data deleted from K:\GIS_UPDATE\MONTHLY folder")

#Download and copy over the City's Zoning data with ZONECLASS field to Monthly.gdb
arcpy.conversion.FeatureClassToFeatureClass("https://gis.ocalafl.org/arcgis/rest/services/OneMap/GrowthManagement/FeatureServer/7", r"K:\GIS_UPDATE\DATA\Monthly.gdb", "CityZONING", '', 'ZONECLASS "Zoning Classification" true true false 10 Text 0 0,First,#,https://gis.ocalafl.org/arcgis/rest/services/OneMap/GrowthManagement/FeatureServer/7,ZONECLASS,0,10', '')
print("City of Ocala Zoning Downloaded and copied to CityZONING in Monthly.gdb")

#Download and copy over the County's Zoning data with ZONECLASS field to Monthly.gdb
arcpy.conversion.FeatureClassToFeatureClass("https://gis.marionfl.org/public/rest/services/App/ZoningDistricts_PA/FeatureServer/0", r"K:\GIS_UPDATE\DATA\Monthly.gdb", "CountyZONING", '', 'ZONECLASS "Zoning Classification" true true false 10 Text 0 0,First,#,https://gis.marionfl.org/public/rest/services/App/ZoningDistricts_PA/FeatureServer/0,ZONECLASS,0,10', '')
print("County Zoning Downloaded and copied to CountyZONING in Monthly.gdb")
print("Monthly Zoning update completed")

#end timer
endTime = time.perf_counter()
elapsedTime = round((endTime - startTime) / 60, 2)
print("Script finished in {0} minutes".format(elapsedTime))

#provide input to stop script - uncomment input below if you need to see the script final output in the command window
input('Press ENTER to exit')

 

Amanda Bishop, GISP

View solution in original post

0 Kudos
5 Replies
ABishop
Regular Contributor

@DaveTenney 

Are you using ArcGIS Pro?  Version?

Reason I ask is because they have unpacked functionality in the feature class to feature class tool which recognizes the transfer of feature or web map services to a local file.  I have provided a copy of a script that I used to download zoning data on a monthly basis.  The beginning of my script includes deleting old shapefiles, but its not necessary. Hope this helps.

#import arcpy module
import arcpy, os, sys
from arcpy import env
env.workspace = r"W:\MiscLayerData\Parcel\Monthly updates"
env.overwriteOutput = True

#start timer
startTime = time.perf_counter()

#Delete old CityZONING and CountyZONING shapefiles from K\GIS_UPDATE\MONTHLY folder
arcpy.management.Delete(r"K:\GIS_UPDATE\MONTHLY\CountyZONING.shp;K:\GIS_UPDATE\MONTHLY\CityZONING.shp", '')
print("old zoning data deleted from K:\GIS_UPDATE\MONTHLY folder")

#Download and copy over the City's Zoning data with ZONECLASS field to Monthly.gdb
arcpy.conversion.FeatureClassToFeatureClass("https://gis.ocalafl.org/arcgis/rest/services/OneMap/GrowthManagement/FeatureServer/7", r"K:\GIS_UPDATE\DATA\Monthly.gdb", "CityZONING", '', 'ZONECLASS "Zoning Classification" true true false 10 Text 0 0,First,#,https://gis.ocalafl.org/arcgis/rest/services/OneMap/GrowthManagement/FeatureServer/7,ZONECLASS,0,10', '')
print("City of Ocala Zoning Downloaded and copied to CityZONING in Monthly.gdb")

#Download and copy over the County's Zoning data with ZONECLASS field to Monthly.gdb
arcpy.conversion.FeatureClassToFeatureClass("https://gis.marionfl.org/public/rest/services/App/ZoningDistricts_PA/FeatureServer/0", r"K:\GIS_UPDATE\DATA\Monthly.gdb", "CountyZONING", '', 'ZONECLASS "Zoning Classification" true true false 10 Text 0 0,First,#,https://gis.marionfl.org/public/rest/services/App/ZoningDistricts_PA/FeatureServer/0,ZONECLASS,0,10', '')
print("County Zoning Downloaded and copied to CountyZONING in Monthly.gdb")
print("Monthly Zoning update completed")

#end timer
endTime = time.perf_counter()
elapsedTime = round((endTime - startTime) / 60, 2)
print("Script finished in {0} minutes".format(elapsedTime))

#provide input to stop script - uncomment input below if you need to see the script final output in the command window
input('Press ENTER to exit')

 

Amanda Bishop, GISP

View solution in original post

0 Kudos
DaveTenney
Regular Contributor

yes, i am calling on the instance of python from ArcGIS Pro.

0 Kudos
ABishop
Regular Contributor

Try this method... it works like a charm and you don't need credentials as long as you run it from a PC where you are logged into your account.

Amanda Bishop, GISP
0 Kudos
DaveTenney
Regular Contributor

@ABishop - thank you so much that did the job! 

thanks!

Dave

ABishop
Regular Contributor

@DaveTenney 

You are very welcomed!  So happy it worked out for you!

Amanda Bishop, GISP
0 Kudos