Skip to content
Draft
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
4 changes: 4 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if File.exists?("~/.iex.exs") do
import_file("~/.iex.exs")
end

alias ExFleetYards.Repo
alias ExFleetYards.Repo.Game

Expand Down
2 changes: 2 additions & 0 deletions apps/ex_fleet_yards_import/lib/ex_fleet_yards_import.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ defmodule ExFleetYardsImport do
quote do
@behaviour ExFleetYardsImport

alias ExFleetYards.Repo

@data_source unquote(data_source)
@data_name unquote(data_name)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,186 @@
defmodule ExFleetYardsImport.Importer.Paint do
@moduledoc "Importer for paints"

require Logger

alias ExFleetYards.Repo.Game

import Ecto.Query

use ExFleetYardsImport, data_source: :web_rsi, data_name: :paints

@impl ExFleetYardsImport

def import_data(_opts) do
{:error, :not_implemented}
items =
load_items()
|> Enum.map(&resolve_model/1)
|> Enum.filter(fn item -> filter_packs(item) && filter_without_models(item) end)

{:ok, items}
end

defp resolve_model(item) do
{model_name, paint_name} = extract_name(item["name"])

mapping = model_name_mapping(model_name)

models = load_models(model_name, mapping)

%{
name: paint_name,
model_name: model_name,
models: models,
mapping: mapping,
rsi_name: item["name"],
image: String.replace(item["media"]["thumbnail"]["storeSmall"], "store_small", "source"),
price: item["nativePrice"]["amount"],
discountedPrice: item["nativePrice"]["discounted"]
}
end

defp extract_name(paint_shop_name) do
captures = Regex.named_captures(~r/(?<model>.+)\s+-\s+(?<paint>.+)/, paint_shop_name)

if captures do
{String.trim(captures["model"]), String.trim(captures["paint"])}
else
{nil, paint_shop_name}
end
end

defp load_models(name, mapping)

defp load_models(nil, _) do
[]
end

defp load_models(name, mapping) do
from(m in Game.Model,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be moved to ExFleetYards.Repo.Game.Model as function there, but also fine here.

where: ilike(m.name, ^"#{name}%") or m.name in ^mapping,
select: {m.name, m.id}
)
|> Repo.all()
end

defp model_name_mapping(nil) do
[]
end

defp model_name_mapping(name) when name != nil do
mapping = %{
"100 series" => ["100i", "125a", "135c"],
"nova tank" => ["Nova"],
"hercules starlifter" => ["A2 Hercules", "C2 Hercules", "M2 Hercules"],
"hornet" => [
"F7C Hornet",
"F7C Hornet Wildfire",
"F7C-M Super Hornet",
"F7C-M Super Hornet Heartseeker",
"F7C-R Hornet Tracker",
"F7C-S Hornet Ghost"
]
}

Map.get(mapping, String.downcase(name), [])
end

defp filter_packs(item) do
not String.contains?(item.name, "Pack")
end

defp filter_without_models(item) do
if Enum.empty?(item.models) do
Logger.debug("No Models for: #{item.rsi_name}")
Logger.debug(item)
false
else
true
end
end

defp load_items(limit \\ 20, page \\ 1, items \\ []) do
page_items = load_page(page, limit)

if limit == length(page_items) do
load_items(limit, page + 1, items ++ page_items)
else
items ++ page_items
end
end

defp load_page(page, limit) do
Logger.debug("Loading page: #{page}")

{status, response} =
Neuron.query(
query(),
variables(page, limit),
url: graphql_endpoint()
)

if status == :ok do
response.body["data"]["store"]["listing"]["resources"]
else
[]
end
end

defp variables(page, limit) do
%{
query: %{
skus: %{
products: [
"268"
]
},
limit: limit,
page: page,
sort: %{
field: "weight",
direction: "desc"
}
}
}
end

defp graphql_endpoint do
ExFleetYards.Config.get(:ex_fleet_yards_import, :rsi_graphql_url, nil)
end

defp query do
"""
query GetBrowseListingQuery($query: SearchQuery) {
store(browse: true) {
listing: search(query: $query) {
resources {
id
slug
name
title
subtitle
url
body
excerpt
type
media {
thumbnail {
slideshow
storeSmall
}
list {
slideshow
}
}
nativePrice {
amount
discounted
discountDescription
}
}
}
}
}
"""
end
end
3 changes: 2 additions & 1 deletion apps/ex_fleet_yards_import/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule ExFleetYardsImport.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ex_fleet_yards, in_umbrella: true}
{:ex_fleet_yards, in_umbrella: true},
{:neuron, "~> 5.0.0"}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs an update to the nix/mix.nix file to not break nix builds.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a task to regenerate the mix.nix?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a task, but currently seems broken, mix2nix > nix/mix.nix is how I generated it (having mix2nix installed via the devenv)

# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}
Expand Down
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ config :ex_fleet_yards_api, ExFleetYardsApi.Endpoint,
server: false,
render_errors: [view: ExFleetYardsApi.ErrorView, accepts: ~w(json), layout: false]

config :ex_fleet_yards_import, rsi_graphql_url: "https://robertsspaceindustries.com/graphql"

# Configure esbuild (the version is required)
config :esbuild,
version: "0.14.29",
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"floki": {:hex, :floki, "0.34.0", "002d0cc194b48794d74711731db004fafeb328fe676976f160685262d43706a8", [:mix], [], "hexpm", "9c3a9f43f40dde00332a589bd9d389b90c1f518aef500364d00636acc5ebc99c"},
"gettext": {:hex, :gettext, "0.22.0", "a25d71ec21b1848957d9207b81fd61cb25161688d282d58bdafef74c2270bdc4", [:mix], [{:expo, "~> 0.3.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "cb0675141576f73720c8e49b4f0fd3f2c69f0cd8c218202724d4aebab8c70ace"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"influxql": {:hex, :influxql, "0.2.1", "71bfd5c0d81bf870f239baf3357bf5226b44fce16e1b9399ba1368203ca71245", [:mix], [], "hexpm", "75faf04960d6830ca0827869eaac1ba092655041c5e96deb2a588bafb601205c"},
"instream": {:hex, :instream, "2.2.0", "54f040ca7eeb7604212ff55d33a9d94c879f3db0d00f47451a528393ff55d88d", [:mix], [{:hackney, "~> 1.1", [hex: :hackney, repo: "hexpm", optional: false]}, {:influxql, "~> 0.2.0", [hex: :influxql, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "77d6f3970fc1e1219dc39f537da47bf6aab7be024422f9e6defccf669ea87d42"},
Expand All @@ -36,6 +37,7 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"neuron": {:hex, :neuron, "5.0.0", "64c6b14138e4f6e61a55abb0bb95659aa193145ed9baf80b4e760d4c189f6c6f", [:mix], [{:httpoison, "~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "7597308e22e34f77acaadbae3da63974edc9e4041ef7f7154d45ce80c0e33d00"},
"nimble_csv": {:hex, :nimble_csv, "1.2.0", "4e26385d260c61eba9d4412c71cea34421f296d5353f914afe3f2e71cce97722", [:mix], [], "hexpm", "d0628117fcc2148178b034044c55359b26966c6eaa8e2ce15777be3bbc91b12a"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"open_api_spex": {:hex, :open_api_spex, "3.16.0", "9843af4e87550cd8ac5821b10e4c74f1d51f0d4e3310f824d780614743423b25", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 3.0 or ~> 4.0 or ~> 5.0", [hex: :poison, repo: "hexpm", optional: true]}, {:ymlr, "~> 2.0 or ~> 3.0", [hex: :ymlr, repo: "hexpm", optional: true]}], "hexpm", "bb0be24a648b73e8fc8cbda17f514b8486262275e8b33e8b5ae66283df972129"},
Expand Down