import requests import json import os import re def search_save_and_return(sn): begin_url="https://pro-psurf-app.glb.inc.hp.com/partsurferapi/Search/GenericSearch/"+sn+"/country/CN/usertype/EXT" headers = { 'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'Authorization': 'Basic MjAyMzEzNy1wYXJ0c3VyZmVyOlBTVVJGQCNQUk9E', 'Connection': 'keep-alive', 'Host': 'pro-psurf-app.glb.inc.hp.com', 'Origin': 'https://partsurfer.hp.com', 'Referer': 'https://partsurfer.hp.com/', #'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', #'sec-ch-ua-mobile': '?0', #'sec-ch-ua-platform': '"Windows"', #'Sec-Fetch-Dest': 'empty', #'Sec-Fetch-Mode': 'cors', #'Sec-Fetch-Site': 'same-site', #'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' } r = requests.get(begin_url,headers=headers) jsdata={} if(r.status_code==200): htmlx_byt=r.content htmlx=str(htmlx_byt,'utf-8') jsdata=json.loads(htmlx) datacode=jsdata['Status']['Code'] if(datacode=='S-0001'): f=open("database/"+sn+".json","w") f.write(json.dumps(jsdata, indent=4, separators=(',', ': '))) f.close print("saved") elif(datacode=='E-0099'): jsdata={} print("can not seach the sn") else: print("web error") return jsdata print("HP partsurfer get BuildId and FeatureByte. V0.9") print("compiled by kevin. wuwenfengmi@outlook.com") print("for learning purposes only") if not os.path.exists("database/"): os.makedirs("database/") while_loop=True while while_loop: print("Enter Serial No.\nor Enter (exit)") HP_device_SN=input(":") if HP_device_SN=="exit": while_loop=False else: jdata={} if os.path.exists("database/"+HP_device_SN+".json"): print("fined in database") f=open("database/"+HP_device_SN+".json","r") jdata=json.loads(f.read()) else: print("find in wed") jdata=search_save_and_return(HP_device_SN) if jdata!={}: try: sn=jdata['Body']['SerialNumberBOM']['wwsnrsinput']['serial_no'] pn=jdata['Body']['SerialNumberBOM']['wwsnrsinput']['product_no'] dn=jdata['Body']['SerialNumberBOM']['wwsnrsinput']['user_name'] print("------------------------------------------------") print("Serial No: "+sn) print("Product No: "+pn) print("Device Name: "+dn) print("---") unit_data=jdata['Body']['SerialNumberBOM']['unit_configuration'] for i in unit_data: if i['part_number'].lower()=="IMG_BuildId".lower(): print("Build Id: "+str(i['part_description']).replace("BID=","")) if i['part_number'].lower()=="IMG_Desc1".lower(): Desc1=i['part_description'] if i['part_number'].lower()=="IMG_Desc2".lower(): Desc2=i['part_description'] FeatureByte=Desc1+Desc2 FeatureByte=str(FeatureByte).replace(" ","") print("Feature Byte: "+FeatureByte) #print("Feature Byte 4: ") index=0 for i in FeatureByte: if index==4: index=0 print(" ",end="") index+=1 print(i,end="") print("\n------------------------------------------------\n\n") except: print("Data Error") else: print("Data Error 2") print("Thank for use. pass any key..") os.system('pause')