Skip to content

Commit

Permalink
User library retrieval works and is now testable.
Browse files Browse the repository at this point in the history
  • Loading branch information
j-jacobson committed Aug 21, 2023
1 parent ea17463 commit aac1593
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.p8
*key*
.DS_Store
*token*
12 changes: 6 additions & 6 deletions applemusicpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,29 +892,29 @@ def current_user_saved_tracks(self, limit=10, offset=0):
url = self.root + 'me/library/songs'
return self._user_get(url, limit=limit, offset=offset)

def current_user_playlists(self):
def current_user_playlists(self, limit=10, offset=0):
"""
Retrieve all playlists of the current user in Apple Music.
:return: Playlists data in JSON format.
"""
url = self.root + 'me/library/playlists'
return self._user_get(url)
return self._user_get(url, limit=limit, offset=offset)

def current_user_saved_albums(self):
def current_user_saved_albums(self, limit=10, offset=0):
"""
Retrieve saved albums of the current user in Apple Music.
:return: Saved albums data in JSON format.
"""
url = self.root + 'me/library/albums'
return self._user_get(url)
return self._user_get(url, limit=limit, offset=offset)

def current_user_followed_artists(self):
def current_user_followed_artists(self, limit=10, offset=0):
"""
Retrieve artists followed by the current user in Apple Music.
:return: Followed artists data in JSON format.
"""
url = self.root + 'me/library/artists'
return self._user_get(url)
return self._user_get(url, limit=limit, offset=offset)

def user_playlist_create(self, playlist_name, tracks):
"""
Expand Down
85 changes: 80 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,64 @@
from applemusicpy import AppleMusic
import unittest
import unittest, os

class UserTests(unittest.TestCase):
def setUp(self):
# albums
self.born_to_run = '310730204'
self.ready_to_die = '204669326'
# songs
self.xo_tour_life = '1274153124'
self.new_patek = '1436530704'
# artists
self.lil_pump = '1129587661'
self.smokepurpp = '1122104172'

class TestApple(unittest.TestCase):
def test_album_retrieve(self):
albums = am.current_user_saved_albums()
self.assertIsNotNone(albums)
# You can add more assertions here based on the expected behavior of the function

def test_playlist_retrieve(self):
playlists = am.current_user_playlists()
self.assertIsNotNone(playlists)
# You can add more assertions here based on the expected behavior of the function

def test_song_retrieve(self):
songs = am.current_user_saved_tracks()
self.assertIsNotNone(songs)
# You can add more assertions here based on the expected behavior of the function

def test_artist_retrieve(self):
artists = am.current_user_followed_artists()
self.assertIsNotNone(artists)
# You can add more assertions here based on the expected behavior of the function

def test_album_set(self):
album_id = self.born_to_run
response = am.current_user_saved_albums_add(album_id)
self.assertTrue(response) # Check if the album was added successfully
# You can add more assertions here based on the expected behavior of the function

def test_playlist_set(self):
playlist_name = "Test Playlist"
tracks = [self.xo_tour_life, self.new_patek] # Replace with actual track IDs
response = am.user_playlist_create(playlist_name, tracks)
self.assertTrue(response) # Check if the playlist was added successfully
# You can add more assertions here based on the expected behavior of the function

def test_song_set(self):
song_id = self.xo_tour_life
response = am.current_user_saved_tracks_add(song_id)
self.assertTrue(response) # Check if the song was added successfully
# You can add more assertions here based on the expected behavior of the function

def test_artist_set(self):
artist_id = self.smokepurpp
response = am.current_user_followed_artists_add(artist_id)
self.assertTrue(response) # Check if the artist was added successfully
# You can add more assertions here based on the expected behavior of the function

class BaseTests(unittest.TestCase):

def setUp(self):
# albums
Expand Down Expand Up @@ -272,7 +328,7 @@ def test_charts(self):
self.assertTrue(expected_name == actual_name, f"Expected: {expected_name}, Actual: {actual_name}")

if __name__ == '__main__':
# These tests require API authorization, so need to read in keys
# These tests require API authorization, so need to read in keys and user token
keys = {}

with open('private_key.p8', 'r') as f:
Expand All @@ -283,6 +339,25 @@ def test_charts(self):
name, val = line.partition('=')[::2]
keys[name.strip()] = val.strip()

am = AppleMusic(secret_key=keys['secret'], key_id=keys['keyID'], team_id=keys['teamID'])
test_loader = unittest.TestLoader()

user_token_file = 'music_user_token.txt'
if os.path.exists(user_token_file):
with open(user_token_file, 'r') as f:
music_user_token = f.read().strip()
am = AppleMusic(secret_key=keys['secret'], key_id=keys['keyID'], team_id=keys['teamID'], music_user_token=music_user_token)

print("Running tests with user token...")
user_test_suite = test_loader.loadTestsFromTestCase(UserTests)
else:
print("No music_user_token.txt found. Running normal tests...")
am = AppleMusic(secret_key=keys['secret'], key_id=keys['keyID'], team_id=keys['teamID'])

base_test_suite = test_loader.loadTestsFromTestCase(BaseTests)

all_tests = unittest.TestSuite()
#all_tests.addTests(base_test_suite)
if 'user_test_suite' in locals():
all_tests.addTests(user_test_suite)

unittest.main()
unittest.TextTestRunner(verbosity=2).run(all_tests)

0 comments on commit aac1593

Please sign in to comment.