Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix product updates #74

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/nerves_metal_detector/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule NervesMetalDetector.Application do
{Cluster.Supervisor, [topologies, [name: NervesMetalDetector.ClusterSupervisor]]},
NervesMetalDetectorWeb.Telemetry,
NervesMetalDetector.Repo,
{DNSCluster, query: Application.get_env(:nerves_metal_detector, :dns_cluster_query) || :ignore},
{DNSCluster,
query: Application.get_env(:nerves_metal_detector, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: NervesMetalDetector.PubSub},
{Finch, name: NervesMetalDetector.Finch},
NervesMetalDetectorWeb.Endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ defmodule NervesMetalDetector.Inventory.Data.ProductUpdateItems.ElektorNl do
alias NervesMetalDetector.Vendors.ElektorNl.ProductUpdate

@items [
# RPi Zero
%ProductUpdate{url: "https://www.elektor.nl/raspberry-pi-zero-w", sku: "SC0020"},

# RPi Zero 2
%ProductUpdate{
url:
"https://www.elektor.nl/raspberry-pi-zero-2-wh-with-pre-soldered-40-pin-color-coded-gpio-header",
sku: "SC0510WH"
},
%ProductUpdate{url: "https://www.elektor.nl/raspberry-pi-zero-2-w", sku: "SC0510"}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ defmodule NervesMetalDetector.Inventory.Data.ProductUpdateItems.PiShopCh do
},

# RPi Zero
%ProductUpdate{url: "https://www.pi-shop.ch/raspberry-pi-zero-w-1811", sku: "SC0020"},
%ProductUpdate{url: "https://www.pi-shop.ch/raspberry-pi-zero-w", sku: "SC0020"},
%ProductUpdate{
url: "https://www.pi-shop.ch/raspberry-pi-zero-wh-header-geloetet",
sku: "SC0020WH"
},

# RPi Zero 2
%ProductUpdate{url: "https://www.pi-shop.ch/raspberry-pi-zero-2-w", sku: "SC0510"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ defmodule NervesMetalDetector.Inventory.Data.ProductUpdateItems.WelectronDe do

# RPi Zero
%ProductUpdate{
url: "https://www.welectron.com/Raspberry-Pi-Zero-W-mit-loser-Stiftleiste",
url: "https://www.welectron.com/Raspberry-Pi-Zero-W",
sku: "SC0020"
},
%ProductUpdate{
url: "https://www.welectron.com/Raspberry-Pi-Zero-WH-mit-verloeteter-Stiftleiste",
url: "https://www.welectron.com/Raspberry-Pi-Zero-WH",
sku: "SC0020WH"
},

Expand Down
63 changes: 42 additions & 21 deletions lib/nerves_metal_detector/vendors/semaf_at.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ defimpl NervesMetalDetector.Inventory.ProductAvailability.Fetcher,
]
]

# Semaf has "application/ld+json" data but in a wrong encoding and Jason cannot parse it,
# so we parse the HTML instead.
with {:load_body, {:ok, %{body: body}}} when body not in [nil, ""] <-
{:load_body, HTTPoison.get(url, [], options)},
{:parse_document, parsed} when parsed not in [nil, []] <-
{:parse_document, Floki.parse_document!(body)},
{:parse_json_info, json_info} when json_info not in [%{}] <-
{:parse_json_info, parse_json_info(parsed)},
{:parse_product_offer, product_offer} when product_offer not in [nil, []] <-
{:parse_product_offer, parse_product_offer(parsed)},
{:parse_currency, currency} when not is_nil(currency) <-
{:parse_currency, parse_currency(product_offer)},
{:parse_currency, parse_currency(json_info)},
{:parse_price, price} when not is_nil(price) <-
{:parse_price, parse_price(product_offer)},
{:parse_item_url, item_url} when not is_nil(item_url) <-
{:parse_item_url, parse_item_url(product_offer)},
{:parse_stock, {in_stock, items_in_stock}} <- {:parse_stock, parse_stock(product_offer)} do
{:parse_price, parse_price(json_info)},
{:parse_in_stock, in_stock} <- {:parse_in_stock, parse_in_stock(json_info)},
{:parse_items_in_stock, items_in_stock} <-
{:parse_items_in_stock, parse_items_in_stock(product_offer)} do
data = %{
sku: sku,
vendor: SemafAt.vendor_info().id,
url: item_url,
url: url,
in_stock: in_stock,
items_in_stock: items_in_stock,
price: Money.new!(String.to_atom(currency), price)
Expand All @@ -69,39 +69,60 @@ defimpl NervesMetalDetector.Inventory.ProductAvailability.Fetcher,
end
end

defp parse_json_info(html_tree) do
html_tree
|> Floki.find("[type=\"application/ld+json\"]")
|> Enum.map(fn item ->
parse_result =
item
|> Floki.children()
|> Enum.at(0)
|> String.replace("\r", "")
|> String.replace("\n", "")
|> Jason.decode()

case parse_result do
{:ok, parsed} -> parsed
_ -> %{}
end
end)
|> Enum.reduce(&Map.merge(&2, &1))
end

defp parse_product_offer(html_tree) do
Floki.find(html_tree, "#product-offer .product-offer")
end

defp parse_currency(html_tree) do
Floki.find(html_tree, "[itemprop=priceCurrency]") |> Floki.attribute("content") |> Enum.at(0)
defp parse_currency(json_info) do
get_in(json_info, ["offers", "priceCurrency"])
end

defp parse_price(html_tree) do
Floki.find(html_tree, "[itemprop=price]") |> Floki.attribute("content") |> Enum.at(0)
defp parse_price(json_info) do
get_in(json_info, ["offers", "price"])
end

defp parse_item_url(html_tree) do
Floki.find(html_tree, "[itemprop=url]") |> Enum.at(0) |> Floki.attribute("href") |> Enum.at(0)
defp parse_in_stock(json_info) do
case get_in(json_info, ["offers", "availability"]) do
"http://schema.org/InStock" -> true
"https://schema.org/InStock" -> true
_ -> false
end
end

defp parse_stock(html_tree) do
children = Floki.find(html_tree, "#stock") |> Enum.at(0) |> Floki.children()
defp parse_items_in_stock(html_tree) do
children = Floki.find(html_tree, ".delivery-status .status") |> Enum.at(0) |> Floki.children()

with children when children not in [nil, []] <- children,
classes <- Floki.attribute(children, "class") |> Enum.at(0) |> String.split(" "),
text <- Floki.text(children) do
in_stock = "status-2" in classes

items_in_stock =
case Integer.parse(String.trim(text)) do
{count, _} -> count
_ -> nil
end

{in_stock, items_in_stock}
items_in_stock
else
value -> {:error, value}
_ -> nil
end
end
end
17 changes: 15 additions & 2 deletions lib/nerves_metal_detector/vendors/sparkfun_us.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ defimpl NervesMetalDetector.Inventory.ProductAvailability.Fetcher,
{:parse_price, parse_price(product)},
{:parse_item_url, item_url} when not is_nil(item_url) <-
{:parse_item_url, parse_item_url(parsed)},
{:parse_in_stock, in_stock} <- {:parse_in_stock, parse_in_stock(product)} do
{:parse_in_stock, in_stock} <- {:parse_in_stock, parse_in_stock(product)},
{:parse_items_in_stock, items_in_stock} <-
{:parse_items_in_stock, parse_items_in_stock(product)} do
data = %{
sku: sku,
vendor: SparkfunUs.vendor_info().id,
url: item_url,
in_stock: in_stock,
items_in_stock: nil,
items_in_stock: items_in_stock,
price: Money.new!(String.to_atom(currency), price)
}

Expand Down Expand Up @@ -98,4 +100,15 @@ defimpl NervesMetalDetector.Inventory.ProductAvailability.Fetcher,
_ -> false
end
end

defp parse_items_in_stock(html_tree) do
with available when available not in [nil, []] <-
Floki.find(html_tree, ".quantity-row strong"),
text when is_binary(text) <- Floki.text(available),
{value, _} <- Integer.parse(text) do
value
else
_ -> nil
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ defmodule NervesMetalDetectorWeb.CoreComponents do
Attempting to reconnect <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
</.flash>

<.flash
<.flash
id="server-error"
kind={:error}
title="Something went wrong!"
Expand Down
Loading