From 42f79c03d49b5d8e8a3760f5b3abba8bf159a68b Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Sun, 28 Feb 2021 10:33:28 +0100 Subject: [PATCH] Add Micro Center support (#2) As a good start, only the "shippable items" are parsed. Next enhancement would be to configure local shops. Signed-off-by: Julien Riou --- parser.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/parser.go b/parser.go index b3b67ea..2bbddff 100644 --- a/parser.go +++ b/parser.go @@ -124,6 +124,8 @@ func createQuery(shopName string, url string) (string, error) { return createQueryForLDLC(url), nil case "materiel.net": return createQueryForMaterielNet(url), nil + case "microcenter.com": + return createQueryForMicroCenter(url), nil case "mediamarkt.ch": return createQueryForMediamarktCh(url), nil case "topachat.com": @@ -333,3 +335,42 @@ RETURN FLATTEN(results) ` return q } + +func createQueryForMicroCenter(url string) string { + q := ` +LET first_page = '` + url + `' +LET doc = DOCUMENT(first_page, {driver: "cdp"}) +LET base_url = 'https://www.microcenter.com' + +LET next_pages = ( + FOR a IN ELEMENTS(doc, "div .pagination .pages a") + RETURN base_url + a.attributes.href +) + +LET pages = SORTED_UNIQUE(APPEND(next_pages, first_page)) + +LET results = ( + FOR page IN pages + NAVIGATE(doc, page) + LET products = ( + FOR el IN ELEMENTS(doc, "div .products .product_wrapper") + LET details = ELEMENT(el, "h2") + LET name = INNER_TEXT(details) + LET url = ELEMENT(details, "a").attributes.href + LET price = TO_FLOAT(ELEMENT(details, "a").attributes."data-price") + LET available = LENGTH(ELEMENTS(el, "div .price_wrapper form .STBTN"))>0 + RETURN { + name: name, + url: base_url + url, + price: price, + price_currency: "USD", + available: available, + } + ) + RETURN products + ) + +RETURN FLATTEN(results) + ` + return q +}