-
Notifications
You must be signed in to change notification settings - Fork 0
feat(rsi-paints-import): load and map paints data from the RSI Pledge Store #37
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs an update to the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a task to regenerate the mix.nix?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is a task, but currently seems broken, |
||
| # {: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} | ||
|
|
||
There was a problem hiding this comment.
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.Modelas function there, but also fine here.