Skip to content
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

Detecting and replacing unavailable tracks #285

Closed
dennisklad opened this issue Sep 25, 2024 · 4 comments
Closed

Detecting and replacing unavailable tracks #285

dennisklad opened this issue Sep 25, 2024 · 4 comments

Comments

@dennisklad
Copy link

Hi, thanks so much for this API!!

I had a question: Is there a way to detect the unavailable tracks? This has been annoying me and wanted to check if there is a way to replace them with the API.

Thanks in advance.

image

@dennisklad
Copy link
Author

Sorry, just tested it and there is indeed the boolean available that is inhereted from Media.
https:/tamland/python-tidal/blob/c0a6aef57e7eac8232cf491b0316f6c8d0f5da87/tidalapi/media.py#L192C1-L192C27

@tehkillerbee
Copy link
Collaborator

tehkillerbee commented Sep 25, 2024

@dennisklad
Sure, as you have now found out! you can use the .available attribute.

Also, albums, tracks etc that are not available will throw an exception when fetched directly:

from pathlib import Path

import tidalapi
from tidalapi import exceptions

session_file1 = Path("tidal-session-oauth.json")

session = tidalapi.Session()
# Load session from file; create a new OAuth session if necessary
session.login_session_file(session_file1)

tracks = ["381616195", "5587976"]  # Track #2 is not available
# Check if tGet user tracks and check if track is available
for track_id in tracks:
    try:
        track = session.track(track_id=track_id)
    except exceptions.ObjectNotFound:
        print(f"Track {track_id} not available")

@dennisklad
Copy link
Author

dennisklad commented Sep 25, 2024

Perfect! Also, I noticed that it is not possible to directly add the track again to the favorites as it has a new ID and it throws a 404.

From this convo I see that it is also not possible with the isrc, right?
I would have to do a full text search on the track and add it this way?

EDIT: Oh, it says that this was feature was added in the v0.7.7! How would one use this?
EDIT2: I can only install up to v0.7.6 from pip :/

@dennisklad
Copy link
Author

dennisklad commented Sep 25, 2024

For anyone that is interested this is my code:

session = oauth_login.get_tidalapi_session()
favorites = tidalapi.Favorites(session, session.user.id)

limit = 500
offset = 0

tracks = favorites.tracks(limit=limit, offset=offset)

while len(tracks):
    
    print("Ckecking until", limit + offset)


    for track in tracks:

        if not track.available:
            print(f"Searching for {track.full_name} by {track.artist.name}...")
            search = session.search(f"{track.full_name} {track.artist.name}", [tidalapi.Track])
            top_hit = search['top_hit']
            
            if not top_hit:
                print("\tNo results found :(")
                continue
            
            i = 0

            # The search result should have the same IRSC
            while top_hit.isrc != track.isrc and i < len(search["tracks"]):
                top_hit = search["tracks"][i]
                i += 1

            if top_hit.isrc == track.isrc:
                print("Removed old track:", favorites.remove_track(track.id))
                print("Added new track:", favorites.add_track(top_hit.id))
            else:
                print("\tNo maching track found :(")

    offset += limit
    tracks = favorites.tracks(limit=limit, offset=offset)

@dennisklad dennisklad changed the title Detecting unavailable tracks Detecting and replacing unavailable tracks Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants