tamland / python-tidal

Python API for TIDAL music streaming service
GNU Lesser General Public License v3.0
406 stars 110 forks source link

Favorties transfer tidal2tidal account with chrono order #179

Closed Maverick565 closed 1 year ago

Maverick565 commented 1 year ago

Hi! How to modify this code to move songs between accounts in chronological order by date added?

import tidalapi, os from tqdm import tqdm

session = tidalapi.Session() print('Enter your credentials for the account you want to transfer from') session.login_oauth_simple() uid = session.user.id TidalUser = tidalapi.Favorites(session, uid) tracks = TidalUser.tracks()

session2 = tidalapi.Session() print('Enter your credentials for the account you want to transfer to') session2.login_oauth_simple() uid2 = session2.user.id TidalUser2 = tidalapi.Favorites(session2, uid2)

print('Copying ...') for i in tqdm(range(len(tracks))): try: TidalUser2.add_track(tracks[i].id) except: print('Skipped', tracks[i].full_name, 'by' , tracks[i].artist.name)

os.system("pause")

tehkillerbee commented 1 year ago

I would say this is mostly a python related question and not so much related to a tidalapi issue. Usually we reserve issues for bugs or feature suggestions related to the tidalapi.

Anyways, as a suggestion to how you can approach this issue, Albums, Artists and Media objects has a "user_date_added" field that you can you can use to sort your tracks. https://tidalapi.netlify.app/api.html#tidalapi.media.Media.user_date_added

So I would so something like

  1. Get all tracks from account A and store them in a list
  2. Sort tracks according to "user_date_added" or whatever is required.
  3. Add tracks to account B in the correct order.
Maverick565 commented 1 year ago

This code gives me loop output file.

import tidalapi
from tqdm import tqdm

session = tidalapi.Session()
print('Enter your credentials for the account you want to transfer from')
session.login_oauth_simple()
uid = session.user.id
TidalUser = tidalapi.Favorites(session, uid)
artists = TidalUser.artists()

session2 = tidalapi.Session()
print('Enter your credentials for the account you want to transfer to')
session2.login_oauth_simple()
uid2 = session2.user.id
TidalUser2 = tidalapi.Favorites(session2, uid2)
tracks = TidalUser2.tracks()

# Create a dictionary to store track order information
track_order = {}

print('Saving ...')
for i in tqdm(range(len(tracks))):
    try:
        # Populate the track_order dictionary with track information
        track_order[tracks[i].id] = tracks[i].user_date_added

    except Exception as e:
        print(f"Error: {e}")

# Write track order information to the file after the loop
with open('track_order.txt', 'w') as f:
    for track_id, order in track_order.items():
        f.write(f"Track ID: {tracks[i].full_name}, Order: {order}\n")

print('Track order saved to track_order.txt')
Maverick565 commented 1 year ago

This is my output file x1000 same song.

Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-08-15 18:50:17.741000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:04.934000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:05.126000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:05.581000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:05.763000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:06.227000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:06.403000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:06.765000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:06.583000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:06.951000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:07.408000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:07.580000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:07.753000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:07.934000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:08.120000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:08.297000+00:00 Track ID: Ooh Ahh (My Life Be Like) [feat. Tobymac], Order: 2023-07-23 11:29:08.477000+00:00

tehkillerbee commented 1 year ago

Just had a quick glance on the script. To me it looks like you have mixed up your TidalUsers.

Shouldn't it be tracks = TidalUser.tracks() and not tracks = TidalUser2.tracks(), if you are planning to transfer from TidalUser to TidalUser2?

Maverick565 commented 1 year ago

This is script to save all favourites tracks to txt file with added date order.

import tidalapi
from tqdm import tqdm

session = tidalapi.Session()
print('Enter your credentials for the account you want to transfer from')
session.login_oauth_simple()
uid = session.user.id
TidalUser = tidalapi.Favorites(session, uid)
tracks = TidalUser.tracks()

# Create a dictionary to store track order information
track_order = {}

print('Saving ...')
for i in tqdm(range(len(tracks))):
    try:
        # Populate the track_order dictionary with track information
        track_order[tracks[i].id] = tracks[i].user_date_added

    except Exception as e:
        print(f"Error: {e}")

# Write track order information to the file after the loop
with open('track_order.txt', 'w') as f:
    for track_id, order in track_order.items():
        f.write(f"Track ID: {tracks[i].full_name}, Order: {order}\n")

print('Track order saved to track_order.txt')
Maverick565 commented 1 year ago

But it's saves only one song x1000 in file

Maverick565 commented 1 year ago

I was manage with help of chatgpt how to do that and it working.

But how to convert id from errors to song name? tracks[i].full_name not working in that case.

import tidalapi
import os
from tqdm import tqdm
import requests

filepath = 'track_order.txt'

session = tidalapi.Session()
print('Enter your credentials for the account you want to transfer to')
session.login_oauth_simple()
uid = session.user.id
TidalUser = tidalapi.Favorites(session, uid)

print('Adding ...')
with open(filepath) as f:
    lines = f.readlines()
    for i in tqdm(range(len(lines))):
        track_id = lines[i].strip()  # Usuń zbędne białe znaki na początku i końcu
        try:
            TidalUser.add_track(track_id)
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 404:
                print(f"Track {track_id} not found.")
            else:
                print(f"An error occurred while adding track {track_id}: {e}")
        except Exception as e:
            print(f"An error occurred while adding track {track_id}: {e}")

os.system("pause")

Przechwytywanie

tehkillerbee commented 1 year ago

But how to convert id from errors to song name? tracks[i].full_name not working in that case.

I don't think this is possible. Usually error tracks/albums/artists are caused by tidal removing the rights to play(view?) the track. If you open the track through tidal Web, you'll also get an error message, as far as I know

Maverick565 commented 1 year ago

Collaborator

But on tidal web there is name and artist even if can't play it. It's just greyed out.

Here is full code which transfers favourite songs tidal2tidal account with correct add order, maybe someone want it. Need install tqdm for progress bar.

Firstly it saves a 2 files first with only id of songs to transfer, and second txt have also song names which can help to check which song was not transferred. Then login into second account and it reads from track_id_list.txt and saves to second account with correct date added order.

Tracks transfer:

import tidalapi
from tqdm import tqdm
import requests
import os

# Zaloguj się na swoje pierwsze konto TIDAL
session1 = tidalapi.Session()
print('Enter your credentials for the account you want to transfer from')
session1.login_oauth_simple()
uid1 = session1.user.id
TidalUser1 = tidalapi.Favorites(session1, uid1)
tracks = TidalUser1.tracks()

# Sortowanie utworów według daty dodania (od najnowszego do najstarszego)
sorted_tracks = sorted(tracks, key=lambda x: x.user_date_added, reverse=False)

# Tworzenie lub otwieranie pliku do zapisu dla ID utworów z kodowaniem UTF-8
with open('track_id_list.txt', 'w', encoding='utf-8') as id_file:
    for track in sorted_tracks:
        track_id = track.id
        id_file.write(f'{track_id}\n')

# Tworzenie lub otwieranie pliku do zapisu z pełnymi informacjami z kodowaniem UTF-8
with open('track_list.txt', 'w', encoding='utf-8') as full_info_file:
    for track in sorted_tracks:
        track_id = track.id
        track_name = track.name
        added_date = track.user_date_added
        full_info_file.write(f'Track ID: {track_id}, Track Name: {track_name}, Added Date: {added_date}\n')

print('Tracks saved to track_id_list.txt and track_list.txt')

filepath = 'track_id_list.txt'

# Zaloguj się na swoje drugie konto TIDAL
session2 = tidalapi.Session()
print('Enter your credentials for the account you want to transfer to')
session2.login_oauth_simple()
uid2 = session2.user.id
TidalUser2 = tidalapi.Favorites(session2, uid2)

print('Adding ...')
with open(filepath) as f:
    lines = f.readlines()
    for i in tqdm(range(len(lines))):
        track_id = lines[i].strip()  # Usuń zbędne białe znaki na początku i końcu
        try:
            TidalUser2.add_track(track_id)
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 404:
                print(f"Track {track_id} not found.")
            else:
                print(f"An error occurred while adding track {track_id}: {e}")
        except Exception as e:
            print(f"An error occurred while adding track {track_id}: {e}")

os.system("pause")
Maverick565 commented 1 year ago

And here is working code for albums transfer:

import tidalapi
from tqdm import tqdm
import requests
import os

# Zaloguj się na swoje pierwsze konto TIDAL
session1 = tidalapi.Session()
print('Enter your credentials for the account you want to transfer from')
session1.login_oauth_simple()
uid1 = session1.user.id
TidalUser1 = tidalapi.Favorites(session1, uid1)
albums = TidalUser1.albums()

# Sortowanie albumów według daty dodania (od najnowszego do najstarszego)
sorted_albums = sorted(albums, key=lambda x: x.user_date_added, reverse=False)

# Tworzenie lub otwieranie pliku do zapisu dla ID albumów z kodowaniem UTF-8
with open('album_id_list.txt', 'w', encoding='utf-8') as id_file:
    for album in sorted_albums:
        album_id = album.id
        id_file.write(f'{album_id}\n')

# Tworzenie lub otwieranie pliku do zapisu z pełnymi informacjami z kodowaniem UTF-8
with open('album_list.txt', 'w', encoding='utf-8') as full_info_file:
    for album in sorted_albums:
        album_id = album.id
        album_name = album.name
        added_date = album.user_date_added
        full_info_file.write(f'Album ID: {album_id}, Album Name: {album_name}, Added Date: {added_date}\n')

print('Albums saved to album_id_list.txt and album_list.txt')

filepath = 'album_id_list.txt'

# Zaloguj się na swoje drugie konto TIDAL
session2 = tidalapi.Session()
print('Enter your credentials for the account you want to transfer to')
session2.login_oauth_simple()
uid2 = session2.user.id
TidalUser2 = tidalapi.Favorites(session2, uid2)

print('Adding ...')
with open(filepath) as f:
    lines = f.readlines()
    for i in tqdm(range(len(lines))):
        album_id = lines[i].strip()  # Usuń zbędne białe znaki na początku i końcu
        try:
            TidalUser2.add_album(album_id)
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 404:
                print(f"Album {album_id} not found.")
            else:
                print(f"An error occurred while adding album {album_id}: {e}")
        except Exception as e:
            print(f"An error occurred while adding album {album_id}: {e}")

os.system("pause")
2e0byo commented 1 year ago

Closing as this isn't really a tidalapi issue and the OP seems to have got things working anyway.