-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3DScanner.py
More file actions
851 lines (691 loc) · 32.1 KB
/
3DScanner.py
File metadata and controls
851 lines (691 loc) · 32.1 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
"""
3DScanner - Your go-to tool for finding trending 3D models
Hey! This script helps you discover what's hot in the 3D printing world.
It pulls data from MakerWorld, Printables, and Thingiverse so you can
spot trends and find models with less competition.
Think of it as your personal scout for the 3D model market.
"""
import csv
import os
import re
import sys
import time
import json
import subprocess
import platform
from datetime import datetime
import requests
from bs4 import BeautifulSoup
# We need Selenium to handle sites that load content with JavaScript
try:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
SELENIUM_AVAILABLE = True
except ImportError:
SELENIUM_AVAILABLE = False
try:
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
except ImportError:
ChromeDriverManager = None
GeckoDriverManager = None
class Scanner3D:
"""
This is the main engine that does all the heavy lifting.
It knows how to talk to each platform and grab the model data you need.
"""
def __init__(self, browser='auto'):
# We pretend to be a regular browser so websites don't block us
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
}
self.models = []
self.driver = None
self.browser_type = browser
self.browser_name = None
# Each platform organizes models differently, so we map their categories here
self.makerworld_categories = {
'1': ('All', ''),
'2': ('Art', '100'),
'3': ('Education', '500'),
'4': ('Fashion', '200'),
'5': ('Hobby & DIY', '300'),
'6': ('Household', '400'),
'7': ('Miniatures', '600'),
'8': ('Tools', '700'),
'9': ('Toys & Games', '800'),
}
self.printables_categories = {
'1': ('All', ''),
'2': ('3D Printer Accessories', '3d-printer-accessories'),
'3': ('Art', 'art'),
'4': ('Fashion', 'fashion'),
'5': ('Gadgets', 'gadgets'),
'6': ('Home', 'home'),
'7': ('Toys & Games', 'toys-games'),
'8': ('Tools', 'tools'),
'9': ('Outdoor & Garden', 'outdoor-garden'),
}
self.thingiverse_categories = {
'1': ('All', ''),
'2': ('Art', 'art'),
'3': ('Fashion', 'fashion'),
'4': ('Gadgets', 'gadgets'),
'5': ('Hobby', 'hobby'),
'6': ('Household', 'household'),
'7': ('Learning', 'learning'),
'8': ('Tools', 'tools'),
'9': ('Toys & Games', 'toys-games'),
}
def init_browser(self):
"""
Some websites need a full browser to work properly.
This sets one up for you automatically.
"""
if not SELENIUM_AVAILABLE:
print(" Heads up - you need Selenium for this.")
print(" Just run: pip install selenium webdriver-manager")
return False
if self.driver is not None:
return True
if self.browser_type in ['auto', 'chrome']:
if self._init_chrome():
return True
if self.browser_type in ['auto', 'firefox']:
if self._init_firefox():
return True
return False
def _init_chrome(self):
"""Tries to get Chrome up and running."""
print(" Checking if Chrome works...")
try:
chrome_options = ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument(f'user-agent={self.headers["User-Agent"]}')
if ChromeDriverManager:
service = ChromeService(ChromeDriverManager().install())
self.driver = webdriver.Chrome(service=service, options=chrome_options)
else:
self.driver = webdriver.Chrome(options=chrome_options)
self.browser_name = 'Chrome'
print(" Chrome is good to go!")
return True
except Exception as e:
print(f" Chrome didn't work: {str(e)[:40]}")
return False
def _init_firefox(self):
"""Falls back to Firefox if Chrome isn't available."""
print(" Checking if Firefox works...")
try:
firefox_options = FirefoxOptions()
firefox_options.add_argument('--headless')
if GeckoDriverManager:
service = FirefoxService(GeckoDriverManager().install())
self.driver = webdriver.Firefox(service=service, options=firefox_options)
else:
self.driver = webdriver.Firefox(options=firefox_options)
self.browser_name = 'Firefox'
print(" Firefox is ready!")
return True
except Exception as e:
print(f" Firefox didn't work: {str(e)[:40]}")
return False
def close_browser(self):
"""Cleans up when we're done."""
if self.driver:
try:
self.driver.quit()
except:
pass
self.driver = None
def scrape_makerworld(self, category='', sort='hotScore', limit=30):
"""
Grabs models from MakerWorld.
Quick note: MakerWorld uses a lot of JavaScript, so we need
a browser to see the actual content. Takes a bit longer, but it works.
"""
print(f"\n[Searching] Checking MakerWorld for {sort} models...")
if not self.init_browser():
print(" Can't access MakerWorld without a browser.")
print(" Run this to fix it: pip install selenium webdriver-manager")
return
try:
if category:
url = f"https://makerworld.com/en/3d-models?categories={category}&orderBy={sort}"
else:
url = f"https://makerworld.com/en/3d-models?orderBy={sort}"
print(f" Opening: {url}")
self.driver.get(url)
print(" Waiting for the page to load...")
time.sleep(5)
print(" Scrolling down to load more models...")
for i in range(4):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
self.driver.execute_script("window.scrollTo(0, 0);")
time.sleep(1)
soup = BeautifulSoup(self.driver.page_source, 'html.parser')
# We try a few different ways to find the models
model_cards = soup.find_all('div', {'data-trackid': re.compile(r'\d+')})
if model_cards:
count = self._parse_makerworld_cards(model_cards, category, limit)
if count > 0:
print(f" Nice! Found {count} models on MakerWorld")
return
count = self._parse_makerworld_links(soup, category, limit)
if count > 0:
print(f" Nice! Found {count} models on MakerWorld")
return
count = self._parse_makerworld_nextdata(soup, category, limit)
if count > 0:
print(f" Nice! Found {count} models on MakerWorld")
return
print(" Hmm, couldn't find any models. MakerWorld might have changed their layout.")
except Exception as e:
print(f" Something went wrong with MakerWorld: {e}")
def _parse_makerworld_cards(self, model_cards, category, limit):
"""Pulls model info from MakerWorld's card layout."""
count = 0
seen_ids = set()
for card in model_cards:
if count >= limit:
break
track_id = card.get('data-trackid', '')
model_id_match = re.search(r'^(\d+)', track_id)
if not model_id_match:
continue
model_id = model_id_match.group(1)
if model_id in seen_ids:
continue
seen_ids.add(model_id)
link = card.find('a', href=re.compile(r'/models/\d+'))
if not link:
continue
href = link.get('href', '')
name = link.get('title', '')
if not name:
h3 = card.find('h3')
if h3:
name = h3.get_text(strip=True)
if not name or len(name) < 2:
name = f"Model {model_id}"
author = 'Unknown'
author_link = card.find('a', href=re.compile(r'/@'))
if author_link:
author_span = author_link.find('span', class_=re.compile(r'author'))
if author_span:
author = author_span.get_text(strip=True)
else:
author = author_link.get_text(strip=True)
likes = 0
downloads = 0
stat_spans = card.find_all('span')
for span in stat_spans:
text = span.get_text(strip=True)
if text.isdigit():
num = int(text)
if likes == 0:
likes = num
elif downloads == 0:
downloads = num
model_data = {
'source': 'MakerWorld',
'name': name[:80],
'author': author,
'url': f"https://makerworld.com{href}" if not href.startswith('http') else href,
'model_id': model_id,
'downloads': downloads,
'likes': likes,
'comments': 0,
'date_scraped': datetime.now().strftime('%Y-%m-%d'),
'category': category if category else 'All',
}
self.models.append(model_data)
count += 1
return count
def _parse_makerworld_links(self, soup, category, limit):
"""Another way to find models - just look for model links."""
model_links = soup.find_all('a', href=re.compile(r'/en/models/\d+'))
seen_ids = set()
count = 0
for link in model_links:
if count >= limit:
break
href = link.get('href', '')
match = re.search(r'/models/(\d+)', href)
if not match:
continue
model_id = match.group(1)
if model_id in seen_ids:
continue
seen_ids.add(model_id)
name = link.get('title', '')
if not name:
h3 = link.find('h3')
if h3:
name = h3.get_text(strip=True)
else:
name = link.get_text(strip=True)
if not name or len(name) < 2:
continue
if name.isdigit() or len(name.strip()) < 3:
continue
model_data = {
'source': 'MakerWorld',
'name': name[:80],
'author': 'Unknown',
'url': f"https://makerworld.com{href}" if not href.startswith('http') else href,
'model_id': model_id,
'downloads': 0,
'likes': 0,
'comments': 0,
'date_scraped': datetime.now().strftime('%Y-%m-%d'),
'category': category if category else 'All',
}
self.models.append(model_data)
count += 1
return count
def _parse_makerworld_nextdata(self, soup, category, limit):
"""Tries to grab data directly from the page's embedded JSON."""
script = soup.find('script', {'id': '__NEXT_DATA__'})
if not script:
return 0
try:
data = json.loads(script.string)
props = data.get('props', {}).get('pageProps', {})
designs = (props.get('designs') or
props.get('data') or
props.get('models') or
props.get('items') or
[])
if not isinstance(designs, list):
if isinstance(designs, dict):
designs = designs.get('list', designs.get('items', []))
if not designs:
return 0
count = 0
for item in designs[:limit]:
if not isinstance(item, dict):
continue
model_id = str(item.get('id', item.get('designId', '')))
if not model_id:
continue
name = item.get('title', item.get('name', f'Model {model_id}'))
designer = item.get('designer', item.get('author', {}))
if isinstance(designer, dict):
author = designer.get('name', designer.get('nickname', 'Unknown'))
else:
author = str(designer) if designer else 'Unknown'
model_data = {
'source': 'MakerWorld',
'name': str(name)[:80],
'author': author,
'url': f"https://makerworld.com/en/models/{model_id}",
'model_id': model_id,
'downloads': int(item.get('downloadCount', item.get('downloads', 0))),
'likes': int(item.get('likeCount', item.get('likes', 0))),
'comments': int(item.get('commentCount', item.get('comments', 0))),
'date_scraped': datetime.now().strftime('%Y-%m-%d'),
'category': category if category else 'All',
}
self.models.append(model_data)
count += 1
return count
except (json.JSONDecodeError, KeyError, TypeError) as e:
print(f" Couldn't read the page data: {e}")
return 0
def scrape_printables(self, category='', sort='-hot', limit=30):
"""
Grabs models from Printables.
Good news - Printables works without a browser, so this is faster.
"""
print(f"\n[Searching] Checking Printables for {sort} models...")
if category:
url = f"https://www.printables.com/model?category={category}&ordering={sort}"
else:
url = f"https://www.printables.com/model?ordering={sort}"
try:
response = requests.get(url, headers=self.headers, timeout=15)
soup = BeautifulSoup(response.text, 'html.parser')
model_links = soup.find_all('a', href=re.compile(r'/model/\d+'))
seen_ids = set()
count = 0
for link in model_links:
if count >= limit:
break
href = link.get('href', '')
match = re.search(r'/model/(\d+)', href)
if not match:
continue
model_id = match.group(1)
if model_id in seen_ids:
continue
seen_ids.add(model_id)
name_match = re.search(r'/model/\d+-(.+)', href)
name = name_match.group(1).replace('-', ' ').title() if name_match else f"Model {model_id}"
model_data = {
'source': 'Printables',
'name': name[:80],
'author': 'Unknown',
'url': f"https://www.printables.com{href}",
'model_id': model_id,
'downloads': 0,
'likes': 0,
'comments': 0,
'date_scraped': datetime.now().strftime('%Y-%m-%d'),
'category': category if category else 'All',
}
self.models.append(model_data)
count += 1
print(f" Nice! Found {count} models on Printables")
except Exception as e:
print(f" Something went wrong: {e}")
def scrape_thingiverse(self, category='', sort='popular', limit=30):
"""
Grabs models from Thingiverse.
We try the simple approach first. If that doesn't work,
we fall back to using a browser.
"""
print(f"\n[Searching] Checking Thingiverse for {sort} models...")
# Simple approach first - it's faster
try:
url = f"https://www.thingiverse.com/search?type=things&sort={sort}"
print(f" Fetching: {url}")
response = requests.get(url, headers=self.headers, timeout=20)
soup = BeautifulSoup(response.text, 'html.parser')
count = self._parse_thingiverse_page(soup, category, limit)
if count > 0:
print(f" Nice! Found {count} models on Thingiverse")
return
except Exception as e:
print(f" Simple method didn't work: {str(e)[:50]}")
# Okay, let's try with a browser
print(" Trying with a browser instead...")
if self.init_browser():
try:
url = f"https://www.thingiverse.com/search?type=things&sort={sort}"
self.driver.get(url)
time.sleep(5)
for _ in range(2):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
soup = BeautifulSoup(self.driver.page_source, 'html.parser')
count = self._parse_thingiverse_page(soup, category, limit)
if count > 0:
print(f" Nice! Found {count} models on Thingiverse")
return
except Exception as e:
print(f" Browser method didn't work either: {str(e)[:50]}")
# One more try with a different URL
print(" Trying one more approach...")
try:
url = f"https://www.thingiverse.com/things?sort={sort}"
response = requests.get(url, headers=self.headers, timeout=20)
soup = BeautifulSoup(response.text, 'html.parser')
count = self._parse_thingiverse_page(soup, category, limit)
print(f" Found {count} models on Thingiverse")
except Exception as e:
print(f" Couldn't connect to Thingiverse: {str(e)[:50]}")
def _parse_thingiverse_page(self, soup, category, limit):
"""Extracts model data from Thingiverse pages."""
model_links = soup.find_all('a', href=re.compile(r'/thing:\d+'))
seen_ids = set()
count = 0
for link in model_links:
if count >= limit:
break
href = link.get('href', '')
match = re.search(r'/thing:(\d+)', href)
if not match:
continue
model_id = match.group(1)
if model_id in seen_ids:
continue
seen_ids.add(model_id)
name = link.get_text(strip=True)
if not name or len(name) < 2:
name = f"Thing {model_id}"
model_data = {
'source': 'Thingiverse',
'name': name[:80],
'author': 'Unknown',
'url': f"https://www.thingiverse.com/thing:{model_id}",
'model_id': model_id,
'downloads': 0,
'likes': 0,
'comments': 0,
'date_scraped': datetime.now().strftime('%Y-%m-%d'),
'category': category if category else 'All',
}
self.models.append(model_data)
count += 1
return count
def get_desktop_path(self):
"""Figures out where your Desktop folder is."""
home = os.path.expanduser("~")
# Different languages have different Desktop folder names
desktop_names = ["Desktop", "Рабочий стол", "Bureau", "Escritorio", "Schreibtisch", "桌面"]
for name in desktop_names:
path = os.path.join(home, name)
if os.path.exists(path):
return path
return home
def get_dated_filename(self, base_name='3DScanner_Results'):
"""Creates a filename with today's date so you can keep track of your scans."""
today = datetime.now().strftime('%Y-%m-%d')
return f"{base_name}_{today}.csv"
def save_to_csv(self, filename=None, open_after=True):
"""
Saves everything to a CSV file on your Desktop.
Opens it automatically so you can see the results right away.
"""
if not self.models:
print("\nNothing to save yet - try scraping some models first!")
return None
if not filename:
filename = self.get_dated_filename()
elif not filename.endswith('.csv'):
filename += '.csv'
desktop = self.get_desktop_path()
filepath = os.path.join(desktop, filename)
fieldnames = ['source', 'name', 'author', 'url', 'model_id',
'downloads', 'likes', 'comments', 'date_scraped', 'category']
with open(filepath, 'w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(self.models)
print(f"\nSaved {len(self.models)} models to your Desktop:")
print(f" {filepath}")
if open_after:
self.open_file(filepath)
return filepath
def open_file(self, filepath):
"""Opens the file in whatever app handles CSVs on your system."""
try:
system = platform.system()
if system == 'Windows':
os.startfile(filepath)
print(" Opening in Excel...")
elif system == 'Darwin':
subprocess.run(['open', filepath], check=True)
print(" Opening the file...")
else:
subprocess.run(['xdg-open', filepath], check=True)
print(" Opening the file...")
except Exception as e:
print(f" Saved! You can open it manually at: {filepath}")
def load_from_csv(self, filename=None):
"""Loads data from a previous scan so you can pick up where you left off."""
desktop = self.get_desktop_path()
if not filename:
filename = self.get_dated_filename()
filepath = os.path.join(desktop, filename)
if not os.path.exists(filepath):
print(f"\nCouldn't find: {filename}")
print(f" Looked in: {desktop}")
csv_files = [f for f in os.listdir(desktop) if f.startswith('3DScanner') and f.endswith('.csv')]
if csv_files:
print(f"\n Here's what I found:")
for f in sorted(csv_files)[-5:]:
print(f" - {f}")
return False
self.models = []
with open(filepath, 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
self.models.append(row)
print(f"\nLoaded {len(self.models)} models from:")
print(f" {filepath}")
return True
def show_results(self, top_n=25):
"""Shows you what we found in a nice, readable format."""
if not self.models:
print("\nNo models yet! Run a scan first to see results.")
return
print("\n" + "=" * 95)
print("TRENDING 3D MODELS - " + datetime.now().strftime('%Y-%m-%d'))
print("=" * 95)
print(f"{'#':<3} {'Source':<12} {'Name':<50} {'Downloads':<10} {'Likes':<8}")
print("-" * 95)
for i, model in enumerate(self.models[:top_n], 1):
name = model['name'][:48] + '..' if len(model['name']) > 50 else model['name']
downloads = model.get('downloads', 0)
likes = model.get('likes', 0)
print(f"{i:<3} {model['source']:<12} {name:<50} {downloads:<10} {likes:<8}")
print("-" * 95)
print(f"Total: {len(self.models)} models")
sources = {}
for m in self.models:
src = m['source']
sources[src] = sources.get(src, 0) + 1
print("\nBreakdown:", end=" ")
print(" | ".join([f"{src}: {count}" for src, count in sources.items()]))
def clear_models(self):
"""Wipes the slate clean so you can start fresh."""
count = len(self.models)
self.models = []
print(f"\nCleared {count} models. Ready for a fresh scan!")
def show_menu():
"""Shows you what you can do."""
print("\n" + "=" * 50)
print("What would you like to do?")
print("=" * 50)
print(" 1. Scan MakerWorld")
print(" 2. Scan Printables")
print(" 3. Scan Thingiverse")
print(" 4. Scan everything (all three sites)")
print(" 5. See what we found")
print(" 6. Save results to Desktop")
print(" 7. Load a previous scan")
print(" 8. Clear everything")
print(" 9. Exit")
print("=" * 50)
def select_category(scanner, platform):
"""Lets you pick which type of models to look for."""
categories = {
'makerworld': scanner.makerworld_categories,
'printables': scanner.printables_categories,
'thingiverse': scanner.thingiverse_categories,
}.get(platform, {})
print(f"\n{platform.title()} - Pick a category:")
for key, (name, _) in categories.items():
print(f" {key}. {name}")
choice = input("\nYour choice (1-9): ").strip()
return categories.get(choice, ('All', ''))[1]
def select_sort_order(platform):
"""Lets you choose how to sort the results."""
print("\nHow should we sort them?")
options = {
'makerworld': {'1': ('Trending', 'hotScore'), '2': ('Newest', 'newUploads'), '3': ('Most downloaded', 'downloadCount'), '4': ('Most liked', 'likeCount')},
'printables': {'1': ('Trending', '-hot'), '2': ('Newest', '-first_publish'), '3': ('Most downloaded', '-downloads'), '4': ('Most liked', '-likes')},
'thingiverse': {'1': ('Popular', 'popular'), '2': ('Newest', 'newest'), '3': ('Most makes', 'makes'), '4': ('Most liked', 'likes')},
}.get(platform, {})
for key, (name, _) in options.items():
print(f" {key}. {name}")
choice = input("\nYour choice (1-4): ").strip()
return options.get(choice, list(options.values())[0])[1]
def get_limit():
"""Asks how many models you want to grab."""
try:
limit = int(input("\nHow many models do you want? (just hit Enter for 30): ").strip() or "30")
return min(max(limit, 5), 100)
except:
return 30
def select_browser():
"""Lets you pick which browser to use for JavaScript-heavy sites."""
print("\nWhich browser should I use?")
print(" 1. Auto (tries Chrome first, then Firefox)")
print(" 2. Chrome")
print(" 3. Firefox")
choice = input("\nYour choice (1-3, Enter for auto): ").strip()
return {'2': 'chrome', '3': 'firefox'}.get(choice, 'auto')
def main():
"""Where everything kicks off."""
print("\n" + "=" * 55)
print(" 3DScanner")
print(" Find trending models before everyone else does")
print("=" * 55)
print(f"\nToday: {datetime.now().strftime('%Y-%m-%d')}")
print("Supported: MakerWorld, Printables, Thingiverse")
browser = select_browser()
scanner = Scanner3D(browser=browser)
try:
while True:
show_menu()
choice = input("Your choice (1-9): ").strip()
if choice == '1':
category = select_category(scanner, 'makerworld')
sort = select_sort_order('makerworld')
limit = get_limit()
scanner.scrape_makerworld(category=category, sort=sort, limit=limit)
elif choice == '2':
category = select_category(scanner, 'printables')
sort = select_sort_order('printables')
limit = get_limit()
scanner.scrape_printables(category=category, sort=sort, limit=limit)
elif choice == '3':
category = select_category(scanner, 'thingiverse')
sort = select_sort_order('thingiverse')
limit = get_limit()
scanner.scrape_thingiverse(category=category, sort=sort, limit=limit)
elif choice == '4':
print("\nScanning all platforms...")
scanner.clear_models()
limit = get_limit()
scanner.scrape_makerworld(sort='hotScore', limit=limit)
time.sleep(1)
scanner.scrape_printables(sort='-hot', limit=limit)
time.sleep(1)
scanner.scrape_thingiverse(sort='popular', limit=limit)
elif choice == '5':
scanner.show_results()
elif choice == '6':
filename = input(f"\nFilename (Enter for {scanner.get_dated_filename()}): ").strip()
scanner.save_to_csv(filename if filename else None, open_after=True)
elif choice == '7':
filename = input("\nWhich file? (Enter for today's): ").strip()
scanner.load_from_csv(filename if filename else None)
elif choice == '8':
scanner.clear_models()
elif choice == '9':
print("\nTake care! Happy hunting!")
break
else:
print("\nThat's not an option - pick a number from 1 to 9.")
finally:
scanner.close_browser()
if __name__ == "__main__":
main()