Skip to content

Instantly share code, notes, and snippets.

@maciek134
Created May 17, 2020 18:14
Show Gist options
  • Save maciek134/3a30e04f5a96cbf873432d8b733ab679 to your computer and use it in GitHub Desktop.
Save maciek134/3a30e04f5a96cbf873432d8b733ab679 to your computer and use it in GitHub Desktop.
Search JLCPCB SMT parts library from CLI (using Deno)
#!/usr/bin/env -Sdeno run --allow-net
import { parse } from "https://deno.land/std/flags/mod.ts";
import { red, bold, green, blue } from "https://deno.land/std/fmt/colors.ts";
import { sprintf } from "https://deno.land/std/fmt/sprintf.ts";
export interface ComponentPrice {
endNumber: number;
productPrice: number;
startNumber: number;
}
export interface Part {
componentBrandEn: string;
componentCode: string;
componentImageUrl: string;
componentLibraryType: string;
componentModelEn: string;
componentPrices: ComponentPrice[];
componentSource: string;
componentSpecificationEn: string;
componentTypeEn: string;
dataManualUrl: string;
describe: string;
erpComponentName: string;
firstSortAccessId: string;
lcscGoodsUrl: string;
minImage: string;
secondSortAccessId: string;
stockCount: number;
}
const args = parse(Deno.args);
const res = await fetch(`https://jlcpcb.com/shoppingCart/smtGood/selectSmtComponentList`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: [
`keyword=${args._.join(' ')}`,
args.basic ? 'componentLibraryType=base' : '',
].join('&'),
});
const json = await res.json();
let parts: Part[] = json.data.list;
if (args.sort) {
parts.sort((a, b) => {
if (args.sort === 'price') {
return a.componentPrices[0].productPrice - b.componentPrices[0].productPrice;
}
return 0;
});
}
if (args.available) {
parts = parts.filter(part => part.stockCount > 0);
}
console.log(
sprintf(`${bold('%36s')} | ${bold('%5s')} | ${bold('%20s')} | ${bold('%8s')} | ${bold('%8s')}`,
'model',
'basic',
'package',
'stock',
'price',
)
);
parts.forEach(part => {
const packageSplit = part.componentSpecificationEn.split('_');
console.log(
sprintf(`${green('%36s')} | ${bold('%5t')} | ${blue('%20s')} | ${bold('%8d')} | ${bold('$%2.4f')} @ %d`,
part.componentModelEn,
part.componentLibraryType === 'base',
packageSplit[0] + (packageSplit.length > 1 ? '_' + packageSplit[1] : ''),
part.stockCount,
part.componentPrices[0].productPrice,
part.componentPrices[0].startNumber,
),
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment