Move functions inside, as they depend on being called there anyway

This commit is contained in:
Cole Deck 2024-01-05 20:26:16 -06:00
parent 42425d4681
commit 72804146ab

View File

@ -23,88 +23,7 @@ def check_internet(url='https://belden.com', timeout=5):
# If a connection error occurs, return False
return False
def try_download_datasheet(partnum, output_dir): # Guess datasheet URL
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.makedirs(output_dir, exist_ok=True)
with open(output_dir + "/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 output_dir + "/datasheet.pdf"
except KeyboardInterrupt:
print("Quitting!")
os.remove(partnum + "/datasheet.pdf")
sys.exit()
def download_datasheet(url, output_dir): # Download datasheet with known URL
global bartext
#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.makedirs(output_dir, exist_ok=True)
with open(output_dir + "/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 output_dir + "/datasheet.pdf"
except KeyboardInterrupt:
print("Quitting!")
os.remove(partnum + "/datasheet.pdf")
sys.exit()
def download_image(url, output_dir): # Download datasheet with known URL
global bartext
#print(url)
try:
with requests.get(url, stream=True) as r:
#r.raise_for_status()
if r.status_code == 404:
return False
os.makedirs(output_dir, exist_ok=True)
with open(output_dir + "/part-hires." + url.split(".")[-1], '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 output_dir + "/part-hires." + url.split(".")[-1]
except KeyboardInterrupt:
print("Quitting!")
os.remove(partnum + "/datasheet.pdf")
sys.exit()
def query_search(partnum):
"""token_url = "https://www.belden.com/coveo/rest/token?t=" + str(int(time.time()))
@ -148,6 +67,89 @@ def touch(path):
def get_multi(partnums):
with alive_bar(len(partnums) * 2, dual_line=True, calibrate=30, bar="classic2", spinner="classic") as bar:
def _try_download_datasheet(partnum, output_dir): # Guess datasheet URL
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.makedirs(output_dir, exist_ok=True)
with open(output_dir + "/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 output_dir + "/datasheet.pdf"
except KeyboardInterrupt:
print("Quitting!")
os.remove(output_dir + "/datasheet.pdf")
sys.exit()
def _download_datasheet(url, output_dir): # Download datasheet with known URL
global bartext
#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.makedirs(output_dir, exist_ok=True)
with open(output_dir + "/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 output_dir + "/datasheet.pdf"
except KeyboardInterrupt:
print("Quitting!")
os.remove(output_dir + "/datasheet.pdf")
sys.exit()
def _download_image(url, output_dir): # Download datasheet with known URL
global bartext
#print(url)
try:
with requests.get(url, stream=True) as r:
#r.raise_for_status()
if r.status_code == 404:
return False
os.makedirs(output_dir, exist_ok=True)
with open(output_dir + "/part-hires." + url.split(".")[-1], '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 output_dir + "/part-hires." + url.split(".")[-1]
except KeyboardInterrupt:
print("Quitting!")
os.remove(partnum + "/datasheet.pdf")
sys.exit()
def __use_cached_datasheet(partnum, path, output_dir):
print("Using cached datasheet for " + partnum, end='')
bar.text = "Using cached datasheet for " + partnum
@ -179,7 +181,7 @@ def get_multi(partnums):
if search_result is not False:
# Download high resolution part image if available and needed
if not os.path.exists(output_dir + "/found_part_hires"):
if download_image(search_result["image"], output_dir):
if _download_image(search_result["image"], output_dir):
print("Downloaded hi-res part image for " + partnum)
touch(output_dir + "/found_part_hires")
else:
@ -189,14 +191,14 @@ def get_multi(partnums):
if os.path.exists(path) and os.path.getsize(path) > 1:
__use_cached_datasheet(partnum, path, output_dir)
elif download_datasheet(search_result["datasheet"], output_dir) is not False:
elif _download_datasheet(search_result["datasheet"], output_dir) is not False:
__downloaded_datasheet(partnum, path, output_dir)
elif os.path.exists(path) and os.path.getsize(path) > 1:
__use_cached_datasheet(partnum, path, output_dir)
# If search fails, and we don't already have the datasheet, guess datasheet URL and skip the hires image download
elif try_download_datasheet(partnum, output_dir) is not False:
elif _try_download_datasheet(partnum, output_dir) is not False:
__downloaded_datasheet(partnum, path, output_dir)
# Failed to download with search or guess :(