#!/usr/bin/env python3 import os import sys import read_datasheet from alive_progress import alive_bar import requests #import time import json import subprocess from util import fprint bartext = "" failed = [] def check_internet(url='https://belden.com', timeout=5): try: # Make a GET request to the specified URL response = requests.get(url, timeout=timeout) # If the request succeeds, return True return True except requests.ConnectionError: # If a connection error occurs, return False return False def query_search(partnum, source): """token_url = "https://www.belden.com/coveo/rest/token?t=" + str(int(time.time())) with requests.get(token_url) as r: out = json.loads(r.content) token = out["token"] search_url = "https://www.belden.com/coveo/rest/search" search_data ='{ "q": "' + str(partnum) + '", "sortCriteria": "relevancy", "numberOfResults": "250", "sortCriteria": "@catalogitemwebdisplaypriority ascending", "searchHub": "products-only-search", "pipeline": "Site Search", "maximumAge": "900000", "tab": "products-search", "locale": "en" }' #"aq": "", "cq": "((@z95xlanguage==en) (@z95xlatestversion==1) (@source==\\"Coveo_web_index - rg-nc-prod-sitecore-prod\\")) OR (@source==(\\"website_001002_catalog_index-rg-nc-prod-sitecore-prod\\",\\"website_001002_Category_index-rg-nc-prod-sitecore-prod\\"))", "firstResult": "0", "categoryFacets": "[{\\"field\\":\\"@catalogitemcategories\\",\\"path\\":[],\\"injectionDepth\\":1000,\\"maximumNumberOfValues\\":6,\\"delimitingCharacter\\":\\"|\\"}]", "facetOptions": "{}", "groupBy": "" }' #fprint(search_data) fprint(json.loads(search_data)) #search_data = '{ "q": "' + str(partnum) + '" }' fprint(search_data) headers = headers = { 'Authorization': f'Bearer {token}', 'Content-Type': 'application/json' } with requests.post(search_url, headers=headers, data=search_data) as r: fprint(r.text)""" # TODO: Reimplement in python # Bash script uses some crazy json formatting that I could not figure out # Despite the fact that I wrote it # So I'll just leave it, becuase it works. if source == "Belden": command = ["./query-search.sh", partnum] result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) if result.returncode != 0: # error fprint("No results found in search database for " + partnum + ". No hi-res part image available.", result.stderr) return False else: data_out = json.loads(result.stdout) return data_out elif source == "Alphawire": alphaurl = "https://www.alphawire.com//sxa/search/results/?l=en&s={4A774076-6068-460C-9CC6-A2D8E85E407F}&itemid={BF82F58C-EFD9-4D8B-AE3E-097DD12CF7DA}&sig=&autoFireSearch=true&productpartnumber=*" + partnum + "*&v={B22CD56D-AB95-4048-8AA1-5BBDF2F2D17F}&p=10&e=0&o=ProductPartNumber%2CAscending" r = requests.get(url=alphaurl) data = r.json() output = dict() #print(data) try: if data["Count"] > 0: print(data["Results"][0]["Url"]) result = data["Results"][0] if result["Url"].split("/")[-1] == partnum: #print(partnum) print(result["Html"]) try: imgidx = result["Html"].index(" 1): # Use query search_result = query_search(partnum.replace(" ", ""), dstype) # Try to use belden.com search 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): fprint("Downloaded hi-res part image for " + partnum) touch(output_dir + "/found_part_hires") else: fprint("Using cached hi-res part image for " + partnum) # Download datasheet from provided URL if needed if os.path.exists(path) and os.path.getsize(path) > 1: __use_cached_datasheet(partnum, path, output_dir, dstype) elif _download_datasheet(search_result["datasheet"], output_dir) is not False: __downloaded_datasheet(partnum, path, output_dir, dstype) elif os.path.exists(path) and os.path.getsize(path) > 1: __use_cached_datasheet(partnum, path, output_dir, dstype) # 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: __downloaded_datasheet(partnum, path, output_dir, dstype) # Failed to download with search or guess :( else: fprint("Failed to download datasheet for part " + partnum) bar.text = "Failed to download datasheet for part " + partnum failed.append(partnum) bar(skipped=True) bar(skipped=True) # We already have a hi-res image and the datasheet - perfect! else: fprint("Using cached hi-res part image for " + partnum) __use_cached_datasheet(partnum, path, output_dir, dstype) if len(failed) > 0: fprint("Failed to download:") for partnum in failed: fprint(partnum) return False # Go to manual review upload page else: return True # All cables downloaded; we are good to go if __name__ == "__main__": partnums = ["AW3248", "BL7958A", "BL10GXS12", "BLRST 5L-RKT 5L-949", "BL10GXS13", "BL10GXW12", "BL10GXW13", "BL2412", "BL2413", "BLOSP6AU", "BLFI4D024P9", "BLFISD012R9", "BLFDSD012A9", "BLFSSL024NG", "BLFISX006W0", "BLFISX00103", "BLC6D1100007" ] #get_multi(partnums) query_search("3248", "Alphawire")