Skip to content

Commit

Permalink
refined discogs prefill to include subtracks and finess artist name
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketMan committed Nov 25, 2024
1 parent 237572a commit 06a1ac9
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions ui/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,24 +297,51 @@ private function prefillTracks() {
!preg_match('|/spacer.gif$|', $result->cover_image) ?
$result->cover_image : null;
$infoUrl = self::DISCOGS_BASE . $result->uri;

$response = $discogs->get($result->resource_url);
$page = $response->getBody()->getContents();
$json = json_decode($page);
$seq = 0;
$tracks = [];
$addUrls = $this->getUrlAutofill();
foreach($json->tracklist as $track) {
if($track->type_ == "index") {
// The artist name is formatted like:
// Richard Wilson (22)
// where (22) is an optional suffix.
// We want only the last word, exclusive the suffix
// (in this case, 'Wilson').
$artist = isset($track->extraartists) &&
preg_match('/(\w+)(?:\s\(\d+\))?$/', $track->extraartists[0]->name, $matches) ? $matches[1] . ': ' : '';

foreach($track->sub_tracks as $track) {
$entry = [];
$entry["seq"] = ++$seq;
$entry["oseq"] = trim($track->position);
$entry["time"] = trim($track->duration);
$entry["title"] = mb_substr($artist . trim($track->title), 0, PlaylistEntry::MAX_FIELD_LENGTH);

$tracks[] = $entry;
}
continue;
}

if($track->type_ != "track")
continue;

// see comment above about the artist name format
$artist = isset($track->extraartists) &&
preg_match('/(\w+)(?:\s\(\d+\))?$/', $track->extraartists[0]->name, $matches) ? $matches[1] . ': ' : '';

$entry = [];
$entry["seq"] = ++$seq;
$entry["oseq"] = trim($track->position);
$entry["time"] = trim($track->duration);
$entry["title"] = mb_substr(trim($track->title), 0, PlaylistEntry::MAX_FIELD_LENGTH);
if($track->artists)
$entry["artist"] = mb_substr(trim($track->artists[0]->name), 0, PlaylistEntry::MAX_FIELD_LENGTH);
$entry["title"] = mb_substr($artist . trim($track->title), 0, PlaylistEntry::MAX_FIELD_LENGTH);
// strip optional numeric suffix from artist name
if($track->artists &&
preg_match('/^(.+?)(?:\s\(\d+\))?$/', $track->artists[0]->name, $matches))
$entry["artist"] = mb_substr(trim($matches[1]), 0, PlaylistEntry::MAX_FIELD_LENGTH);

if($addUrls && $json->videos) {
foreach($json->videos as $key => $video) {
if(mb_stripos($video->title, $entry['title']) !== false) {
Expand Down

0 comments on commit 06a1ac9

Please sign in to comment.