def downloadUnzipHydroResults(visitID, unzipPath): """ Download a topo zip file to a local path using a visitID :param visitID: visit ID :param unzipPath: Location to save model results :returns: tuple (files, projpath) WHERE dict files is api dictionary of hydro model results list projpath is list of rs.xml project path for each model result. """ tokenator = Tokenator() log = Logger("downloadHydroModelResults") # First find the appropriate download URL try: hydroFieldFolders = APIGet( 'visits/{}/fieldFolders/HydroModel'.format(visitID)) log.debug("Getting visit file data") files = hydroFieldFolders['files'] except Exception as e: raise MissingException( "ERROR: No hydro results found for visit: {}".format(visitID)) projpath = [] for file in files: downloadUrl = file['downloadUrl'] # Download the file to a temporary location with NamedTemporaryFile() as f: response = APIGet(downloadUrl, absolute=True) f.write(response.content) log.debug("Downloaded file: {} to: {}".format(downloadUrl, f.name)) # Now we have it. Unzip with zipfile.ZipFile(f, 'r') as zip_ref: unzipPathModel = os.path.join(unzipPath, f.name.rstrip(".zip")) log.debug("Unzipping file: {} to: {}".format( f.name, unzipPathModel)) zip_ref.extractall(unzipPathModel) # Return a folder where we can find a project.rs.xml (or not) for root, subFolders, files in os.walk(unzipPath): if "project.rs.xml" in files: projpath.append(root) return files, projpath
def downloadUnzipTopo(visitID, unzipPath): """ Download a topo zip file to a local path using a visitID :param visitID: :param zipFilePath: :return: """ tokenator = Tokenator() log = Logger("downloadTopoZip") # First find the appropriate download URL try: topoFieldFolders = APIGet( 'visits/{}/fieldFolders/Topo'.format(visitID)) log.debug("Getting visit file data") file = next(file for file in topoFieldFolders['files'] if file['componentTypeID'] == 181) downloadUrl = file['downloadUrl'] except Exception as e: raise MissingException( "ERROR: No TopoData.zip file found for visit: {}".format(visitID)) # Download the file to a temporary location with NamedTemporaryFile() as f: response = APIGet(downloadUrl, absolute=True) f.write(response.content) log.debug("Downloaded file: {} to: {}".format(downloadUrl, f.name)) # Now we have it. Unzip with zipfile.ZipFile(f, 'r') as zip_ref: log.debug("Unzipping file: {} to: {}".format(f.name, unzipPath)) zip_ref.extractall(unzipPath) # Return a folder where we can find a project.rs.xml (or not) projpath = None for root, subFolders, files in os.walk(unzipPath): if "project.rs.xml" in files: projpath = root return file, projpath