From 013a73e02d9c2da63e235953488cd1793a3b715e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Wed, 4 Mar 2026 10:33:48 +0100 Subject: [PATCH] small fix in api --- api.py | 38 +++++++++++--------------------------- static/js/api.js | 12 ++++++------ templates/api.html | 9 ++++++++- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/api.py b/api.py index 34fd922..b6a204e 100644 --- a/api.py +++ b/api.py @@ -439,30 +439,17 @@ def generate_raw_cidr(): } ) - if handler.needs_update(): - handler.check_and_update() - - update_progress(f'Loading data for {len(countries)} countries...', 0, 100) + update_progress('Loading networks...', 10, 100) country_networks = {} cache_sources = {} - total_countries = len(countries) - for idx, country in enumerate(countries, 1): - base_progress = int((idx - 1) / total_countries * 80) - update_progress(f'[{idx}/{total_countries}] Loading {country}...', base_progress, 100) - - networks, source = get_country_networks_cached(country, use_cache=use_cache) - - if networks: - country_networks[country] = networks + for i, country in enumerate(countries): + update_progress(f'Processing {country}...', 10 + (i * 60 // max(1, len(countries))), 100) + nets, source = get_country_networks_cached(country, use_cache=use_cache) + if nets: + country_networks[country] = nets cache_sources[country] = source - next_progress = int(idx / total_countries * 80) - update_progress( - f'[{idx}/{total_countries}] {country}: {len(networks):,} networks ({source})', - next_progress, - 100 - ) if not country_networks: clear_progress() @@ -470,12 +457,7 @@ def generate_raw_cidr(): update_progress('Generating file...', 85, 100) - if 'txt' in app_type or 'cidr' in app_type or 'newline' in app_type: - config_text = ConfigGenerator.generate_raw_cidr(country_networks, aggregate=aggregate, redis_ips=None) - filename = f"blocklist_{'_'.join(sorted(countries))}.txt" - mimetype = 'text/plain' - - elif 'json' in app_type: + if 'json' in app_type: all_networks = [] for nets in country_networks.values(): all_networks.extend(nets) @@ -507,8 +489,10 @@ def generate_raw_cidr(): mimetype = 'text/csv' else: - clear_progress() - return jsonify({'success': False, 'error': f'Unknown format: {app_type}'}), 400 + # TXT / CIDR / newline (default) + config_text = ConfigGenerator.generate_raw_cidr(country_networks, aggregate=aggregate, redis_ips=None) + filename = f"blocklist_{'_'.join(sorted(countries))}.txt" + mimetype = 'text/plain' total_networks = sum(len(nets) for nets in country_networks.values()) stats = { diff --git a/static/js/api.js b/static/js/api.js index 151cc67..ac50073 100644 --- a/static/js/api.js +++ b/static/js/api.js @@ -42,15 +42,16 @@ function cacheHeaderSummary(headers) { async function readBodyAuto(response) { const ct = (response.headers.get('content-type') || '').toLowerCase(); + const raw = await response.text(); const isJson = ct.includes('application/json'); if (isJson) { try { - return { kind: 'json', data: await response.json(), contentType: ct }; + return { kind: 'json', data: JSON.parse(raw), contentType: ct, raw }; } catch { - return { kind: 'text', data: await response.text(), contentType: ct }; + return { kind: 'text', data: raw, contentType: ct, raw }; } } - return { kind: 'text', data: await response.text(), contentType: ct }; + return { kind: 'text', data: raw, contentType: ct, raw }; } function safeParseJsonFromTextarea(textareaId) { @@ -255,7 +256,7 @@ function downloadFromApiJsonTextarea(endpoint, textareaId, fileBaseName) { const url = baseUrl + '/api/' + endpoint; fetchAsBlob(url, 'POST', body) - .then(async ({ blob, headersText, cacheSummary, contentType, contentDisposition }) => { + .then(async ({ blob, headersText, cacheSummary, contentType }) => { const ext = guessExtensionFromContentType(contentType); const filename = `${fileBaseName}.${ext}`; @@ -278,7 +279,6 @@ function downloadFromApiJsonTextarea(endpoint, textareaId, fileBaseName) { }); } - function previewTextFromGenerate(textareaId) { const { body: out } = showResponse('response-generate-download'); @@ -291,7 +291,7 @@ function previewTextFromGenerate(textareaId) { } fetchAsBlob(baseUrl + '/api/generate', 'POST', body) - .then(async ({ blob, headersText, cacheSummary, contentType }) => { + .then(async ({ blob, headersText, cacheSummary }) => { let text = ''; try { text = await blob.text(); } catch { text = '(binary content)'; } diff --git a/templates/api.html b/templates/api.html index bb7612b..ebfa2c3 100644 --- a/templates/api.html +++ b/templates/api.html @@ -465,10 +465,11 @@ raw-cidr_csvtext/csvCSV export raw-cidr_jsonapplication/json{countries, networks, count, aggregated} raw-cidr_json + as_js=trueapplication/javascriptconst <js_var> = {...}; + raw-jsonapplication/jsonalias (same schema as raw-cidr_json) -
Example JSON Schema (raw-cidr_json)
+
Example JSON Schema (raw-cidr_json / raw-json)
{
   "countries": ["SG"],
   "networks": ["1.2.3.0/24", "..."],
@@ -523,6 +524,12 @@
   -d '{"countries":["PL"],"app_type":"raw-cidr_json","aggregate":true,"use_cache":true}' \
   -o blocklist.json
+

JSON (alias raw-json):

+
curl -X POST /api/generate/raw \
+  -H "Content-Type: application/json" \
+  -d '{"countries":["CN"],"aggregate":true,"use_cache":true,"app_type":"raw-json"}' \
+  -o blocklist.cn.json
+

JS wrapped:

curl -X POST /api/generate/raw \
   -H "Content-Type: application/json" \