def download_from_url(url, output_directory, filename=None, use_cache=True): """Download file from a url and put it under output_directory. :param url: Url that gives response. :type url: str :param output_directory: Directory to put the diagram. :type output_directory: str :param filename: Optional filename for downloaded file. :type filename: str :param use_cache: If there is a cached copy of the file already in the output directory, do not refetch it (True) or force refecth it (False). :type use_cache: bool :returns: File path if success to download, else None :rtype: str """ if filename is None: filename = get_filename(url) LOGGER.info('Download file %s from %s' % (filename, url)) file_path = os.path.join(output_directory, filename) if os.path.exists(file_path) and use_cache: LOGGER.info('File %s exists, not downloading' % file_path) return file_path # Set Proxy in webpage proxy = get_proxy() network_manager = QNetworkAccessManager() if not proxy is None: network_manager.setProxy(proxy) # Download Process # noinspection PyTypeChecker downloader = FileDownloader(network_manager, url, file_path) try: result = downloader.download() except IOError as ex: raise DownloadException(ex) if result[0] is not True: _, error_message = result raise DownloadException(error_message) if os.path.exists(file_path): return file_path else: return None
def construct_url(db_manager, sg_code=None, province_name=None): """Construct url to download sg diagram. :param db_manager: A database manager :type db_manager: DatabaseManager :param sg_code: SG code. :type sg_code: str :param province_name: province_name name. :type province_name: str :returns: URL to download sg diagram. :rtype: str """ LOGGER.info('Constructing url for %s %s' % (sg_code, province_name)) if not is_valid_sg_code(sg_code): raise InvalidSGCodeException if sg_code is None or province_name is None: raise UrlException() if province_name not in PROVINCE_NAMES: raise NotInSouthAfricaException base_url = 'http://csg.dla.gov.za/esio/listdocument.jsp?' reg_division = sg_code[:8] try: record = get_office(db_manager, reg_division, province_name) except DatabaseException: raise DatabaseException if record is None or bool(record) is None: raise DatabaseException office, office_number, typology = record erf = sg_code[8:16] portion = sg_code[16:] url = base_url + 'regDivision=' + reg_division url += '&office=' + office url += '&Noffice=' + office_number url += '&Erf=' + erf url += '&Portion=' + portion return url