|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright (C) 2023- The Tidalapi Developers |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU Lesser General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | +"""transfer_favorites.py: Use this script to transfer your Tidal favourites from Tidal user A to Tidal user B""" |
| 19 | +import logging |
| 20 | +from pathlib import Path |
| 21 | +import csv |
| 22 | +import time |
| 23 | +import sys |
| 24 | + |
| 25 | +import tidalapi |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | +logger.setLevel(logging.INFO) |
| 29 | +logger.addHandler(logging.StreamHandler(sys.stdout)) |
| 30 | + |
| 31 | +oauth_file1 = Path("tidal-oauth-user.json") |
| 32 | +oauth_file2 = Path("tidal-oauth-userB.json") |
| 33 | + |
| 34 | + |
| 35 | +class TidalSession: |
| 36 | + def __init__(self): |
| 37 | + self._active_session = tidalapi.Session() |
| 38 | + |
| 39 | + def get_uid(self): |
| 40 | + return self._active_session.user.id |
| 41 | + |
| 42 | + def get_session(self): |
| 43 | + return self._active_session |
| 44 | + |
| 45 | + |
| 46 | +class TidalTransfer: |
| 47 | + def __init__(self): |
| 48 | + self.session_src = TidalSession() |
| 49 | + self.session_dst = TidalSession() |
| 50 | + |
| 51 | + def export_csv(self, my_tracks, my_albums, my_artists, my_playlists): |
| 52 | + logger.info("Exporting user A favorites to csv...") |
| 53 | + # save to csv file |
| 54 | + with open("fav_tracks.csv", "w") as file: |
| 55 | + wr = csv.writer(file, quoting=csv.QUOTE_ALL) |
| 56 | + for track in my_tracks: |
| 57 | + wr.writerow( |
| 58 | + [ |
| 59 | + track.id, |
| 60 | + track.user_date_added, |
| 61 | + track.artist.name, |
| 62 | + track.album.name, |
| 63 | + ] |
| 64 | + ) |
| 65 | + with open("fav_albums.csv", "w") as file: |
| 66 | + wr = csv.writer(file, quoting=csv.QUOTE_ALL) |
| 67 | + for album in my_albums: |
| 68 | + wr.writerow( |
| 69 | + [album.id, album.user_date_added, album.artist.name, album.name] |
| 70 | + ) |
| 71 | + with open("fav_artists.csv", "w") as file: |
| 72 | + wr = csv.writer(file, quoting=csv.QUOTE_ALL) |
| 73 | + for artist in my_artists: |
| 74 | + wr.writerow([artist.id, artist.user_date_added, artist.name]) |
| 75 | + with open("fav_playlists.csv", "w") as file: |
| 76 | + wr = csv.writer(file, quoting=csv.QUOTE_ALL) |
| 77 | + for playlist in my_playlists: |
| 78 | + wr.writerow( |
| 79 | + [playlist.id, playlist.created, playlist.type, playlist.name] |
| 80 | + ) |
| 81 | + |
| 82 | + def do_transfer(self): |
| 83 | + # do login for src and dst Tidal account |
| 84 | + session_src = self.session_src.get_session() |
| 85 | + session_dst = self.session_dst.get_session() |
| 86 | + logger.info("Login to user A (source)...") |
| 87 | + if not session_src.login_oauth_file(oauth_file1): |
| 88 | + logger.error("Login to Tidal user...FAILED!") |
| 89 | + exit(1) |
| 90 | + logger.info("Login to user B (destination)...") |
| 91 | + if not session_dst.login_oauth_file(oauth_file2): |
| 92 | + logger.error("Login to Tidal user...FAILED!") |
| 93 | + exit(1) |
| 94 | + |
| 95 | + # get current user favourites (source) |
| 96 | + my_tracks = session_src.user.favorites.tracks() |
| 97 | + my_albums = session_src.user.favorites.albums() |
| 98 | + my_artists = session_src.user.favorites.artists() |
| 99 | + my_playlists = session_src.user.playlist_and_favorite_playlists() |
| 100 | + # my_mixes = self._active_session.user.mixes() |
| 101 | + |
| 102 | + # export to csv |
| 103 | + self.export_csv(my_tracks, my_albums, my_artists, my_playlists) |
| 104 | + |
| 105 | + # add favourites to new user |
| 106 | + logger.info("Adding favourites to Tidal user B...") |
| 107 | + for idx, track in enumerate(my_tracks): |
| 108 | + logger.info("Adding track {}/{}".format(idx, len(my_tracks))) |
| 109 | + try: |
| 110 | + session_dst.user.favorites.add_track(track.id) |
| 111 | + time.sleep(0.1) |
| 112 | + except: |
| 113 | + logger.error("error while adding track {} {}".format(track.id, track.name)) |
| 114 | + |
| 115 | + for idx, album in enumerate(my_albums): |
| 116 | + logger.info("Adding album {}/{}".format(idx, len(my_albums))) |
| 117 | + try: |
| 118 | + session_dst.user.favorites.add_album(album.id) |
| 119 | + time.sleep(0.1) |
| 120 | + except: |
| 121 | + logger.error("error while adding album {} {}".format(album.id, album.name)) |
| 122 | + |
| 123 | + for idx, artist in enumerate(my_artists): |
| 124 | + logger.info("Adding artist {}/{}".format(idx, len(my_artists))) |
| 125 | + try: |
| 126 | + session_dst.user.favorites.add_artist(artist.id) |
| 127 | + time.sleep(0.1) |
| 128 | + except: |
| 129 | + logger.error("error while adding artist {} {}".format(artist.id, artist.name)) |
| 130 | + |
| 131 | + for idx, playlist in enumerate(my_playlists): |
| 132 | + logger.info("Adding playlist {}/{}".format(idx, len(my_playlists))) |
| 133 | + try: |
| 134 | + session_dst.user.favorites.add_playlist(playlist.id) |
| 135 | + time.sleep(0.1) |
| 136 | + except: |
| 137 | + logger.error( |
| 138 | + "error while adding playlist {} {}".format( |
| 139 | + playlist.id, playlist.name |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + TidalTransfer().do_transfer() |
0 commit comments