-
Notifications
You must be signed in to change notification settings - Fork 1
Add track duration metadata system with timer-only timing for SiriusXM streams #35
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b87d4c8
Initial plan
Copilot 4326ced
Implement core track duration functionality with database and MusicBr…
Copilot 97e1c13
Add documentation for new track duration feature
Copilot f2763bd
Fix all reported review issues: ranking by release date, cache direct…
Copilot a54575a
Implement dynamic track timing system following RadioParadise pattern
Copilot 699446c
Remove track timing display code and implement simplified timer formula
Copilot 09d1d7c
Remove block data functions and track timing internally for metadata …
Copilot 3d14f91
Fix metadata timer calculation by updating song metadata when duratio…
Copilot 8f7cde8
Fix duplicate MusicBrainz API calls and timer calculation issues
Copilot c98ee0e
Merge branch 'main' into copilot/fix-34
paul-1 e7f93f9
Remove track duration display and reduce timer buffer to 15 seconds
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package Plugins::SiriusXM::MusicBrainzAPI; | ||
|
|
||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Slim::Utils::Log; | ||
| use Slim::Networking::SimpleAsyncHTTP; | ||
| use JSON::XS; | ||
| use URI::Escape; | ||
|
|
||
| my $log = logger('plugin.siriusxm.musicbrainz'); | ||
|
|
||
| # MusicBrainz API endpoint | ||
| use constant MUSICBRAINZ_URL => 'https://musicbrainz.org/ws/2/recording'; | ||
|
|
||
| # Minimum score threshold (75%) | ||
| use constant MIN_SCORE_THRESHOLD => 75; | ||
|
|
||
| # Search for track duration using MusicBrainz API | ||
| sub searchTrackDuration { | ||
| my ($class, $title, $artist, $callback) = @_; | ||
|
|
||
| return unless $title && $artist && $callback; | ||
|
|
||
| # Clean up search terms | ||
| $title = _cleanSearchTerm($title); | ||
| $artist = _cleanSearchTerm($artist); | ||
|
|
||
| # Build search query | ||
| my $query = uri_escape("recording:\"$title\" AND artist:\"$artist\""); | ||
| my $url = MUSICBRAINZ_URL . "?query=$query&limit=10&fmt=json"; | ||
|
|
||
| $log->debug("Searching MusicBrainz for: $title by $artist"); | ||
|
|
||
| my $http = Slim::Networking::SimpleAsyncHTTP->new( | ||
| sub { | ||
| my $response = shift; | ||
| $class->_processResponse($title, $artist, $response, $callback); | ||
| }, | ||
| sub { | ||
| my ($http, $error) = @_; | ||
| $log->warn("MusicBrainz API error: $error"); | ||
| $callback->(); | ||
| }, | ||
| { | ||
| timeout => 20, | ||
| # User-Agent required by MusicBrainz | ||
| 'User-Agent' => 'LMS-SiriusXM-Plugin/1.0 (https://github.com/paul-1/plugin-SiriusXM)', | ||
| } | ||
| ); | ||
|
|
||
| $http->get($url); | ||
| } | ||
|
|
||
| # Process MusicBrainz API response | ||
| sub _processResponse { | ||
| my ($class, $title, $artist, $response, $callback) = @_; | ||
|
|
||
| my $content = $response->content; | ||
| my $data; | ||
|
|
||
| eval { | ||
| $data = decode_json($content); | ||
| }; | ||
|
|
||
| if ($@) { | ||
| $log->warn("Failed to parse MusicBrainz response: $@"); | ||
| $callback->(); | ||
| return; | ||
| } | ||
|
|
||
| my $recordings = $data->{recordings}; | ||
| unless ($recordings && @$recordings) { | ||
| $log->debug("No recordings found for: $title by $artist"); | ||
| $callback->(); | ||
| return; | ||
| } | ||
|
|
||
| # Find the best match based on score and release date | ||
| my $best_match; | ||
| my $best_score = 0; | ||
| my $best_release_date; | ||
|
|
||
| foreach my $recording (@$recordings) { | ||
| my $score = $recording->{score} || 0; | ||
|
|
||
| # Skip if below threshold | ||
| next if $score < MIN_SCORE_THRESHOLD; | ||
|
|
||
| # Check if this recording has a duration | ||
| my $length = $recording->{length}; | ||
| next unless defined $length; | ||
|
|
||
| # Convert milliseconds to seconds | ||
| my $duration_seconds = int($length / 1000); | ||
| next if $duration_seconds <= 0; | ||
|
|
||
| # Get earliest release date | ||
| my $release_date = _getEarliestReleaseDate($recording); | ||
|
|
||
| # Update best match if score is higher, or if score is equal and this has an older release date | ||
| my $is_better = ($score > $best_score); | ||
| if ($score == $best_score && defined $release_date && defined $best_release_date) { | ||
| $is_better = ($release_date lt $best_release_date); | ||
| } elsif ($score == $best_score && defined $release_date && !defined $best_release_date) { | ||
| $is_better = 1; | ||
| } | ||
|
|
||
| if ($is_better) { | ||
| $best_score = $score; | ||
| $best_release_date = $release_date; | ||
| $best_match = { | ||
| duration => $duration_seconds, | ||
| score => $score, | ||
| title => $recording->{title}, | ||
| length_ms => $length, | ||
| release_date => $release_date, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| if ($best_match) { | ||
| $log->info("Found MusicBrainz match for '$title' by '$artist': " . | ||
| $best_match->{duration} . "s (score: $best_score%)"); | ||
|
|
||
| $callback->($best_match->{duration}, $best_score); | ||
| } else { | ||
| $log->debug("No suitable MusicBrainz matches found for: $title by $artist (threshold: " . MIN_SCORE_THRESHOLD . "%)"); | ||
| $callback->(); | ||
| } | ||
| } | ||
|
|
||
| # Clean search terms for better matching | ||
| sub _cleanSearchTerm { | ||
| my $term = shift || ''; | ||
|
|
||
| # Remove common noise | ||
| $term =~ s/\s*\(.*?\)\s*//g; # Remove parentheses content | ||
| $term =~ s/\s*\[.*?\]\s*//g; # Remove brackets content | ||
| $term =~ s/\s*feat\.?\s+.*$//i; # Remove featuring | ||
| $term =~ s/\s*ft\.?\s+.*$//i; # Remove ft. | ||
| $term =~ s/\s*with\s+.*$//i; # Remove with | ||
| $term =~ s/^\s+|\s+$//g; # Trim whitespace | ||
| $term =~ s/\s+/ /g; # Normalize whitespace | ||
|
|
||
| return $term; | ||
| } | ||
|
|
||
| # Get the earliest release date from a recording's releases | ||
| sub _getEarliestReleaseDate { | ||
| my $recording = shift; | ||
|
|
||
| my $releases = $recording->{releases}; | ||
| return unless $releases && @$releases; | ||
|
|
||
| my $earliest_date; | ||
|
|
||
| foreach my $release (@$releases) { | ||
| my $date = $release->{'first-release-date'} || $release->{date}; | ||
| next unless $date; | ||
|
|
||
| # Normalize date format (handle partial dates like "1975" or "1975-06") | ||
| if ($date =~ /^(\d{4})(?:-(\d{2}))?(?:-(\d{2}))?/) { | ||
| my ($year, $month, $day) = ($1, $2 || '01', $3 || '01'); | ||
| $date = sprintf("%04d-%02d-%02d", $year, $month, $day); | ||
| } | ||
|
|
||
| if (!defined $earliest_date || $date lt $earliest_date) { | ||
| $earliest_date = $date; | ||
| } | ||
| } | ||
|
|
||
| return $earliest_date; | ||
| } | ||
|
|
||
| 1; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.