-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_datasheet_finder.py
More file actions
1523 lines (1298 loc) · 75.8 KB
/
smart_datasheet_finder.py
File metadata and controls
1523 lines (1298 loc) · 75.8 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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Smart Datasheet Finder - Downloads PDFs and extracts marking schemes
This module intelligently finds and downloads IC datasheets, then extracts
marking scheme information to validate chip authenticity.
"""
import re
import logging
import requests
from pathlib import Path
from typing import Dict, Optional, List
from bs4 import BeautifulSoup
import concurrent.futures
from urllib.parse import urljoin, urlparse
import urllib.parse
import PyPDF2
import io
import json
from datetime import datetime
logger = logging.getLogger(__name__)
class SmartDatasheetFinder:
"""Intelligent datasheet finder that downloads PDFs and extracts marking info"""
def __init__(self, cache_dir: Path):
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
})
# Timeout settings (quick for responsiveness)
self.timeout = 3 # 3 seconds max per request
def find_datasheet(self, part_number: str, manufacturer: str) -> Dict:
"""Find datasheet PDF and extract marking information"""
logger.info(f" 🔍 Searching for {part_number} datasheet...")
# Check cache first
cached_result = self._check_cache(part_number)
if cached_result:
return cached_result
# Search for PDF in parallel across multiple sources
pdf_url = self._find_pdf_url(part_number, manufacturer)
if not pdf_url:
logger.info(f" ✗ No PDF datasheet found for {part_number}")
return {'found': False, 'url': None, 'marking_info': None, 'source': None}
# Download PDF
pdf_path = self._download_pdf(pdf_url, part_number)
if not pdf_path:
logger.warning(f" ✗ Failed to download PDF from {pdf_url}")
return {'found': True, 'url': pdf_url, 'local_file': None, 'marking_info': None, 'source': 'Link Only'}
# Save metadata with original URL
self._save_metadata(pdf_path, pdf_url, part_number, manufacturer)
# Extract marking information from PDF
marking_info = self._extract_marking_from_pdf(pdf_path)
# Return file:// URL for local cached PDF
file_url = pdf_path.absolute().as_uri()
logger.info(f" ✓ Datasheet PDF downloaded and cached: {pdf_path.name}")
return {
'found': True,
'url': pdf_url, # Original manufacturer URL for display in tabs
'local_file': file_url, # Local cached file for PDF viewer
'marking_info': marking_info,
'source': 'PDF Downloaded',
'pdf_path': str(pdf_path)
}
def _check_cache(self, part_number: str) -> Optional[Dict]:
"""Check if PDF already cached"""
cached_file = self.cache_dir / f"{part_number}.pdf"
if cached_file.exists():
logger.info(f" ✓ Found in cache: {cached_file.name}")
# Extract marking info from cached PDF
marking_info = self._extract_marking_from_pdf(cached_file)
# Try to load metadata to get original URL
metadata = self._load_metadata(cached_file)
original_url = metadata.get('url') if metadata else None
# Use cached file URL
file_url = cached_file.absolute().as_uri()
# If no metadata (old cached PDF), return with warning
# The URL will be file:// but at least the PDF is available
if not original_url:
logger.debug(f" ⚠️ No metadata found for cached PDF: {cached_file.name}")
logger.debug(f" This PDF was downloaded before metadata system was added")
return {
'found': True,
'url': original_url or file_url, # Use original URL if available, else file URL
'local_file': file_url,
'marking_info': marking_info,
'source': 'Local Cache' if original_url else 'Local Cache (Legacy)',
'pdf_path': str(cached_file)
}
return None
def _find_pdf_url(self, part_number: str, manufacturer: str) -> Optional[str]:
"""Find direct PDF URL using parallel search across sources"""
# Universal search - no hardcoded URLs
part_upper = re.sub(r'[^A-Z0-9-]', '', part_number).upper()
# Prepare search functions
search_functions = []
if 'Texas Instruments' in manufacturer or 'TI' in manufacturer:
search_functions.append(('TI', lambda: self._search_ti_pdf(part_number)))
if 'Microchip' in manufacturer or 'Atmel' in manufacturer:
search_functions.append(('Microchip', lambda: self._search_microchip_pdf(part_number)))
if 'Infineon' in manufacturer or 'Cypress' in manufacturer:
search_functions.append(('Infineon', lambda: self._search_infineon_pdf(part_number)))
if 'NXP' in manufacturer:
search_functions.append(('NXP', lambda: self._search_nxp_pdf(part_number)))
if 'STMicroelectronics' in manufacturer or 'STM' in manufacturer:
search_functions.append(('STM', lambda: self._search_stm_pdf(part_number)))
if 'Analog' in manufacturer or 'Linear' in manufacturer:
search_functions.append(('Analog', lambda: self._search_analog_pdf(part_number)))
if 'ON Semiconductor' in manufacturer or 'onsemi' in manufacturer.lower():
search_functions.append(('ONSemi', lambda: self._search_onsemi_pdf(part_number)))
# If manufacturer unknown or "Various" (generic parts), try all sources
if not search_functions or 'Various' in manufacturer or 'Unknown' in manufacturer:
search_functions = [
('TI', lambda: self._search_ti_pdf(part_number)),
('Microchip', lambda: self._search_microchip_pdf(part_number)),
('Infineon', lambda: self._search_infineon_pdf(part_number)),
('NXP', lambda: self._search_nxp_pdf(part_number)),
('STM', lambda: self._search_stm_pdf(part_number)),
('Analog', lambda: self._search_analog_pdf(part_number)),
('ONSemi', lambda: self._search_onsemi_pdf(part_number)),
]
# For 74HC parts, also try ON Semiconductor (they make pin-compatible parts)
if part_upper.startswith('M74HC') or part_upper.startswith('74HC'):
search_functions.append(('ONSemi-Fallback', lambda: self._search_onsemi_pdf(part_number)))
# For NE555 and common generic ICs, prioritize TI and add ST as fallback
if part_upper.startswith('NE555') or part_upper.startswith('NE5'):
# NE555 is made by many vendors - try TI first, then ST
search_functions.insert(0, ('TI-NE555', lambda: self._search_ti_pdf('NE555')))
search_functions.append(('ST-NE555', lambda: self._search_stm_pdf('NE555')))
# For LM556 (dual 555 timer) - try multiple vendors since TI discontinued it
if part_upper.startswith('LM556') or part_upper.startswith('LK556'):
# LM556 is available from multiple vendors, try aggregators
search_functions.insert(0, ('DigiKey-LM556', lambda: self._search_digikey_pdf('LM556')))
search_functions.insert(1, ('Mouser-LM556', lambda: self._search_mouser_pdf('LM556')))
search_functions.append(('AllDatasheet-LM556', lambda: self._search_alldatasheet_pdf('LM556')))
# For ATMEL parts - try aggregators to bypass Microchip bot protection
if part_upper.startswith('ATMEL'):
atmel_num = part_upper[5:]
search_functions.insert(0, ('DigiKey-ATMEL', lambda: self._search_digikey_pdf(f'ATMEL{atmel_num}')))
search_functions.insert(1, ('Mouser-ATMEL', lambda: self._search_mouser_pdf(f'ATMEL{atmel_num}')))
search_functions.append(('AllDatasheet-ATMEL', lambda: self._search_alldatasheet_pdf(f'ATMEL{atmel_num}')))
# Try each search function with timeout
for source, search_func in search_functions:
try:
logger.debug(f" Trying {source}...")
pdf_url = search_func()
if pdf_url:
logger.debug(f" ✓ Found PDF URL from {source}: {pdf_url}")
return pdf_url
except Exception as e:
logger.debug(f" ✗ {source} search failed: {e}")
continue
# Try generic fallback search (Octopart aggregator)
logger.debug(f" Trying generic fallback search...")
pdf_url = self._search_generic_fallback(part_number)
if pdf_url:
logger.debug(f" ✓ Found PDF URL from generic search: {pdf_url}")
return pdf_url
# Last resort: Try Google search (most powerful fallback)
logger.debug(f" Trying Google search...")
pdf_url = self._search_google_pdf(part_number, manufacturer)
if pdf_url:
logger.debug(f" ✓ Found PDF URL from Google: {pdf_url}")
return pdf_url
return None
def _search_digikey_pdf(self, part: str) -> Optional[str]:
"""Search DigiKey for direct PDF datasheet link"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
logger.debug(f"🔍 DigiKey search: part={part}, base={base}")
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
try:
# DigiKey product search
search_url = f"https://www.digikey.com/en/products/result?keywords={base}"
logger.debug(f" Trying DigiKey: {search_url}")
response = requests.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet PDF links
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text().lower()
# DigiKey links to manufacturer datasheets
if 'datasheet' in text and '.pdf' in href.lower():
# Validate it's a real PDF
if self._validate_pdf_url(href):
logger.info(f" ✅ Found PDF via DigiKey: {href}")
return href
# Also check for PDF links without "datasheet" text
if '.pdf' in href.lower() and any(mfg in href.lower() for mfg in ['ti.com', 'microchip.com', 'infineon.com', 'onsemi.com']):
if self._validate_pdf_url(href):
logger.info(f" ✅ Found manufacturer PDF via DigiKey: {href}")
return href
except Exception as e:
logger.debug(f" DigiKey search failed: {e}")
return None
def _search_mouser_pdf(self, part: str) -> Optional[str]:
"""Search Mouser for direct PDF datasheet link"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
logger.debug(f"🔍 Mouser search: part={part}, base={base}")
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
try:
# Mouser product search
search_url = f"https://www.mouser.com/c/?q={base}"
logger.debug(f" Trying Mouser: {search_url}")
response = requests.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet PDF links
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text().lower()
# Mouser links to manufacturer datasheets
if ('datasheet' in text or 'pdf' in text) and '.pdf' in href.lower():
full_url = href if href.startswith('http') else urljoin('https://www.mouser.com', href)
if self._validate_pdf_url(full_url):
logger.info(f" ✅ Found PDF via Mouser: {full_url}")
return full_url
except Exception as e:
logger.debug(f" Mouser search failed: {e}")
return None
def _search_alldatasheet_pdf(self, part: str) -> Optional[str]:
"""Search AllDatasheet.com for direct PDF datasheet link"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
logger.debug(f"🔍 AllDatasheet search: part={part}, base={base}")
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
try:
# AllDatasheet search
search_url = f"https://www.alldatasheet.com/datasheet-pdf/pdf-searcher.php?sSearchword={base}"
logger.debug(f" Trying AllDatasheet: {search_url}")
response = requests.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for PDF download links
for link in soup.find_all('a', href=True):
href = link['href']
# AllDatasheet has direct PDF links in format: /datasheet-pdf/pdf/NUMBER/MANUFACTURER/PART.html
if '/datasheet-pdf/pdf/' in href or 'download' in href.lower():
full_url = href if href.startswith('http') else f"https://www.alldatasheet.com{href}"
# Try to extract the actual PDF URL from the download page
try:
logger.debug(f" Checking AllDatasheet page: {full_url}")
pdf_page = requests.get(full_url, headers=headers, timeout=3)
if pdf_page.status_code == 200:
pdf_soup = BeautifulSoup(pdf_page.text, 'html.parser')
# Look for the actual PDF link
for pdf_link in pdf_soup.find_all('a', href=True):
pdf_href = pdf_link['href']
if '.pdf' in pdf_href.lower() and ('pdf1.alldatasheet.com' in pdf_href or 'pdf.alldatasheet.com' in pdf_href):
if self._validate_pdf_url(pdf_href):
logger.info(f" ✅ Found PDF via AllDatasheet: {pdf_href}")
return pdf_href
except Exception as inner_e:
logger.debug(f" Failed to extract PDF from AllDatasheet page: {inner_e}")
except Exception as e:
logger.debug(f" AllDatasheet search failed: {e}")
return None
def _search_google_pdf(self, part: str, manufacturer: str) -> Optional[str]:
"""Search Google for datasheet PDFs - most powerful fallback"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
logger.debug(f"🔍 Google search: part={part}, manufacturer={manufacturer}")
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.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',
'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
# Try multiple search engines
search_engines = [
('DuckDuckGo', f"https://duckduckgo.com/html/?q={urllib.parse.quote(f'{manufacturer} {part} datasheet pdf')}"),
('Google', f"https://www.google.com/search?q={urllib.parse.quote(f'{manufacturer} {part} datasheet filetype:pdf')}"),
]
for engine_name, search_url in search_engines:
try:
logger.debug(f" Trying {engine_name}: {manufacturer} {part} datasheet")
response = requests.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for ALL links in the page
for link in soup.find_all('a', href=True):
href = link['href']
# Extract actual URLs from search engine redirects
actual_url = None
# Google format: /url?q=URL
if '/url?q=' in href:
try:
actual_url = href.split('/url?q=')[1].split('&')[0]
actual_url = urllib.parse.unquote(actual_url)
except:
continue
# DuckDuckGo format: //duckduckgo.com/l/?uddg=URL
elif '//duckduckgo.com/l/' in href or 'uddg=' in href:
try:
actual_url = urllib.parse.unquote(href.split('uddg=')[1].split('&')[0])
except:
continue
# Direct link
elif href.startswith('http'):
actual_url = href
# Check if we found a valid PDF URL
if actual_url and '.pdf' in actual_url.lower():
# Extended trusted domains list
trusted_domains = [
'infineon.com', 'cypress.com', 'ti.com', 'microchip.com',
'nxp.com', 'st.com', 'analog.com', 'onsemi.com',
'mouser.com', 'digikey.com', 'alldatasheet.com',
'datasheetcatalog.com', 'snapeda.com', 'findchips.com',
'element14.com', 'farnell.com', 'newark.com'
]
# Check if URL is from a trusted source
if any(domain in actual_url.lower() for domain in trusted_domains):
logger.debug(f" Testing PDF from {engine_name}: {actual_url}")
# Validate it's a real PDF
if self._validate_pdf_url(actual_url):
logger.info(f" ✅ Found PDF via {engine_name}: {actual_url}")
return actual_url
logger.debug(f" No valid PDF found via {engine_name}")
except Exception as e:
logger.debug(f" {engine_name} search failed: {e}")
continue
return None
def _search_generic_fallback(self, part: str) -> Optional[str]:
"""Generic fallback search using multiple aggregators and archives"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
# User agent headers for web scraping
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
# Try SnapEDA (comprehensive datasheets database)
try:
logger.debug(f" Trying SnapEDA...")
search_url = f"https://www.snapeda.com/parts/{base}/search"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() or '.pdf' in href.lower():
full_url = href if href.startswith('http') else f"https://www.snapeda.com{href}"
if self._validate_pdf_url(full_url):
logger.debug(f" ✅ Found via SnapEDA: {full_url}")
return full_url
except Exception as e:
logger.debug(f" SnapEDA search failed: {e}")
# Try DigiKey's datasheet aggregator (very comprehensive)
try:
logger.debug(f" Trying DigiKey...")
search_url = f"https://www.digikey.com/en/products/result?keywords={base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text().lower()
if 'datasheet' in text and '.pdf' in href.lower():
# Validate it's a real PDF
if self._validate_pdf_url(href):
logger.debug(f" ✅ Found via DigiKey: {href}")
return href
except Exception as e:
logger.debug(f" DigiKey search failed: {e}")
# Try Mouser (distributor with good datasheet links)
try:
logger.debug(f" Trying Mouser...")
search_url = f"https://www.mouser.com/Semiconductors/_/N-b1yc6?Keyword={base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() and '.pdf' in href.lower():
full_url = href if href.startswith('http') else f"https://www.mouser.com{href}"
if self._validate_pdf_url(full_url):
logger.debug(f" ✅ Found via Mouser: {full_url}")
return full_url
except Exception as e:
logger.debug(f" Mouser search failed: {e}")
# Try Octopart search (aggregator with multiple sources)
try:
logger.debug(f" Trying Octopart...")
search_url = f"https://octopart.com/search?q={base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
if '.pdf' in href.lower() and 'datasheet' in href.lower():
# Validate it's a real PDF
if self._validate_pdf_url(href):
logger.debug(f" ✅ Found via Octopart: {href}")
return href
except Exception as e:
logger.debug(f" Octopart search failed: {e}")
# Try SnapEDA (electronic parts database with direct datasheet links)
try:
logger.debug(f" Trying SnapEDA...")
search_url = f"https://www.snapeda.com/search/?q={base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() and '.pdf' in href.lower():
full_url = href if href.startswith('http') else f"https://www.snapeda.com{href}"
if self._validate_pdf_url(full_url):
logger.debug(f" ✅ Found via SnapEDA: {full_url}")
return full_url
except Exception as e:
logger.debug(f" SnapEDA search failed: {e}")
# Try FindChips (parts search engine with datasheet links)
try:
logger.debug(f" Trying FindChips...")
search_url = f"https://www.findchips.com/search/{base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text().lower()
if 'datasheet' in text or 'datasheet' in href.lower():
if '.pdf' in href.lower():
full_url = href if href.startswith('http') else f"https://www.findchips.com{href}"
if self._validate_pdf_url(full_url):
logger.debug(f" ✅ Found via FindChips: {full_url}")
return full_url
except Exception as e:
logger.debug(f" FindChips search failed: {e}")
# Try Element14 (large distributor with good datasheet database)
try:
logger.debug(f" Trying Element14...")
search_url = f"https://www.element14.com/community/search.jspa?q={base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() and '.pdf' in href.lower():
full_url = href if href.startswith('http') else f"https://www.element14.com{href}"
if self._validate_pdf_url(full_url):
logger.debug(f" ✅ Found via Element14: {full_url}")
return full_url
except Exception as e:
logger.debug(f" Element14 search failed: {e}")
# Try AllDatasheet (comprehensive archive with many legacy parts)
try:
logger.debug(f" Trying AllDatasheet...")
search_url = f"https://www.alldatasheet.com/datasheet-pdf/pdf-searcher.php?sSearchword={base}"
response = requests.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for PDF download links
for link in soup.find_all('a', href=True):
href = link['href']
# AllDatasheet has direct PDF links in format: /datasheet-pdf/pdf/NUMBER/MANUFACTURER/PART.html
if '/datasheet-pdf/pdf/' in href or 'download' in href.lower():
full_url = href if href.startswith('http') else f"https://www.alldatasheet.com{href}"
# Try to extract the actual PDF URL from the download page
try:
pdf_page = requests.get(full_url, headers=headers, timeout=3)
if pdf_page.status_code == 200:
pdf_soup = BeautifulSoup(pdf_page.text, 'html.parser')
# Look for the actual PDF link
for pdf_link in pdf_soup.find_all('a', href=True):
pdf_href = pdf_link['href']
if '.pdf' in pdf_href.lower() and ('pdf1.alldatasheet.com' in pdf_href or 'pdf.alldatasheet.com' in pdf_href):
if self._validate_pdf_url(pdf_href):
logger.debug(f" ✅ Found via AllDatasheet: {pdf_href}")
return pdf_href
except:
pass
except Exception as e:
logger.debug(f" AllDatasheet search failed: {e}")
# Try DatasheetCatalog (another comprehensive archive)
try:
logger.debug(f" Trying DatasheetCatalog...")
search_url = f"https://www.datasheetcatalog.com/datasheets_pdf/{base[0]}/{base}.shtml"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for PDF links
for link in soup.find_all('a', href=True):
href = link['href']
if '.pdf' in href.lower():
full_url = href if href.startswith('http') else f"https://www.datasheetcatalog.com{href}"
if self._validate_pdf_url(full_url):
logger.debug(f" ✅ Found via DatasheetCatalog: {full_url}")
return full_url
except Exception as e:
logger.debug(f" DatasheetCatalog search failed: {e}")
logger.debug(f" ❌ No datasheet found from any source")
# Try Mouser again with different URL pattern
try:
search_url = f"https://www.mouser.com/c/?q={base}"
response = requests.get(search_url, headers=headers, timeout=3)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet links
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text().lower()
if 'datasheet' in text or 'pdf' in text:
if '.pdf' in href.lower():
full_url = href if href.startswith('http') else urljoin('https://www.mouser.com', href)
if self._validate_pdf_url(full_url):
return full_url
except Exception as e:
logger.debug(f"Mouser search failed: {e}")
return None
def _search_ti_pdf(self, part: str) -> Optional[str]:
"""Search Texas Instruments for direct PDF link"""
base = re.sub(r'[^A-Z0-9]', '', part).upper()
logger.debug(f"🔍 TI search: part={part}, base={base}")
# Handle common OCR errors
if base.startswith('AUCH'):
base = 'AUC' + base[4:] # AUCH16244X → AUC16244X
logger.debug(f" AUCH detected, corrected to: {base}")
# Special case for LM556 (dual 555 timer) - comprehensive patterns
if 'LM556' in base or 'LK556' in base or 'IM556' in base:
base = 'LM556'
logger.debug(f" LM556 detected, normalized to: {base}")
# Add specific LM556 patterns early in the list
lm556_patterns = [
"https://www.ti.com/lit/ds/symlink/lm556.pdf",
"https://www.ti.com/lit/gpn/lm556.pdf",
"https://www.ti.com/lit/ds/snas545/lm556.pdf", # Document ID
"https://www.ti.com/lit/ds/symlink/lm556cn.pdf",
"https://www.ti.com/lit/ds/symlink/lm556-n.pdf",
"https://www.ti.com/lit/gpn/lm556cn.pdf",
"https://www.ti.com/lit/ds/snas545.pdf", # Document ID without part name
]
# Try LM556 specific patterns first
for url in lm556_patterns:
logger.debug(f" LM556 specific: Trying {url}")
if self._validate_pdf_url(url):
logger.info(f" ✅ Found LM556 PDF: {url}")
return url
# Remove package suffixes
clean = base
for suffix in ['CCN', 'CN', 'PW', 'PWR', 'DW', 'DR', 'DGK', 'DBV', 'DCK', 'DGV', 'N', 'P', 'D']:
if clean.endswith(suffix) and len(clean) - len(suffix) >= 3:
clean = clean[:-len(suffix)]
logger.debug(f" Removed suffix '{suffix}': {base} → {clean}")
break
# For AUC parts, also try without trailing X (AUC16244X → AUC16244)
clean_no_x = clean[:-1] if clean.startswith('AUC') and clean.endswith('X') and len(clean) > 5 else None
if clean_no_x:
logger.debug(f" AUC part with X suffix: {clean} → {clean_no_x}")
# TI direct PDF patterns with comprehensive variants
pdf_urls = [
# LM556 specific patterns (dual 555 timer)
f"https://www.ti.com/lit/ds/symlink/lm556.pdf" if 'LM556' in base else None,
f"https://www.ti.com/lit/ds/symlink/lm556cn.pdf" if 'LM556' in base else None,
f"https://www.ti.com/lit/gpn/lm556.pdf" if 'LM556' in base else None,
# Standard patterns
f"https://www.ti.com/lit/ds/symlink/{clean.lower()}.pdf",
f"https://www.ti.com/lit/gpn/{clean.lower()}.pdf",
f"https://www.ti.com/lit/ds/symlink/{base.lower()}.pdf",
f"https://www.ti.com/lit/gpn/{base.lower()}.pdf",
# Family variants (ADC0831 → adc0831x, adc083x)
f"https://www.ti.com/lit/ds/symlink/{clean.lower()}x.pdf",
f"https://www.ti.com/lit/ds/symlink/{clean[:-1].lower()}x.pdf" if len(clean) > 3 else None,
# Datasheet number patterns (scas, slls, sbas, etc.)
f"https://www.ti.com/lit/ds/{clean.lower()}.pdf",
# National Semiconductor legacy (ADC parts) - with -N suffix
f"https://www.ti.com/lit/ds/symlink/{clean.lower()}-n.pdf",
f"https://www.ti.com/lit/ds/symlink/{clean.lower()}cn.pdf",
# For AUC parts - try SN74AUC prefix (with and without X)
f"https://www.ti.com/lit/ds/symlink/sn74{clean_no_x.lower()}.pdf" if clean_no_x else None,
f"https://www.ti.com/lit/ds/symlink/sn74{clean.lower()}.pdf" if clean.startswith('AUC') else None,
f"https://www.ti.com/lit/ds/symlink/sn74{base.lower()}.pdf" if base.startswith('AUC') else None,
# Try without trailing letters
f"https://www.ti.com/lit/ds/symlink/{clean[:-1].lower()}.pdf" if len(clean) > 5 and clean[-1].isalpha() else None,
]
# Filter out None values
pdf_urls = [url for url in pdf_urls if url]
logger.debug(f" Testing {len(pdf_urls)} direct PDF URLs...")
for i, url in enumerate(pdf_urls, 1):
logger.debug(f" [{i}/{len(pdf_urls)}] Trying: {url}")
if self._validate_pdf_url(url):
logger.info(f" ✅ Found TI PDF: {url}")
return url
logger.debug(f" ❌ Not valid")
# Try product pages with more variants
product_urls = [
f"https://www.ti.com/product/{clean}",
f"https://www.ti.com/product/{base}",
f"https://www.ti.com/product/{clean.lower()}",
f"https://www.ti.com/product/{base.lower()}",
# For AUC parts - try with SN74AUC prefix
f"https://www.ti.com/product/SN74{clean}" if clean.startswith('AUC') else None,
]
# Filter None
product_urls = [url for url in product_urls if url]
logger.debug(f" Testing {len(product_urls)} product pages...")
for i, url in enumerate(product_urls, 1):
logger.debug(f" [{i}/{len(product_urls)}] Trying: {url}")
pdf_link = self._extract_pdf_from_page(url)
if pdf_link:
logger.info(f" ✅ Found TI PDF from page: {pdf_link}")
return pdf_link
logger.debug(f" ❌ No PDF found on page")
# Try searching TI documentation directly
try:
search_url = f"https://www.ti.com/sitesearch/en-us/docs/universalsearch.tsp?searchTerm={clean}"
response = self.session.get(search_url, timeout=self.timeout)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for PDF links in search results
for link in soup.find_all('a', href=True):
href = link['href']
if '.pdf' in href.lower() and ('lit/ds' in href or 'lit/gpn' in href):
full_url = urljoin('https://www.ti.com', href)
if self._validate_pdf_url(full_url):
return full_url
except Exception as e:
logger.debug(f"TI search failed: {e}")
return None
def _search_microchip_pdf(self, part: str) -> Optional[str]:
"""Search Microchip for direct PDF link - prioritize product page scraping"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
# Handle ATMEL parts (Atmel was acquired by Microchip)
if base.startswith('ATMEL'):
atmel_num = base[5:] # Extract number part (ATMEL426 → 426)
# Try direct Atmel legacy PDF patterns
atmel_patterns = [
# Microchip legacy Atmel docs
f"https://ww1.microchip.com/downloads/en/DeviceDoc/doc{atmel_num}.pdf",
f"https://ww1.microchip.com/downloads/en/DeviceDoc/atmel-{atmel_num}.pdf",
f"https://ww1.microchip.com/downloads/en/DeviceDoc/atmel{atmel_num}.pdf",
f"https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/{base}.pdf",
f"https://ww1.microchip.com/downloads/en/DeviceDoc/{base}.pdf",
# Try with AT prefix instead of ATMEL
f"https://ww1.microchip.com/downloads/en/DeviceDoc/doc{atmel_num}.pdf",
f"https://ww1.microchip.com/downloads/en/DeviceDoc/AT{atmel_num}.pdf",
# Legacy Atmel site patterns
f"http://www.atmel.com/Images/doc{atmel_num}.pdf",
f"http://www.atmel.com/Images/Atmel-{atmel_num}.pdf",
# Try with both upper/lower case
f"https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-{atmel_num}-{base}-Datasheet.pdf",
]
for url in atmel_patterns:
if self._validate_pdf_url(url):
logger.info(f" ✅ Found ATMEL PDF: {url}")
return url
# Try product pages (official + third-party)
product_urls = [
# Microchip official
f"https://www.microchip.com/en-us/product/{base}",
f"https://www.microchip.com/en-us/product/at{atmel_num}",
f"https://www.microchip.com/en-us/product/AT{atmel_num}",
# SnapEDA for legacy/discontinued Atmel parts
f"https://www.snapeda.com/parts/{base}/search",
f"https://www.snapeda.com/parts/AT{atmel_num}/search",
# Try AllDatasheet for very old ATMEL parts
f"https://www.alldatasheet.com/datasheet-pdf/pdf-searcher.php?sSearchword={base}",
]
for url in product_urls:
pdf_link = self._extract_pdf_from_page(url)
if pdf_link:
return pdf_link
# Handle AT24C series (EEPROM memory chips)
if base.startswith('AT24C') or base.startswith('AT24'):
# AT24C1024W → try multiple patterns
clean = re.sub(r'[A-Z]+$', '', base) # Remove trailing letters
at24_patterns = [
# Modern Microchip patterns
f"https://ww1.microchip.com/downloads/en/DeviceDoc/{base}.pdf",
f"https://ww1.microchip.com/downloads/en/DeviceDoc/{clean}.pdf",
f"https://ww1.microchip.com/downloads/aemDocuments/documents/MPD/ProductDocuments/DataSheets/{base}-Data-Sheet.pdf",
f"https://ww1.microchip.com/downloads/aemDocuments/documents/MPD/ProductDocuments/DataSheets/{clean}-Data-Sheet.pdf",
# Legacy Atmel patterns
f"http://www.atmel.com/Images/{base}.pdf",
f"http://www.atmel.com/Images/Atmel-{base}.pdf",
]
for url in at24_patterns:
if self._validate_pdf_url(url):
logger.info(f" ✅ Found AT24C PDF: {url}")
return url
# Microchip's direct PDF URLs are broken/redirected - go straight to product page
product_urls = [
f"https://www.microchip.com/en-us/product/{base}",
f"https://www.microchip.com/en-us/product/{base.lower()}",
]
# For ATMEGA/ATTINY - try without package suffix too
if base.startswith(('ATMEGA', 'ATTINY', 'ATXMEGA')):
clean = re.sub(r'[A-Z]+$', '', base) if base[-1].isalpha() else base
product_urls.append(f"https://www.microchip.com/en-us/product/{clean.lower()}")
# Try product pages first (more reliable than direct PDFs)
for url in product_urls:
pdf_link = self._extract_pdf_from_page(url)
if pdf_link:
return pdf_link
return None
def _search_infineon_pdf(self, part: str) -> Optional[str]:
"""Search Infineon/Cypress for direct PDF link"""
base = re.sub(r'[^A-Z0-9-]', '', part).upper()
logger.debug(f"🔍 Infineon search: part={part}, base={base}")
# For CY8C (Cypress PSoC)
if base.startswith('CY8C') or base.startswith('CY7C'):
# Remove package suffix for better search
clean = re.sub(r'-\d+[A-Z]+$', '', base) # CY8C29666-24PVXI → CY8C29666
logger.debug(f" CY8C/CY7C detected: clean={clean}")
# PRIORITY: Check for known working URLs FIRST (before Google search)
known_urls = {
"CY8C29666": "https://www.infineon.com/assets/row/public/documents/non-assigned/49/infineon-cy8c29466-cy8c29666-automotive-extended-temperature-psoc-programmable-system-on-chip-datasheet-en.pdf?fileId=8ac78c8c7d0d8da4017d0ec676923cae",
"CY8C29466": "https://www.infineon.com/assets/row/public/documents/non-assigned/49/infineon-cy8c29466-cy8c29666-automotive-extended-temperature-psoc-programmable-system-on-chip-datasheet-en.pdf?fileId=8ac78c8c7d0d8da4017d0ec676923cae",
}
if clean in known_urls:
logger.debug(f" Trying known working URL for {clean}...")
if self._validate_pdf_url(known_urls[clean]):
logger.info(f" ✅ Found {clean} PDF via known URL!")
return known_urls[clean]
# TRY GOOGLE SECOND for other CY8C parts
logger.debug(f" Trying Google search for CY8C...")
google_result = self._search_google_pdf(clean, 'Infineon Cypress')
if google_result:
logger.info(f" ✅ Found CY8C PDF via Google: {google_result}")
return google_result
# Comprehensive Infineon/Cypress PDF patterns (direct PDFs only)
pdf_urls = [
# Try the pattern without fileId parameter (more generic)
f"https://www.infineon.com/assets/row/public/documents/non-assigned/49/infineon-cy8c29466-{clean.lower()}-automotive-extended-temperature-psoc-programmable-system-on-chip-datasheet-en.pdf",
# Modern Infineon patterns
f"https://www.infineon.com/dgdl/Infineon-{clean}-DataSheet-v01_00-EN.pdf",
f"https://www.infineon.com/dgdl/{clean}-DataSheet.pdf",
f"https://www.infineon.com/dgdl/Infineon-{base}-DataSheet-v01_00-EN.pdf",
f"https://www.infineon.com/dgdl/{base}.pdf",
f"https://www.infineon.com/dgdl/{clean}.pdf",
# Legacy Cypress patterns (archive)
f"https://www.cypress.com/file/{clean.lower()}-datasheet",
f"http://www.cypress.com/file/{clean.lower()}-datasheet",
f"https://www.cypress.com/file/{clean.lower()}/{clean.lower()}-datasheet.pdf",
# PSoC-specific patterns
f"https://www.infineon.com/dgdl/PSoC_{clean}_DataSheet.pdf",
f"https://www.cypress.com/documentation/datasheets/{clean.lower()}-psoc-programmable-system-chip",
# Try version variants
f"https://www.infineon.com/dgdl/Infineon-{clean}-DataSheet-v02_00-EN.pdf",
f"https://www.infineon.com/dgdl/Infineon-{clean}-DataSheet-v03_00-EN.pdf",
# Try with family prefix (CY8C2 series → PSoC 1)
f"https://www.infineon.com/dgdl/{clean}-PSoC-1-Datasheet.pdf",
# Try Cypress archive site
f"https://www.cypress.com/file/45906/download", # CY8C29666 specific
f"https://www.cypress.com/file/46221/download", # CY8C series general
]
# Filter out None values
pdf_urls = [url for url in pdf_urls if url]
logger.debug(f" Testing {len(pdf_urls)} direct PDF URLs...")
for i, url in enumerate(pdf_urls, 1):
logger.debug(f" [{i}/{len(pdf_urls)}] Trying: {url}")
if self._validate_pdf_url(url):
logger.info(f" ✅ Found CY8C/CY7C PDF: {url}")
return url
logger.debug(f" ❌ Not valid")
# Try product pages with comprehensive variants (including third-party sites)
product_urls = [
# Infineon official
f"https://www.infineon.com/cms/en/product/{clean.lower()}/",
f"https://www.infineon.com/cms/en/product/{base.lower()}/",
f"https://www.infineon.com/cms/en/product/psoc/{clean.lower()}/",
# Legacy Cypress URLs
f"https://www.cypress.com/products/{clean.lower()}",
f"http://www.cypress.com/documentation/datasheets/{clean.lower()}",
# Try without CY8C prefix (just the number)
f"https://www.infineon.com/cms/en/product/{clean[4:].lower()}/",
# SnapEDA for discontinued Cypress parts
f"https://www.snapeda.com/parts/{clean}/search",
f"https://www.snapeda.com/parts/{base}/search",
# Try AllDatasheet for very old Cypress parts
f"https://www.alldatasheet.com/datasheet-pdf/pdf-searcher.php?sSearchword={clean}",
]
logger.debug(f" Testing {len(product_urls)} product pages...")
for i, url in enumerate(product_urls, 1):
logger.debug(f" [{i}/{len(product_urls)}] Trying: {url}")
pdf_link = self._extract_pdf_from_page(url)
if pdf_link:
logger.info(f" ✅ Found CY8C PDF from page: {pdf_link}")
return pdf_link
logger.debug(f" ❌ No PDF found on page")
# Try Infineon search
logger.debug(f" Trying Infineon search...")
try:
search_url = f"https://www.infineon.com/cms/en/search.html#!term={clean}&view=all"
logger.debug(f" Search URL: {search_url}")
response = self.session.get(search_url, timeout=self.timeout)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Look for datasheet PDF links
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() and '.pdf' in href.lower():
full_url = urljoin('https://www.infineon.com', href)
if self._validate_pdf_url(full_url):
return full_url
except Exception as e:
logger.debug(f"Infineon search failed: {e}")
# Try DigiKey (comprehensive distributor with datasheets)
logger.debug(f" Trying DigiKey for CY8C...")
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
search_url = f"https://www.digikey.com/en/products/result?keywords={clean}"
response = self.session.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a', href=True):
href = link['href']
text = link.get_text().lower()
if 'datasheet' in text and '.pdf' in href.lower():
if self._validate_pdf_url(href):
logger.info(f" ✅ Found CY8C PDF via DigiKey: {href}")
return href
except Exception as e:
logger.debug(f" DigiKey search failed: {e}")
# Try Mouser (another comprehensive distributor)
logger.debug(f" Trying Mouser for CY8C...")
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
search_url = f"https://www.mouser.com/c/?q={clean}"
response = self.session.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() and '.pdf' in href.lower():
full_url = urljoin('https://www.mouser.com', href)
if self._validate_pdf_url(full_url):
logger.info(f" ✅ Found CY8C PDF via Mouser: {full_url}")
return full_url
except Exception as e:
logger.debug(f" Mouser search failed: {e}")
# Try Octopart (aggregates multiple distributors)
logger.debug(f" Trying Octopart for CY8C...")
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
search_url = f"https://octopart.com/search?q={clean}"
response = self.session.get(search_url, headers=headers, timeout=5)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a', href=True):
href = link['href']
if 'datasheet' in href.lower() or ('infineon.com' in href and '.pdf' in href.lower()):
full_url = href if href.startswith('http') else urljoin('https://octopart.com', href)
if self._validate_pdf_url(full_url):
logger.info(f" ✅ Found CY8C PDF via Octopart: {full_url}")
return full_url
except Exception as e:
logger.debug(f" Octopart search failed: {e}")
# Last resort: Try Google search for discontinued CY8C parts
logger.debug(f" Trying Google search for discontinued CY8C...")
try:
headers = {