85 lines
2.8 KiB
Python
Executable File
85 lines
2.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
import read_datasheet
|
|
from alive_progress import alive_bar
|
|
import requests
|
|
|
|
|
|
bartext = ""
|
|
|
|
def try_download_datasheet(partnum):
|
|
global bartext
|
|
|
|
sanitized_name = partnum.replace(" ", "")
|
|
url = "https://catalog.belden.com/techdata/EN/" + sanitized_name + "_techdata.pdf"
|
|
#print(url)
|
|
try:
|
|
with requests.get(url, stream=True) as r:
|
|
#r.raise_for_status()
|
|
if r.headers.get("Content-Type") != "application/pdf":
|
|
return False
|
|
if r.status_code == 404:
|
|
return False
|
|
os.mkdir(partnum)
|
|
with open(partnum + "/datasheet.pdf", 'wb') as f:
|
|
for chunk in r.iter_content(chunk_size=131072):
|
|
# If you have chunk encoded response uncomment if
|
|
# and set chunk_size parameter to None.
|
|
#if chunk:
|
|
bartext = bartext + "."
|
|
bar.text = bartext
|
|
f.write(chunk)
|
|
#print("")
|
|
return sanitized_name + ".pdf"
|
|
except KeyboardInterrupt:
|
|
print("Quitting!")
|
|
os.remove(partnum + "/datasheet.pdf")
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
partnums = ["10GXS12", "RST 5L-RKT 5L-949",
|
|
"10GXS13",
|
|
"10GXW12",
|
|
"10GXW13",
|
|
"2412",
|
|
"2413",
|
|
"OSP6AU",
|
|
"FI4D024P9",
|
|
"FISD012R9",
|
|
"FDSD012A9",
|
|
"FSSL024NG",
|
|
"FISX006W0",
|
|
]
|
|
with alive_bar(len(partnums) * 2, dual_line=True, calibrate=30, bar="classic2", spinner="classic") as bar:
|
|
for partnum in partnums:
|
|
path = partnum + "/datasheet.pdf"
|
|
bartext = "Downloading datasheet for part " + partnum
|
|
bar.text = bartext
|
|
if os.path.exists(path) and os.path.getsize(path) > 1:
|
|
print("Using cached " + path, end='')
|
|
bar.text = "Using cached " + path
|
|
bar(skipped=True)
|
|
print("Parsing Datasheet contents of " + path, end='')
|
|
bar.text = "Parsing Datasheet contents of " + partnum + ".pdf..."
|
|
read_datasheet.parse(path, partnum)
|
|
bar(skipped=False)
|
|
elif try_download_datasheet(partnum) is not False:
|
|
print("Downloaded " + path, end='')
|
|
bar.text = "Downloaded " + path
|
|
bar(skipped=False)
|
|
print("Parsing Datasheet contents of " + path, end='')
|
|
bar.text = "Parsing Datasheet contents of " + partnum + ".pdf..."
|
|
read_datasheet.parse(path, partnum)
|
|
bar(skipped=False)
|
|
else:
|
|
print("Failed to download datasheet for part " + partnum, end='')
|
|
bar.text = "Failed to download datasheet for part " + partnum
|
|
bar(skipped=True)
|
|
bar(skipped=True)
|
|
|