Remove obsolete Obsidian import logic
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
defmodule Mix.Tasks.Obsidian.Import do
|
||||
@shortdoc "Import records from an Obsidian Vault containing Media entries "
|
||||
@moduledoc """
|
||||
Import records from an Obsidian Vault containing Media entries.
|
||||
|
||||
Requires the path of the vault as argument to the task:
|
||||
|
||||
`mix obsidian.import path/to/vault`
|
||||
"""
|
||||
|
||||
use Mix.Task
|
||||
|
||||
@impl Mix.Task
|
||||
def run(args) do
|
||||
case args do
|
||||
[] ->
|
||||
Mix.Shell.IO.error("Error: requires the path to the obsidian vault record folder")
|
||||
System.halt(1)
|
||||
|
||||
[path] ->
|
||||
Mix.Shell.IO.info("Seeding the database from #{path}")
|
||||
|
||||
%{valid: valid, errors: errors} =
|
||||
Path.wildcard("#{path}/*.md")
|
||||
|> Enum.reduce(%{valid: [], errors: []}, fn entry, acc ->
|
||||
file_stat = File.stat!(entry)
|
||||
|
||||
case entry
|
||||
|> File.read!()
|
||||
|> Obsidian.Parser.from_file_contents() do
|
||||
{:ok, parsed_entry} ->
|
||||
inserted_at =
|
||||
file_stat.ctime
|
||||
|> NaiveDateTime.from_erl!()
|
||||
|> DateTime.from_naive!("Etc/UTC")
|
||||
|
||||
updated_at =
|
||||
file_stat.mtime
|
||||
|> NaiveDateTime.from_erl!()
|
||||
|> DateTime.from_naive!("Etc/UTC")
|
||||
|
||||
data =
|
||||
parsed_entry
|
||||
|> Map.put(:inserted_at, inserted_at)
|
||||
|> Map.put(:updated_at, updated_at)
|
||||
|
||||
%{acc | valid: [data | acc.valid]}
|
||||
|
||||
{:error, error} ->
|
||||
%{acc | errors: [{entry, error} | acc.errors]}
|
||||
end
|
||||
end)
|
||||
|
||||
Mix.Shell.IO.info("Parsed #{length(valid)} entries")
|
||||
Mix.Shell.IO.error("Failed to parse #{length(errors)} entries")
|
||||
|
||||
MusicLibrary.Repo.insert_all(MusicLibrary.Records.Record, valid)
|
||||
|
||||
_other ->
|
||||
Mix.Shell.IO.error("Error: too many arguments")
|
||||
System.halt(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
defmodule MusicBrainz.API do
|
||||
@moduledoc """
|
||||
The original data from Obsidian maps records to MusicBrainz release groups, so we can leverage the MusicBrainz API to:
|
||||
We can leverage the MusicBrainz API to:
|
||||
|
||||
- Import new records
|
||||
- Extend the metadata associated with existing records
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
defmodule Obsidian.Entry do
|
||||
@enforce_keys [:type, :musicbrainz_id, :title, :release, :cover_url, :genres]
|
||||
defstruct [:type, :musicbrainz_id, :title, :release, :cover_url, :genres]
|
||||
end
|
||||
@@ -1,42 +0,0 @@
|
||||
defmodule Obsidian.Parser do
|
||||
alias Obsidian.Entry
|
||||
|
||||
def from_file_contents(file_contents) do
|
||||
with {:ok, meta} <- parse_frontmatter(file_contents) do
|
||||
{:ok,
|
||||
%Entry{
|
||||
type: parse_subtype(meta["subType"]),
|
||||
musicbrainz_id: meta["id"],
|
||||
title: meta["title"],
|
||||
release: meta["year"] |> parse_release(),
|
||||
cover_url: meta["image"],
|
||||
genres: meta["genres"]
|
||||
}}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_frontmatter("---\n" <> rest) do
|
||||
with [frontmatter, _] <- String.split(rest, "\n---\n"),
|
||||
{:ok, [meta]} <- YamlElixir.read_all_from_string(frontmatter) do
|
||||
{:ok, meta}
|
||||
else
|
||||
_ ->
|
||||
{:error, "Invalid frontmatter"}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_frontmatter(_file_contents) do
|
||||
{:error, "Invalid frontmatter"}
|
||||
end
|
||||
|
||||
defp parse_subtype("Album"), do: :album
|
||||
defp parse_subtype("ep"), do: :ep
|
||||
defp parse_subtype("Live"), do: :live
|
||||
defp parse_subtype("Compilation"), do: :compilation
|
||||
defp parse_subtype("Single"), do: :single
|
||||
defp parse_subtype(_), do: :other
|
||||
|
||||
defp parse_release(nil), do: nil
|
||||
defp parse_release(year) when is_integer(year), do: Integer.to_string(year)
|
||||
defp parse_release(year) when is_binary(year), do: year
|
||||
end
|
||||
Reference in New Issue
Block a user