-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjlcpcb-data-download.py
106 lines (96 loc) · 3.37 KB
/
jlcpcb-data-download.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# this program is download the datasheet from the JLCPCB "CART" page
# the part number for URL is from the BOM file. the BOM file is read from the
# argument of the program
# the datasheet is saved in the current directory
# the datasheet file name is the same as the part number
# We can find the part number from the BOM file
# the BOM file is a csv file
# You can find the component detail from the JLCPCB "CART" page using below URL
# https://cart.jlcpcb.com/shoppingCart/smtGood/getComponentDetail?componentCode={part_number}
# author: Daekeun Kang
# date: 2023-12-27
# version: 1.0.0
import requests
import json
import csv
import sys
import os
# the current directory
current_dir = os.getcwd()
# the BOM file is read from the argument of the program
# if the argument is not provided, the program will load
# the default BOM file name. it starts with "bom" or "BOM" and ends with ".csv"
# the BOM file is a csv file
if len(sys.argv) > 1:
bom_file = sys.argv[1]
else:
# find the BOM file name in the current directory
bom_file = ""
for file in os.listdir(current_dir):
if file.lower().startswith("bom") and file.lower().endswith(".csv"):
bom_file = file
break
if bom_file == "":
print("BOM file not found")
exit()
# load the BOM file
bom = []
with open(bom_file, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
bom.append(row)
# the part number is 4th column in the BOM file
part_number = []
for row in bom:
part_number.append(row["LCSC"])
# looking for the datasheet URL from the JLCPCB "CART" page
# the part number for URL is from the BOM file
# the datasheet URL is in the "datasheet" field
for part in part_number:
url = "https://cart.jlcpcb.com/shoppingCart/smtGood/getComponentDetail?componentCode=" + part
r = requests.get(url)
data = json.loads(r.text)
# peel off the "data" layer
data = data["data"]
# the datasheet URL is in the "dataManualUrl" field
# if the field is empty, the datasheet is not found.
try:
datasheet_url = data["dataManualUrl"]
except:
pass
# if the datasheet is found, download it
if datasheet_url != "":
# get the file name from the URL
datasheet_file = datasheet_url.split("/")[-1]
if os.path.isfile(datasheet_file):
print("skip " + datasheet_file)
else:
# download the datasheet
print("download " + datasheet_file)
r = requests.get(datasheet_url)
with open(datasheet_file, 'wb') as f:
f.write(r.content)
else:
print("datasheet not found for " + part)
# now, download the images
# the image URL is in the "imageList" field
imageList = ""
try:
imageList = data["imageList"]
except:
pass
# if the image is found, download it
if imageList != "":
for image_dict in imageList:
image_url = image_dict["productBigImage"]
# get the file name from the URL
image_file = image_url.split("/")[-1]
if os.path.isfile(image_file):
print("skip " + image_file)
else:
# download the image
print("download " + image_file)
r = requests.get(image_url)
with open(image_file, 'wb') as f:
f.write(r.content)
print("done")