json_raw = res.group(2) break return json.loads(json_raw) @staticmethod def write_data(fpath, data, delimiter=";"): with open(fpath, "a", encoding="utf8") as f: csv_writer = csv.writer(f, delimiter=delimiter) for row in data: csv_writer.writerow(row.values()) @staticmethod def parse(instance, fpath): data = ParseJavaScriptJsonToCSV.get_data(instance) ParseJavaScriptJsonToCSV.write_data(fpath, data) @staticmethod def path(srconf, jobconf, dpath): name = Box(json.loads(srconf)).name data_format = Box(json.loads(jobconf)).data_format return os.path.join(dpath, "{}.{}".format(name, data_format)) HandlersFactory.register("xlsparse_to_csv", ParseFromExcelToCSV) HandlersFactory.register("xlsparse_oked_to_csv", ParseOkedToCsv) HandlersFactory.register("web_api_raw_json_parse_to_csv", ParseFromAPIToCSV) HandlersFactory.register("web_html_javascript_json_parse_to_csv", ParseJavaScriptJsonToCSV) HandlersFactory.register("web_html_table_text_parse_to_csv", ParseGosRegisterToCSV)
i = -1 for arch in archives: arch_obj = Utils.get_archive_object(arch) data_format = Box(json.loads(instance.srconf)).storage.data_format file_path = path.abspath(path.dirname(arch)) for file in arch_obj.namelist(): if Utils.ext(file) == data_format: i += 1 arch_obj.extract(file, file_path) old_path = path.join(file_path, file).replace('/', os.sep) shutil.move(old_path, targets[i]) return targets @staticmethod def path(srconf, dpath): files_num = Box(json.loads(srconf)).storage.data_files_num archives_num = Box(json.loads(srconf)).storage.data_archives_num name = Box(json.loads(srconf)).name data_format = Box(json.loads(srconf)).storage.data_format files = list() for i in range(archives_num): for j in range(files_num): files.append( path.join(dpath, "{}_{}_{}.{}".format(name, i, j, data_format))) return files HandlersFactory.register("extract_file", ExtractorFile) HandlersFactory.register("extract_files", ExtractorFiles)
return fpath except Exception as e: raise e @staticmethod def urls(conf): url = Box(json.loads(conf)).url base_url = Box(json.loads(conf)).base_url js = requests.get(url, verify=False).text regex = Box(json.loads(conf)).storage.html.url_regexp res = re.findall(regex, js) urls = ["{}{}".format(base_url, res) for res in res] return urls @staticmethod def path(conf): name = Box(json.loads(conf)).name ext = Box(json.loads(conf)).storage.type urls = DownloaderByUrlStatGovCompanies().urls(conf) directory = TEMP_PATH os.makedirs(directory, exist_ok=True) return [ os.path.join(directory, "{}_{}.{}".format(name, i, ext)) for i, url in enumerate(urls) ] HandlersFactory.register("download_url", DownloaderByUrlToFile) HandlersFactory.register("download_urllist_ajax", DownloaderByUrlStatGovCompanies)