Support getting artist bios from Wikipedia (experimental)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
defmodule MusicBrainz.ArtistTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias MusicBrainz.Artist
|
||||
|
||||
describe "get_wikidata_id/1" do
|
||||
test "extracts wikidata ID from relations" do
|
||||
artist_data = MusicBrainz.Fixtures.Artist.get_artist()
|
||||
artist = Artist.from_api_response(artist_data)
|
||||
|
||||
assert Artist.get_wikidata_id(artist) == "Q352766"
|
||||
end
|
||||
|
||||
test "returns nil when no wikidata relation exists" do
|
||||
artist = %Artist{
|
||||
id: "test",
|
||||
name: "Test",
|
||||
sort_name: "Test",
|
||||
relations: [
|
||||
%{type: "discogs", url: %{"resource" => "https://www.discogs.com/artist/123"}}
|
||||
]
|
||||
}
|
||||
|
||||
assert Artist.get_wikidata_id(artist) == nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"batchcomplete": "",
|
||||
"query": {
|
||||
"pages": {
|
||||
"1845781": {
|
||||
"pageid": 1845781,
|
||||
"ns": 0,
|
||||
"title": "Steven Wilson",
|
||||
"extract": "<p><b>Steven John Wilson</b> (born 3 November 1967) is an English musician. He is most associated with the progressive rock genre, though his influences and work extend beyond it. Wilson first became known as the founder, lead guitarist, singer, and songwriter of the band Porcupine Tree, as well as being a member of several other bands.</p>\n<p>Wilson has been involved in musical projects including No-Man, Bass Communion, Blackfield, and Storm Corrosion. He has also worked as a record producer and is known for his surround sound and high-fidelity stereo remixes of classic albums by artists such as King Crimson, Jethro Tull, Tears for Fears, Roxy Music, and Yes.</p>\n<p>His solo career began in earnest in 2008. He has released six solo studio albums, including <i>The Raven That Refused to Sing (And Other Stories)</i> (2013) and <i>Hand. Cannot. Erase.</i> (2015), both of which received critical acclaim. His seventh album, <i>The Overview</i>, was released in 2025.</p>"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"type": "standard",
|
||||
"title": "Steven Wilson",
|
||||
"displaytitle": "Steven Wilson",
|
||||
"namespace": {
|
||||
"id": 0,
|
||||
"text": ""
|
||||
},
|
||||
"wikibase_item": "Q352766",
|
||||
"titles": {
|
||||
"canonical": "Steven_Wilson",
|
||||
"normalized": "Steven Wilson",
|
||||
"display": "Steven Wilson"
|
||||
},
|
||||
"pageid": 1845781,
|
||||
"extract": "Steven John Wilson is an English musician. He is most associated with the progressive rock genre, though his influences and work extend beyond it.",
|
||||
"extract_html": "<p><b>Steven John Wilson</b> is an English musician. He is most associated with the progressive rock genre, though his influences and work extend beyond it.</p>",
|
||||
"description": "English musician and record producer",
|
||||
"content_urls": {
|
||||
"desktop": {
|
||||
"page": "https://en.wikipedia.org/wiki/Steven_Wilson",
|
||||
"revisions": "https://en.wikipedia.org/wiki/Steven_Wilson?action=history",
|
||||
"edit": "https://en.wikipedia.org/wiki/Steven_Wilson?action=edit",
|
||||
"talk": "https://en.wikipedia.org/wiki/Talk:Steven_Wilson"
|
||||
},
|
||||
"mobile": {
|
||||
"page": "https://en.m.wikipedia.org/wiki/Steven_Wilson",
|
||||
"revisions": "https://en.m.wikipedia.org/w/index.php?title=Steven_Wilson&action=history",
|
||||
"edit": "https://en.m.wikipedia.org/w/index.php?title=Steven_Wilson&action=edit",
|
||||
"talk": "https://en.m.wikipedia.org/wiki/Talk:Steven_Wilson"
|
||||
}
|
||||
},
|
||||
"lang": "en",
|
||||
"dir": "ltr"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
defmodule Wikipedia.Fixtures do
|
||||
@fixtures_folder Path.join([File.cwd!(), "test/support/fixtures/wikipedia"])
|
||||
|
||||
def wikidata_response do
|
||||
Path.join([@fixtures_folder, "wikidata_response.json"])
|
||||
|> File.read!()
|
||||
|> JSON.decode!()
|
||||
end
|
||||
|
||||
def wikidata_response_no_enwiki do
|
||||
Path.join([@fixtures_folder, "wikidata_response_no_enwiki.json"])
|
||||
|> File.read!()
|
||||
|> JSON.decode!()
|
||||
end
|
||||
|
||||
def article_summary do
|
||||
Path.join([@fixtures_folder, "article_summary.json"])
|
||||
|> File.read!()
|
||||
|> JSON.decode!()
|
||||
end
|
||||
|
||||
def article_extract do
|
||||
Path.join([@fixtures_folder, "article_extract.json"])
|
||||
|> File.read!()
|
||||
|> JSON.decode!()
|
||||
end
|
||||
|
||||
def article_extract_html do
|
||||
article_extract()
|
||||
|> get_in(["query", "pages"])
|
||||
|> Map.values()
|
||||
|> List.first()
|
||||
|> Map.get("extract")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"entities": {
|
||||
"Q352766": {
|
||||
"type": "item",
|
||||
"id": "Q352766",
|
||||
"sitelinks": {
|
||||
"enwiki": {
|
||||
"site": "enwiki",
|
||||
"title": "Steven Wilson",
|
||||
"badges": []
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"success": 1
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"entities": {
|
||||
"Q999999": {
|
||||
"type": "item",
|
||||
"id": "Q999999",
|
||||
"sitelinks": {}
|
||||
}
|
||||
},
|
||||
"success": 1
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
defmodule WikipediaTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
describe "get_artist_summary/1" do
|
||||
test "resolves wikidata ID to Wikipedia summary with full intro" do
|
||||
wikidata_id = "Q352766"
|
||||
summary = Wikipedia.Fixtures.article_summary()
|
||||
intro_html = Wikipedia.Fixtures.article_extract_html()
|
||||
|
||||
Req.Test.stub(Wikipedia.API, fn conn ->
|
||||
case conn.request_path do
|
||||
"/w/api.php" ->
|
||||
case conn.params["action"] do
|
||||
"wbgetentities" ->
|
||||
Req.Test.json(conn, Wikipedia.Fixtures.wikidata_response())
|
||||
|
||||
"query" ->
|
||||
Req.Test.json(conn, Wikipedia.Fixtures.article_extract())
|
||||
end
|
||||
|
||||
"/api/rest_v1/page/summary/Steven%20Wilson" ->
|
||||
Req.Test.json(conn, summary)
|
||||
end
|
||||
end)
|
||||
|
||||
assert {:ok, result} = Wikipedia.get_artist_summary(wikidata_id)
|
||||
assert result["extract"] == summary["extract"]
|
||||
assert result["description"] == summary["description"]
|
||||
assert result["intro_html"] == intro_html
|
||||
end
|
||||
|
||||
test "returns error when no English Wikipedia article exists" do
|
||||
wikidata_id = "Q999999"
|
||||
|
||||
Req.Test.stub(Wikipedia.API, fn conn ->
|
||||
Req.Test.json(conn, Wikipedia.Fixtures.wikidata_response_no_enwiki())
|
||||
end)
|
||||
|
||||
assert {:error, :no_english_wikipedia} = Wikipedia.get_artist_summary(wikidata_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user