-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_10.py
More file actions
67 lines (55 loc) · 2.43 KB
/
Copy pathdemo_10.py
File metadata and controls
67 lines (55 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Run enrichment on first 10 rows, output accepted + rejected CSVs."""
import sys
sys.modules.pop("enrich_books", None)
from enrich_books import (
EXCEL_FILE, SHOPIFY_HEADERS, REJECTED_HEADERS, clean_text,
fetch_open_library, verify_image, build_shopify_row, API_DELAY
)
import openpyxl, csv, time
OUTPUT = "demo_10_shopify.csv"
REJECTED = "demo_10_rejected.csv"
LIMIT = 10
wb = openpyxl.load_workbook(EXCEL_FILE, read_only=True, data_only=True)
ws = wb["Sheet1"]
rows = list(ws.iter_rows(min_row=2, max_row=LIMIT + 1, values_only=True))
wb.close()
with open(OUTPUT, "w", newline="", encoding="utf-8-sig") as mf, \
open(REJECTED, "w", newline="", encoding="utf-8-sig") as rf:
writer = csv.DictWriter(mf, fieldnames=SHOPIFY_HEADERS)
rej_writer = csv.DictWriter(rf, fieldnames=REJECTED_HEADERS)
writer.writeheader()
rej_writer.writeheader()
accepted = rejected = 0
for i, row in enumerate(rows):
(isbn_raw, name, author, publisher,
binding, language, currency, price, stock, location) = row
if not isbn_raw:
continue
isbn = str(int(isbn_raw))
excel = {
"name": name or "", "AUTHOR1": author or "",
"PUBLISHER": publisher or "", "BINDINGTYPE": binding or "",
"LANGUAGE": language or "", "price": price or 0, "stock": stock or 0,
}
api = fetch_open_library(isbn)
time.sleep(API_DELAY)
img = verify_image(api.get("image", ""), isbn)
row_data = build_shopify_row(isbn, excel, api, verified_image=img)
if row_data["Image Src"]:
writer.writerow(row_data)
accepted += 1
status = "YES"
else:
rej_writer.writerow({
"ISBN": isbn, "Original Title": clean_text(name or ""),
"Author": clean_text(author or ""), "Publisher": clean_text(publisher or ""),
"Binding": clean_text(binding or ""), "Language": clean_text(language or ""),
"Price": str(price or ""), "Stock": str(int(stock)) if stock else "0",
"Reason": "No verified image on Open Library",
})
rejected += 1
status = "NO"
src = "OL" if api.get("title") else "Excel"
print(f"[{i+1:02d}] img={status} | {src} | {row_data['Title'][:50]:<50} | ${row_data['Variant Price']}")
print(f"\nAccepted: {accepted} -> {OUTPUT}")
print(f"Rejected: {rejected} -> {REJECTED}")