-- Built-in plugin for Amazon product pages (de, co.uk, fr, it, es, nl, com). -- -- It uses only the public plugin API, so it doubles as the reference for -- writing your own: see "Writing a plugin" in README.md. local tlds = { ["de"] = "EUR", ["fr"] = "EUR", ["it"] = "EUR", ["es"] = "EUR", ["nl"] = "EUR", ["co.uk"] = "GBP", ["com"] = "USD", } -- Amazon's own merchant IDs. A page sold by Amazon carries one of them in -- the buybox form. local amazon_merchants = { A3JWKAKR8XB7XF = true, -- amazon.de A3P5ROKL5A1OLE = true, -- amazon.co.uk A1X6FK5RDHNB96 = true, -- amazon.fr A11IL2PNWYJU7H = true, -- amazon.it A1AT7YVPFBWXBL = true, -- amazon.es A17D2BRD4YMT0X = true, -- amazon.nl ATVPDKIKX0DER = true, -- amazon.com } -- Any of these buttons means a new item can go into the cart. The preorder -- selectors are a best guess until we have a real preorder page. -- "#add-to-cart-button-ubb" is deliberately missing: it belongs to the used -- buybox, which Amazon shows when only used copies are left. local buy_buttons = table.concat({ "#add-to-cart-button", "#buy-now-button", "input[name='submit.add-to-cart']", "input[name='submit.buy-now']", "input[name='submit.preorder']", "#preorder-button", }, ", ") -- Where Amazon says an item can't be bought. local availability_area = { "#availability", "#outOfStock", "#buybox" } local unavailable_phrases = { "currently unavailable", "derzeit nicht verfügbar", "actuellement indisponible", "non disponibile", "no disponible", "momenteel niet verkrijgbaar", } -- Older layouts say who sells and ships the item in one sentence, such as -- "Versand und Verkauf durch Amazon." or "Verkauf durch X und Versand durch -- Amazon." local sold_by_amazon_phrases = { "sold by amazon", "verkauf durch amazon", "vendu par amazon", "venduto da amazon", "venduto e spedito da amazon", "vendido por amazon", "vendido y enviado por amazon", "verkocht door amazon", } local shipped_by_amazon_phrases = { "fulfilled by amazon", "ships from amazon", "dispatched from amazon", "versand durch amazon", "expédié par amazon", "spedito da amazon", "enviado por amazon", "verzonden door amazon", } local price_selectors = { "#corePrice_feature_div .a-price .a-offscreen", "#corePriceDisplay_desktop_feature_div .priceToPay .a-offscreen", "#apex_desktop .priceToPay .a-offscreen", ".priceToPay .a-offscreen", "#price_inside_buybox", "#priceblock_ourprice", "#priceblock_dealprice", } -- split returns the TLD and ASIN of an Amazon product URL, or nil. local function split(url) local host = url:match("^[Hh][Tt][Tt][Pp][Ss]?://([^/?#]+)") if not host then return nil end local tld = host:lower():match("^www%.amazon%.(.+)$") or host:lower():match("^amazon%.(.+)$") if not tld or not tlds[tld] then return nil end local m = regex_match(url, "(?i)/(?:dp|gp/product|gp/aw/d|exec/obidos/asin)/([a-z0-9]{10})(?:[/?#]|$)") if not m then return nil end return tld, m[1]:upper() end -- Amazon itself appears as "Amazon", "Amazon.de", "Amazon EU S.à r.l." and -- similar. Shops called "Amazonas Games" must not count. local function is_amazon(name) if not name then return false end local n = name:lower():gsub("^%s+", ""):gsub("[%s%.]+$", "") return n == "amazon" or n:match("^amazon%.[%a%.]+$") ~= nil or n:match("^amazon eu") ~= nil or n:match("^amazon media eu") ~= nil end local function legacy_merchant_info(doc) return doc:text("#merchant-info") or doc:text("#merchantInfoFeature_feature_div") end -- sold_by_amazon is true or false when the page says who sells the item, and -- nil when it doesn't, e.g. when there is no offer at all. local function sold_by_amazon(doc) -- The buybox form names the merchant whose offer the buttons buy. local id = doc:attr("input#merchantID, input[name='merchantID']", "value") if id and id ~= "" then return amazon_merchants[id] == true end -- Newer layout: a "Verkäufer" / "Sold by" row. local name = doc:text("#merchantInfoFeature_feature_div .offer-display-feature-text-message") if name then return is_amazon(name) end local info = legacy_merchant_info(doc) if info then return contains_any(info, sold_by_amazon_phrases) end if doc:exists("#sellerProfileTriggerId") then return false end return nil end local function shipped_by_amazon(doc) -- Newer layout: a "Versand" / "Ships from" row. local name = doc:text("#fulfillerInfoFeature_feature_div .offer-display-feature-text-message") if name then return is_amazon(name) end return contains_any(legacy_merchant_info(doc), shipped_by_amazon_phrases) end local function seller(doc) local sold = sold_by_amazon(doc) if sold == nil then return "unknown" end if sold then return "first_party" end if shipped_by_amazon(doc) then return "fulfilled" end return "third_party" end local function unavailable(doc) for _, sel in ipairs(availability_area) do if contains_any(doc:text(sel), unavailable_phrases) then return true end end return false end local function find_price(doc) for _, sel in ipairs(price_selectors) do local value, currency = parse_price(doc:text(sel)) if value then return value, currency end end return nil end return { name = "amazon", matches = function(url) return split(url) ~= nil end, -- Every variant of a product URL becomes https://www.amazon./dp/, -- so the same item is never watched twice. normalize = function(url) local tld, asin = split(url) if not tld then return url end return "https://www.amazon." .. tld .. "/dp/" .. asin end, parse = function(doc, url) local tld, asin = split(url) local title = doc:text("#productTitle") local value, currency = find_price(doc) return { title = title, -- A dog page or a search result has no product title, so it is -- never available. available = title ~= nil and doc:exists(buy_buttons) and not unavailable(doc), price = value, currency = currency or (tld and tlds[tld]), seller = seller(doc), id = asin, } end, cart_link = function(url, id) local tld, asin = split(url) if not tld then return nil end return "https://www.amazon." .. tld .. "/gp/aws/cart/add.html?ASIN.1=" .. (id or asin) .. "&Quantity.1=1" end, }