refactor: update fetch-scans.py

This commit is contained in:
Fam Zheng 2025-12-27 10:33:49 +00:00
parent a40d664e09
commit 5e1fe5cb5a

View File

@ -13,6 +13,7 @@ from common import *
import io
from tqdm import tqdm
import datetime
from urllib.parse import quote
data_dir = 'data'
@ -51,7 +52,7 @@ class ScanDataFetcher(object):
data = r.json()
fetch_backlog = []
for item in data['items']:
if 'code' not in item or 'id' not in item or not item.get('labels') or 'image' not in item:
if 'code' not in item or 'id' not in item or not item.get('labels') or 'image' not in item or not item.get('image'):
continue
if item['id'] in local_scan_data:
local_labels = local_scan_data[item['id']]['labels']
@ -97,12 +98,13 @@ class ScanDataFetcher(object):
metadata_path = os.path.join(scan_dir, 'metadata.json')
metadata_str = json.dumps(scan, indent=2)
frame_img_url = f'https://themblem.com/api/v1/oss-image/?token={self.token}&name={scan["image"]}'
frame_img_url = f'https://themblem.com/api/v1/oss-image/?token={self.token}&name={quote(scan["image"])}'
frame_img_file = os.path.join(scan_dir, 'frame.jpg')
if not os.path.exists(frame_img_file):
frame_img_bytes = requests.get(frame_img_url).content
r = requests.get(frame_img_url)
r.raise_for_status() # Raise an exception for bad status codes
with open(frame_img_file, 'wb') as f:
f.write(frame_img_bytes)
f.write(r.content)
std_img_file = os.path.join(scan_dir, 'std.jpg')
if not os.path.exists(std_img_file):
std_img = Image.open(io.BytesIO(get_qr_image_bytes(scan['code'])))