1- import requests
2- import json
31from django .core .management .base import BaseCommand
4- from hs_data_services import settings
52from hs_data_services_sync import tasks
3+ from hs_data_services_sync .utilities import get_list_of_public_geo_resources
64
75
86class Command (BaseCommand ):
@@ -19,7 +17,7 @@ def handle(self, *args, **options):
1917 result = self .copy_files (resource_id )
2018 print (result )
2119 else :
22- resources = self . get_list_of_public_geo_resources ()
20+ resources = get_list_of_public_geo_resources ()
2321 num_resources = len (resources )
2422 print (f"Copying files from { num_resources } resources" )
2523 counter = 1
@@ -30,36 +28,5 @@ def handle(self, *args, **options):
3028 counter += 1
3129 print ("Done copying files" )
3230
33- def get_list_of_public_geo_resources (self ):
34- hydroshare_url = "/" .join (settings .HYDROSHARE_URL .split ("/" )[:- 1 ])
35- types = ["Geographic Feature (ESRI Shapefiles)" , "Geographic Raster" ]
36- # replace spaces with + for the query string
37- types = [t .replace (" " , "+" ) for t in types ]
38- params = {
39- "type" : types ,
40- "availability" : ["public" ],
41- "geofilter" : "false"
42- }
43- rest_url = f"{ hydroshare_url } /discoverapi/?filter={ json .dumps (params )} "
44- rest_url = rest_url .replace (" " , "" )
45- print (f"Getting list of public geospatial resources from: { rest_url } " )
46- response = requests .get (rest_url )
47- response_json = response .json ()
48- page_count = response_json .get ('pagecount' , 0 )
49- rescount = response_json .get ('rescount' , 0 )
50- perpage = response_json .get ('perpage' , 0 )
51- res_ids = []
52- print (f"Iterating over { page_count } pages of { perpage } resources each, total resources: { rescount } " )
53- for i in range (1 , page_count + 1 ):
54- print (f"Getting page { i } " )
55- response = requests .get (f"{ rest_url } &pnum={ i } " )
56- response_json = response .json ()
57- resources = json .loads (response_json .get ('resources' , []))
58- new_ids = [resource ["short_id" ] for resource in resources if resource .get ('short_id' , None )]
59- res_ids .extend (new_ids )
60- print (f"Found { len (res_ids )} public geospatial resources" )
61- print (f"Should match { rescount } " )
62- return res_ids
63-
6431 def copy_files (self , resource_id ):
6532 return tasks .update_data_services_task .delay (resource_id )
0 commit comments