30350n / inventree_part_import

CLI to import parts from suppliers like DigiKey, LCSC, Mouser, etc. to InvenTree
MIT License
24 stars 8 forks source link

LCSC API Change(?) #47

Closed zenermerps closed 1 month ago

zenermerps commented 1 month ago

Hello,

I noticed that inventree_part_import does not work with LCSC anymore, it prints this error:

warning: failed to get page, retrying in 3s with new session and user agent
warning: failed to get page, retrying in 3s with new session and user agent
warning: failed to get page, retrying in 3s with new session and user agent
warning: failed to search part at LCSC (internal API error)

Upon digging into the source code I found the following URLs used for the API:

API_BASE_URL = "https://wmsc.lcsc.com/wmsc/"
CURRENCY_URL     = f"{API_BASE_URL}home/currency?currencyCode={{}}"
SEARCH_URL       = f"{API_BASE_URL}search/global?keyword={{}}"
PRODUCT_INFO_URL = f"{API_BASE_URL}product/detail?productCode={{}}"

If I build a search query with those URLs myself I get the following response:

{"timestamp":"2024-05-12 00:15:33","status":404,"error":"Not Found","message":"","path":"/wmsc/search/global"}

I did some digging on the LCSC website, and seems the XHR requests of the search bar go to the following URL:

https://wmsc.lcsc.com/ftps/wm/search/global?keyword=C2763970

Which gives this response:

{"code":200,"msg":null,"result":{"isToDetail":true,"tipProductDetailUrlVO":{"catalogName":"Board-to-Board and Backplane Connector","productModel":"YXT-BB10-10P-02","productCode":"C2763970","brandNameEn":"YXT"}}}

With the product detail page at:

https://wmsc.lcsc.com/ftps/wm/product/detail?productCode=C2763970

And the response being

{"code":200,"msg":null,"result":{"currencyType":"USD","isAutoOffsale":false,"productArrange":"Tape & Reel (TR)","productModel":"YXT-BB10-10P-02","reelPrice":0,"stockNumber":15370,"productIntroEn":"0.4mm 2 10P -55℃~+85℃ Brick nogging male 500mA SMD,P=0.4mm  Board-to-Board and Backplane Connector ROHS","productUnit":"Piece","isReel":false,"parentCatalogName":"Connectors","title":"YXT YXT-BB10-10P-02","isHasBattery":false,"overseasStockVO":{"total":15370,"ship3Days":345,"shipImmediately":15025},"pdfUrl":"https://datasheet.lcsc.com/lcsc/2304140030_YXT-YXT-BB10-10P-02_C2763970.pdf","wmStockHk":0,"catalogId":960,"split":5,"stockJs":345,"domesticStockVO":{"total":15370,"ship3Days":345,"shipImmediately":15025},"productPriceList":[{"currencyPrice":0.0812,"discountRate":"1","ladder":5,"usdPrice":0.0812,"currencySymbol":"$","productPrice":"0.0812"},{"currencyPrice":0.0672,"discountRate":"1","ladder":50,"usdPrice":0.0672,"currencySymbol":"$","productPrice":"0.0672"},{"currencyPrice":0.0601,"discountRate":"1","ladder":150,"usdPrice":0.0601,"currencySymbol":"$","productPrice":"0.0601"},{"currencyPrice":0.0549,"discountRate":"1","ladder":500,"usdPrice":0.0549,"currencySymbol":"$","productPrice":"0.0549"},{"currencyPrice":0.0438,"discountRate":"1","ladder":2500,"usdPrice":0.0438,"currencySymbol":"$","productPrice":"0.0438"},{"currencyPrice":0.0417,"discountRate":"1","ladder":5000,"usdPrice":0.0417,"currencySymbol":"$","productPrice":"0.0417"}],"isEnvironment":true,"minPacketUnit":"Reel","minBuyNumber":5,"productId":2895426,"eccn":"-","isForeignOnsale":true,"minPacketNumber":10000,"currencySymbol":"$","weight":0.135,"isPreSale":false,"productWeight":0.000135000,"catalogName":"Board-to-Board and Backplane Connector","productImages":["https://assets.lcsc.com/images/lcsc/900x900/20230127_YXT-YXT-BB10-10P-02_C2763970_front.jpg","https://assets.lcsc.com/images/lcsc/900x900/20230127_YXT-YXT-BB10-10P-02_C2763970_back.jpg","https://assets.lcsc.com/images/lcsc/900x900/20230127_YXT-YXT-BB10-10P-02_C2763970_blank.jpg"],"productCode":"C2763970","stockSz":15025,"encapStandard":"SMD,P=0.4mm","foreignWeight":0E-9,"paramVOList":[{"paramValueEnForSearch":0.4,"paramValueEn":"0.4mm","paramCode":"param_13542_n","isMain":true,"sortNumber":1,"paramNameEn":"Pitch","paramName":"间距","paramValue":"0.4mm"},{"paramValueEnForSearch":2.0,"paramValueEn":"2","paramCode":"param_13543_n","isMain":true,"sortNumber":1,"paramNameEn":"Number of Rows","paramName":"行数","paramValue":"2"},{"paramValueEnForSearch":-1.0,"paramValueEn":"10P","paramCode":"param_13541","isMain":true,"sortNumber":1,"paramNameEn":"Number of Pins","paramName":"PIN总数","paramValue":"10P"},{"paramValueEn":"-55℃~+85℃","paramCode":"param_13549_s","isMain":true,"sortNumber":1,"paramNameEn":"Operating Temperature Range","paramName":"工作温度范围","paramValue":"-55℃~+85℃"},{"paramValueEnForSearch":-1.0,"paramValueEn":"Brick nogging","paramCode":"param_13545","isMain":true,"sortNumber":1,"paramNameEn":"Mounting Style","paramName":"安装方式","paramValue":"立贴"},{"paramValueEnForSearch":-1.0,"paramValueEn":"male","paramCode":"param_13544","isMain":true,"sortNumber":1,"paramNameEn":"Gender","paramName":"公母","paramValue":"公"},{"paramValueEnForSearch":1.0,"paramValueEn":"500mA","paramCode":"param_13546_n","isMain":true,"sortNumber":1,"paramNameEn":"Current Rating (Max)","paramName":"额定电流","paramValue":"500mA"}],"parentCatalogId":365,"brandId":12643,"brandNameEn":"YXT","maxBuyNumber":-1,"isHot":false}}

I am not sure if LCSC has changed the API paths permanently or if the above 404 error is temporary, however I thought I will report on my findings in that matter.

30350n commented 1 month ago

Thanks a lot for the detailed breakdown and PR! ❤️