jackfagner / OpenTidl

Free and open source API for TIDAL
GNU Affero General Public License v3.0
44 stars 18 forks source link

Playlist - Add track from local track ID...online track ID in playlist changes!? [Region Issue] #5

Open scgough opened 7 years ago

scgough commented 7 years ago

Hi there, I'm seeing a really weird intermittent bug...I'm not sure why it's happening. This isn't necessarily anything in your code...I just need to figure out why it's happening! 🤕

I carry out a track search (e.g. Range Life Pavement) and get some results:

track ID : 58085907 title: Range Life album: Quarantine The Past: Greatest Hits 1989-1999

I then push this track ID to my playlist but the track ID I end up with online is:

track ID: 14831266 title: Range Life album: Crooked Rain Crooked Rain

it happens seemingly randomly on lots of different songs. Sometimes the chosen trackID is added, sometimes it's switched...

jackfagner commented 7 years ago

Can you provide some example code for me to repeat this issue, or is it fully random?

scgough commented 7 years ago

OK, I have:

Update This code seems to do it every time. Adding ID 58085907 to a playlist actually adds ID 14831266...

            string playlistID = "[your-playlist-id]";
            string trackID = "58085907";   //'Range Life' by Pavement
            string tidalToken = "[your-tidal-session-token]";

            OpenTidlClient client = new OpenTidlClient(ClientConfiguration.Default);
            var mySession = await client.RestoreSession(tidalToken);    //session id
            if (mySession.SessionId != "" && mySession.UserId > 0)
            {
                string etag = String.Empty;
                try
                {
                    var tidalPl = await mySession.GetPlaylist(playlistID);
                    if (tidalPl.ETag != null && tidalPl.ETag != String.Empty)
                    {
                        etag = tidalPl.ETag;
                    }

                    var trackIds = new List<int>();
                    trackIds.Add(Convert.ToInt32(trackID));

                    var result = await mySession.AddPlaylistTracks(playlistID, trackIds, toIndex: 0, ifNoneMatch: etag.Replace("&quot;", ""));

                }
                catch(Exception e)
                {
                    var t = e;
                }
            }

It does seem to be random though...I can't seem to 'catch' it in a debug....

jackfagner commented 7 years ago

Just a quick guess: Maybe searchResponse.Items ordering is somewhat random, and searchResponse.Items.First() gives you different tracks from time to time?

scgough commented 7 years ago

That side of it is fine. I don't mind what order the search comes back in. The problem is that I say:

  1. 'give me the first track'.
  2. the id for this track is 58085907
  3. I say add track ID 58085907 to the playlist
  4. the playlist on tidal has track ID 14831266 in it?! (which is the same track but off a different album)

My problem is then that the ID I have 58085907 is then useless in the context of my playlist.

scgough commented 7 years ago

@jackfagner I've updated my example above. this is happening for every test I've done with it.

jackfagner commented 7 years ago

Thank you. I will have a look at it. The API does not alter or process the track ID in any way, so it might be a server side "feature".

scgough commented 7 years ago

maybe...i'm wondering if it's something in the search...

thanks for looking anyway!

jackfagner commented 7 years ago

In the search? I don't see any searching in your example code (any more).

scgough commented 7 years ago

sorry @jackfagner - ignore the search. I was just thinking out loud.

scgough commented 7 years ago

@jackfagner - I've got a little further with this...the problem does seem to stem from what comes back in the search. I'm using your SearchTracks method in /method/opentidlpublicmethods to search. I've slightly adapted it though to return the raw JSON from Tidal (as there are some more fields in it).

When I get the 58085907 ID back it only seems to be in a result set of 1. When I add this track ID it is auto-replaced on Tidal.

I'm trying to get that to come back in the raw JSON (but it's decided not to do it at the moment) as I'm wondering if there are any extra bits of data I can use to know whether to ignore the result and try to search again or skip on....

My thoughts are this is a legacy track of some kind on Tidal know that ID 58085907 should be replaced with another.

scgough commented 7 years ago

@jackfagner i think i've got it. the API is switching my territory between requests. There is a method GetCountryCode attached to each call and it is switching between US and GB. I've attached a screen grab. I noticed the ISRC codes were prefixed with the different regions.

I'm going to update the requests to allow a forced region code in my code.

On this result set, I've run the same search request twice but on one forced US and the other GB:

tidl-json

scgough commented 7 years ago

This wasn't just a matter of forcing one region (i.e. US) on every call. Tidal knows my user session is GB and always forces this on their side when I perform an action like 'add' a track.

Because of that I've worked the solution in another way. Searches and other lookups weren't honouring my session region so I've added a method to check the current session countrycode and make sure the client LastSessionCountryCode is always the same.

I've added this to OpenTidlClient.cs

public void SetCountryCodeFromSession(string countryCode)
{
    if (!string.IsNullOrEmpty(countryCode))
    {
        this.LastSessionCountryCode = countryCode;
     }
}

Then, when I successfully restore my session (to perform whatever action - search, add, etc) I call it so:

...
OpenTidlClient client = new OpenTidlClient(ClientConfiguration.Default);
var tidalSession = await client.RestoreSession(myToken); 
if (tidalSession.SessionId != "" && tidalSession.UserId > 0)
{
    client.SetCountryCodeFromSession(tidalSession.CountryCode); 
   ...
}
jackfagner commented 7 years ago

Should be fixed in 5edce4f499b33e1ee384e02bf44c7a31bedaae36 When I have time I will update the models and publish a new NuGet package.