Move obsidian-related imports under Obsidian

This commit is contained in:
Claudio Ortolina
2024-09-24 09:21:13 +01:00
parent d97f21a8ef
commit b6ae022a6a
5 changed files with 52 additions and 43 deletions
+3
View File
@@ -0,0 +1,3 @@
defmodule Obsidian.Entry do
defstruct [:type, :musicbrainz_id, :title, :year, :image_url, :genres]
end
@@ -1,20 +1,22 @@
defmodule MusicLibrary.Records.Parser do defmodule Obsidian.Parser do
def from_entry_contents(entry_contents) do alias Obsidian.Entry
with {:ok, [meta]} <- parse_frontmatter(entry_contents) do
def from_file_contents(file_contents) do
with {:ok, [meta]} <- parse_frontmatter(file_contents) do
{:ok, {:ok,
%{ %Entry{
type: parse_subtype(meta["subType"]), type: parse_subtype(meta["subType"]),
musicbrainz_id: meta["id"], musicbrainz_id: meta["id"],
title: meta["title"], title: meta["title"],
year: meta["year"] |> maybe_parse_year(), year: meta["year"] |> maybe_parse_year(),
image: meta["image"], image_url: meta["image"],
genres: meta["genres"] genres: meta["genres"]
}} }}
end end
end end
defp parse_frontmatter(entry_contents) do defp parse_frontmatter(file_contents) do
case entry_contents do case file_contents do
"---\n" <> rest -> "---\n" <> rest ->
case String.split(rest, "\n---\n") do case String.split(rest, "\n---\n") do
[frontmatter, _] -> [frontmatter, _] ->
+7 -3
View File
@@ -26,14 +26,18 @@ case System.argv() do
case entry case entry
|> File.read!() |> File.read!()
|> MusicLibrary.Records.Parser.from_entry_contents() do |> Obsidian.Parser.from_file_contents() do
{:ok, parsed_entry} -> {:ok, parsed_entry} ->
inserted_at = file_stat.ctime inserted_at =
file_stat.ctime
|> NaiveDateTime.from_erl!() |> NaiveDateTime.from_erl!()
|> DateTime.from_naive!("Etc/UTC") |> DateTime.from_naive!("Etc/UTC")
updated_at = file_stat.mtime
updated_at =
file_stat.mtime
|> NaiveDateTime.from_erl!() |> NaiveDateTime.from_erl!()
|> DateTime.from_naive!("Etc/UTC") |> DateTime.from_naive!("Etc/UTC")
data = data =
parsed_entry parsed_entry
|> Map.put(:inserted_at, inserted_at) |> Map.put(:inserted_at, inserted_at)
@@ -1,20 +1,21 @@
defmodule MusicLibrary.Records.ParserTest do defmodule Obsidian.ParserTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
alias MusicLibrary.Records.Parser alias Obsidian.{Entry, Parser}
@obsidian_entry_path Path.expand("../../support/fixtures/marillion-marbles.md", __DIR__) @marbles_entry_path Path.expand("../support/fixtures/marillion-marbles.md", __DIR__)
@guardians_entry_path Path.expand("../support/fixtures/guardians.md", __DIR__)
test "parses the content of the Obsidian album entry" do test "parses the content of the Obsidian album entry" do
entry_contents = File.read!(@obsidian_entry_path) entry_contents = File.read!(@marbles_entry_path)
assert Parser.from_entry_contents(entry_contents) == assert Parser.from_file_contents(entry_contents) ==
{:ok, {:ok,
%{ %Entry{
type: :album, type: :album,
musicbrainz_id: "20790e26-98e4-3ad3-a67f-b674758b942d", musicbrainz_id: "20790e26-98e4-3ad3-a67f-b674758b942d",
title: "Marbles", title: "Marbles",
year: 2004, year: 2004,
image: image_url:
"https://coverartarchive.org/release-group/20790e26-98e4-3ad3-a67f-b674758b942d/front", "https://coverartarchive.org/release-group/20790e26-98e4-3ad3-a67f-b674758b942d/front",
genres: [ genres: [
"alternative rock", "alternative rock",
@@ -29,35 +30,13 @@ defmodule MusicLibrary.Records.ParserTest do
end end
test "handles special characters in titles" do test "handles special characters in titles" do
entry_contents = """ entry_contents = File.read!(@guardians_entry_path)
---
type: "musicRelease"
subType: "Album"
title: "Guardians of the Galaxy: Awesome Mix, Vol. 1"
englishTitle: "Guardians of the Galaxy: Awesome Mix, Vol. 1"
year: "2014"
dataSource: "MusicBrainz API"
url: "https://musicbrainz.org/release-group/950092d6-45f6-4269-87da-99a9ff2fcc52"
id: "950092d6-45f6-4269-87da-99a9ff2fcc52"
genres:
- "classic rock"
- "pop"
- "pop rock"
- "rock"
artists:
- "Various Artists"
image: "https://coverartarchive.org/release-group/950092d6-45f6-4269-87da-99a9ff2fcc52/front"
rating: 9.6
personalRating: 0
tags: "mediaDB/music/Album"
---
"""
assert Parser.from_entry_contents(entry_contents) == assert Parser.from_file_contents(entry_contents) ==
{:ok, {:ok,
%{ %Entry{
genres: ["classic rock", "pop", "pop rock", "rock"], genres: ["classic rock", "pop", "pop rock", "rock"],
image: image_url:
"https://coverartarchive.org/release-group/950092d6-45f6-4269-87da-99a9ff2fcc52/front", "https://coverartarchive.org/release-group/950092d6-45f6-4269-87da-99a9ff2fcc52/front",
musicbrainz_id: "950092d6-45f6-4269-87da-99a9ff2fcc52", musicbrainz_id: "950092d6-45f6-4269-87da-99a9ff2fcc52",
title: "Guardians of the Galaxy: Awesome Mix, Vol. 1", title: "Guardians of the Galaxy: Awesome Mix, Vol. 1",
+21
View File
@@ -0,0 +1,21 @@
---
type: "musicRelease"
subType: "Album"
title: "Guardians of the Galaxy: Awesome Mix, Vol. 1"
englishTitle: "Guardians of the Galaxy: Awesome Mix, Vol. 1"
year: "2014"
dataSource: "MusicBrainz API"
url: "https://musicbrainz.org/release-group/950092d6-45f6-4269-87da-99a9ff2fcc52"
id: "950092d6-45f6-4269-87da-99a9ff2fcc52"
genres:
- "classic rock"
- "pop"
- "pop rock"
- "rock"
artists:
- "Various Artists"
image: "https://coverartarchive.org/release-group/950092d6-45f6-4269-87da-99a9ff2fcc52/front"
rating: 9.6
personalRating: 0
tags: "mediaDB/music/Album"
---