-
Notifications
You must be signed in to change notification settings - Fork 0
/
superMakeMinifigLabels.py
executable file
·397 lines (337 loc) · 11.8 KB
/
superMakeMinifigLabels.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
import os
import re
import sys
import time
import random
import shutil # to save it locally
import requests # to get image from the web
import libbrick
import bricklink_wrapper
latex_header = r"""
\documentclass[letterpaper]{article}% Avery 18260
% https://www.avery.com/blank/labels/94200
\usepackage[top=0.5in, bottom=0.4in, left=0.28in, right=0.14in, noheadfoot]{geometry}
\usepackage{varwidth}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{fontspec}
\setsansfont[Ligatures=TeX]{PT Sans Narrow}
\definecolor{DarkBlue}{RGB}{0,0,100}
\newenvironment{legocell}[1]
{
\begin{minipage}[c][0.98in][c]{2.625in}
\centering
% Avery 18260 described as 1in by 2 5/8in
\varwidth{2.5in}
\raggedright % but measures 2.75in wide
\begin{minipage}[c]{0.5in}
\includegraphics[width=0.45in,
height=0.65in,
keepaspectratio,]{#1}
\end{minipage}
\begin{minipage}[c]{1.9in}
\raggedright
}
{
\end{minipage}
\endvarwidth
\end{minipage}
\allowbreak
\ignorespaces
}
\parindent=0pt
\pagestyle{empty}
\begin{document}
\small
% set font size
"""
latex_footer = r"\end{document}\n"
#============================
#============================
def downloadImage(image_url, filename=None):
if image_url is None:
return None
# Set up the image URL and filename
if filename is None:
filename = os.path.basename(image_url)
if os.path.exists(filename):
return filename
time.sleep(random.random())
# Open the url image, set stream to True, this will return the stream content.
r = requests.get(image_url, stream=True)
# Check if the image was retrieved successfully
if r.status_code == 200:
# Set decode_content value to True, otherwise the downloaded image file's size will be zero.
r.raw.decode_content = True
# Open a local file with wb ( write binary ) permission.
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
print('.. image successfully downloaded: ', filename)
else:
print(f"!! image couldn't be retrieved: {image_url}")
return filename
#============================
def makeLabel(minifig_dict: dict, price_dict: dict) -> str:
"""
Generates a LaTeX string for a given minifigure and its price details.
Args:
minifig_dict (dict): Dictionary containing minifigure details.
price_dict (dict): Dictionary containing price details.
Returns:
str: LaTeX formatted string for the minifigure.
"""
print(minifig_dict)
set_num = get_set_num(minifig_dict)
minifig_id = minifig_dict.get('minifig_id')
print(f'-----\nProcessing Minifig {minifig_id} from Set {set_num}')
ensure_images_directory()
filename = download_minifig_image(minifig_dict, minifig_id)
minifig_name = format_minifig_name(minifig_dict.get('name'))
name_fontsize = determine_fontsize(minifig_name)
time_str = time.strftime("%b %Y", time.gmtime())
latex_str = create_latex_string(minifig_id, set_num, minifig_dict, filename, name_fontsize, minifig_name, time_str, price_dict)
print(f'{minifig_id} -- {set_num} ({minifig_dict.get("year_released")}) -- {minifig_dict.get("name")[:60]}')
return latex_str
#============================
def get_set_num(minifig_dict: dict) -> str:
"""
Extracts the set number from the minifig dictionary.
Args:
minifig_dict (dict): Dictionary containing minifigure details.
Returns:
str: Set number.
"""
set_num = minifig_dict.get('set_num')
if set_num is None:
set_id = minifig_dict.get('set_id')
if set_id is not None:
set_num = set_id.split('-')[0]
return set_num
#============================
def ensure_images_directory() -> None:
"""
Creates an 'images' directory if it doesn't exist.
"""
if not os.path.isdir('images'):
os.mkdir('images')
#============================
def download_minifig_image(minifig_dict: dict, minifig_id: str) -> str:
"""
Downloads the minifigure image and returns the filename.
Args:
minifig_dict (dict): Dictionary containing minifigure details.
minifig_id (str): ID of the minifigure.
Returns:
str: Filename where the image is saved.
"""
filename = f"images/minifig_{minifig_id}.jpg"
image_url = minifig_dict.get('image_url')
image_url = 'https:' + image_url
downloadImage(image_url, filename)
return filename
#============================
def format_minifig_name(name: str) -> str:
"""
Formats the minifigure name by removing unwanted characters and truncating if necessary.
Args:
name (str): Original minifigure name.
Returns:
str: Formatted minifigure name.
"""
name = name.replace('#', '')
name = re.sub(r'\([^\)]+\)', '', name)
if len(name) > 64:
new_name = ''
bits = name.split(' ')
i = 0
while len(new_name) < 58:
new_name += bits[i] + ' '
i += 1
name = new_name.strip()
return name
#============================
def determine_fontsize(name: str) -> str:
"""
Determines the LaTeX font size based on the length of the minifigure name.
Args:
name (str): Formatted minifigure name.
Returns:
str: LaTeX font size command.
"""
if len(name) < 18:
return '\\normalsize'
elif len(name) < 26:
return '\\small'
elif len(name) < 34:
return '\\footnotesize'
elif len(name) < 50:
return '\\scriptsize'
else:
return '\\tiny'
#============================
def create_latex_string(minifig_id: str, set_num: str, minifig_dict: dict, filename: str, name_fontsize: str, name: str, time_str: str, price_dict: dict) -> str:
"""
Creates the LaTeX formatted string for the minifigure.
Args:
minifig_id (str): ID of the minifigure.
set_num (str): Set number of the minifigure.
minifig_dict (dict): Dictionary containing minifigure details.
filename (str): Filename where the image is saved.
name_fontsize (str): LaTeX font size command for the minifigure name.
name (str): Formatted minifigure name.
time_str (str): Current time formatted as a string.
price_dict (dict): Dictionary containing price details.
Returns:
str: LaTeX formatted string for the minifigure.
"""
latex_str = '\\begin{legocell}{' + filename + '}\n'
latex_str += '\\textbf{\\color{DarkBlue}'
if len(minifig_id) < 10:
latex_str += '\\LARGE ' + str(minifig_id)
else:
latex_str += '\\large ' + str(minifig_id)
latex_str += '} {\\sffamily\\tiny ' + time_str + '}\\par \n'
latex_str += '\\vspace{-1pt} '
if set_num is not None and len(set_num) > 3:
latex_str += '{\\tiny \\> from set \\textbf{ ' + str(set_num) + ' } \\footnotesize (' + str(minifig_dict.get('year_released')) + ')}\\par \n'
else:
latex_str += '{\\footnotesize \\> release year: ' + str(minifig_dict.get('year_released')) + '}\\par \n'
latex_str += '{\\sffamily' + name_fontsize + ' ' + name + '}\\par \n'
latex_str += format_sales_info(price_dict)
latex_str += format_list_info(price_dict)
latex_str += '\\end{legocell}\n'
return latex_str
#============================
def format_sales_info(price_dict: dict) -> str:
"""
Formats the sales information for the minifigure.
Args:
price_dict (dict): Dictionary containing price details.
Returns:
str: LaTeX formatted sales information.
"""
new_median_sale_price = float(price_dict['new_median_sale_price'])
used_median_sale_price = float(price_dict['used_median_sale_price'])
if new_median_sale_price > 0 or used_median_sale_price > 0:
new_sale_qty = int(price_dict['new_sale_qty'])
used_sale_qty = int(price_dict['used_sale_qty'])
latex_str = '\\vspace{-3pt} {\\sffamily\\tiny sale: '
if new_median_sale_price > 0 and used_median_sale_price > 0:
latex_str += fr'\${new_median_sale_price/100:.2f} new ({new_sale_qty}) / \${used_median_sale_price/100:.2f} used ({used_sale_qty})'
elif new_sale_qty > 0 and new_median_sale_price > 0:
latex_str += fr'\${new_median_sale_price/100:.2f} new ({new_sale_qty})'
elif used_sale_qty > 0 and used_median_sale_price > 0:
latex_str += fr'\${used_median_sale_price/100:.2f} used ({used_sale_qty})'
latex_str += '}\\\\\n'
return latex_str
return ''
#============================
def format_list_info(price_dict: dict) -> str:
"""
Formats the listing information for the minifigure.
Args:
price_dict (dict): Dictionary containing price details.
Returns:
str: LaTeX formatted listing information.
"""
new_median_list_price = float(price_dict['new_median_list_price'])
used_median_list_price = float(price_dict['used_median_list_price'])
if new_median_list_price > 0 or used_median_list_price > 0:
new_list_qty = int(price_dict['new_list_qty'])
used_list_qty = int(price_dict['used_list_qty'])
latex_str = '\\vspace{-3pt} {\\sffamily\\tiny list: '
if new_median_list_price > 0 and used_median_list_price > 0:
latex_str += fr'\${new_median_list_price/100:.2f} new ({new_list_qty}) / \${used_median_list_price/100:.2f} used ({used_list_qty})'
elif new_list_qty > 0 and new_median_list_price > 0:
latex_str += fr'\${new_median_list_price/100:.2f} new ({new_list_qty})'
elif used_list_qty > 0 and used_median_list_price > 0:
latex_str += fr'\${used_median_list_price/100:.2f} used ({used_list_qty})'
latex_str += '}\\\\\n'
return latex_str
return ''
#============================
# Main Function
#============================
def main() -> None:
"""
Main function to process LEGO minifigure data from a given CSV file.
Performs data lookup and label generation for each minifigure.
"""
# Check if the correct number of arguments is provided
if len(sys.argv) < 2:
print("usage: ./lookupLego.py <csv txt file with lego IDs>")
sys.exit(1)
# Get the input file name from the command line argument
minifigIDFile = sys.argv[1]
if not os.path.isfile(minifigIDFile):
print("usage: ./lookupLego.py <csv txt file with lego IDs>")
sys.exit(1)
# Read minifigure ID pairs from the input file
minifigIDpairs = libbrick.read_minifigIDpairs_from_file(minifigIDFile)
# Initialize the BrickLink wrapper
BLW = bricklink_wrapper.BrickLink()
line = 0
minifig_info_tree = []
for pair in minifigIDpairs:
minifigID, setID = pair
line += 1
sys.stderr.write(".")
try:
# Fetch minifigure data from BrickLink
minifig_data = BLW.getMinifigData(minifigID)
except LookupError:
continue
try:
# Fetch category name from BrickLink
category_name = BLW.getCategoryNameFromMinifigID(minifigID)
except LookupError:
time.sleep(random.random())
category_name = None
# Add additional data to minifig_data
minifig_data['category_name'] = category_name
minifig_data['set_id'] = setID
# Fetch price data for the minifigure
price_data = BLW.getMinifigPriceData(minifigID)
total_data = {**minifig_data, **price_data}
total_data['minifig_id'] = minifigID
# Check and clean minifigure data
if total_data.get('weight') is None:
import pprint
pprint.pprint(total_data)
print('')
print(total_data.keys())
raise KeyError("Missing key: weight")
if float(total_data['weight']) > 1000:
print(f"TOO BIG: weight {total_data['weight']} skipping {total_data['no']} from set {total_data.get('set_num')}: {total_data['name'][:60]}")
continue
minifig_info_tree.append(total_data)
# Save the cache every 50 lines
if line % 50 == 0:
BLW.save_cache()
# Sort the minifigures by set ID
minifig_info_tree = sorted(minifig_info_tree, key=lambda item: item.get('minifig_id'))
total_minifigs = len(minifig_info_tree)
print(f"Found {total_minifigs} Minifigs to process")
filename_root = os.path.splitext(minifigIDFile)[0]
outfile = f"labels-{filename_root}.tex"
pdffile = f"labels-{filename_root}.pdf"
# Write the LaTeX labels to the output file
with open(outfile, 'w') as f:
f.write(latex_header)
count = 0
total_pages = total_minifigs // 30 + 1
for minifig_dict in minifig_info_tree:
minifigID = minifig_dict['minifig_id']
price_dict = BLW.getMinifigPriceData(minifigID)
count += 1
label = makeLabel(minifig_dict, price_dict)
f.write(label)
if count % 5 == 0:
f.write(f'% page {count//30 + 1} of {total_pages} --- gap line --- count {count} of {total_minifigs} ---\n')
f.write(latex_footer)
BLW.close()
print(f'\n\nmogrify -verbose -trim images/minifig_*.jpg; \nxelatex {outfile}; \nopen {pdffile}')
#============================
if __name__ == "__main__":
main()