Esempio n. 1
0
def load_data():
    new_artist = None
    new_album = None
    artist_list = []

    with open("albums.txt", "r") as albums:
        for line in albums:
            artist_field, album_field, year_field, song_field = tuple(
                line.strip('\n').split('\t'))
            year_field = int(year_field)
            print(artist_field, album_field, year_field, song_field)

            if new_artist is None:
                new_artist = Artist(artist_field)
            elif new_artist.name != artist_field:
                new_artist.add_album(new_album)
                artist_list.append(new_artist)
                new_artist = Artist(artist_field)
                new_album = None

            if new_album is None:
                new_album = Album(album_field, year_field, new_artist)
            elif new_album.name != album_field:
                new_artist.add_album(new_album)
                new_album = Album(album_field, year_field, new_artist)

            new_song = Song(song_field, new_artist)
            new_album.add_song(new_song)

        if new_artist is not None:
            if new_album is not None:
                new_artist.add_album(new_album)
            artist_list.append(new_artist)

    return artist_list
 def setUpClass(cls) -> None:
     cls.artist1 = Artist('Steve', 'UK')
     cls.artist2 = Artist('John', 'USA')
     cls.album1 = Album('s_album1', 2019, cls.artist1)
     cls.album2 = Album('j_album1', 2017, cls.artist2)
     cls.song1 = Song('s_song', cls.artist1, 2018, 180, cls.album1)
     cls.song2 = Song('j_song', cls.artist2, 2017, 150, cls.album2)
 def test_adding_song_raises_error_when_artists_doesnt_match(self):
     a = Artist('Lana Del Ray', 'USA')
     a1 = Artist('Adele', 'UK')
     b = Album('Born to Die', 2012, 'alternative/indie', a)
     b1 = Album('21', 2011, 'pop/soul', a1)
     c = Song('Blue Jeans', a, 2012, 240, b)
     with self.assertRaises(WrongArtistError):
         b1.add_song(c)
Esempio n. 4
0
def get_tracks_from_albums_with_certain_artist(album_ids: [str], artist_id: str) -> [Track]:
    tracks = []
    query = "https://api.spotify.com/v1/albums"
    while len(album_ids) > 0:
        id_strings = ""
        for i in range(min(20, len(album_ids))):
            id_strings += album_ids[i] + ","
        id_strings = id_strings[:-1]
        album_ids = album_ids[20:]
        params = {"ids": id_strings}
        try:
            aos = requests.get(query, params, headers = HEADERS).json()["albums"]
        except KeyError:
            try:
                sleep(1)
                aos = requests.get(query, params, headers = HEADERS).json()["albums"]
            except:
                print("Spotify did not respond as expected.")
                sys.exit()
        for album in aos:
            for track in album["tracks"]["items"]:
                artists = track["artists"]
                songs_artists = []
                to_be_added = False
                for artist in artists:
                    if artist["id"] == artist_id: to_be_added = True
                    artist_object = Artist(artist["id"], artist["name"])
                    songs_artists.append(artist_object)
                if to_be_added:
                    tracks.append(Track(songs_artists, track["explicit"], track["id"], track["name"]))
    return tracks
Esempio n. 5
0
def get_playlist_tracks(playlist_id: str) -> [Track]:
    tracks = []
    query = "https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id)
    page = 0
    params = {"limit": 50, "offset": 50*page}
    #playlist track object has a track object in "items"
    try:
        ptos = requests.get(query, params, headers = HEADERS).json()["items"]
    except KeyError:
        raise KeyExpiredError()
    while len(ptos) > 0:
        for pto in ptos:
            pto = pto["track"]
            artists = pto["artists"]
            songs_artists = []
            for artist in artists:
                artist_object = Artist(artist["id"], artist["name"])
                songs_artists.append(artist_object)
            tracks.append(Track(songs_artists, pto["explicit"], pto["id"], pto["name"]))
        page += 1
        params = {"limit": 50, "offset": page*50}
        try:
            ptos = requests.get(query, params, headers = HEADERS).json()["items"]
        except KeyError:
            raise KeyExpiredError()
    return tracks
Esempio n. 6
0
 def test_add_song_works_correct(self):
     art1 = Artist("Mettalica", "USA")
     alb1 = Album('Death magnetic', 2008, 'Thrash metal', art1)
     song1 = Song('That Was Just Your Life', 2008, 426, art1, alb1)
     len_1 = len(alb1.songs)
     self.assertEqual(alb1.songs, [song1])
     song2 = Song('The End of the Line', 2008, 470, art1, alb1)
     len_2 = len(alb1.songs)
     self.assertEqual(len_1, len_2 - 1)
     song1 = Song('That Was Just Your Life', 2008, 426, art1, alb1)
     len_3 = len(alb1.songs)
     self.assertEqual(len_2, len_3)
Esempio n. 7
0
def search_for_artist(results: int) -> [Artist]:
    query = "https://api.spotify.com/v1/search"
    params = {"q": input("Enter an artist:\n"), "type": "artist", "limit": results}
    try:
        search_results = requests.get(query, params, headers = HEADERS).json()["artists"]["items"]
    except KeyError:
            try:
                sleep(1)
                search_results = requests.get(query, params, headers = HEADERS).json()["artists"]["items"]
            except:
                print("Spotify did not respond as expected.")
                sys.exit()
    artists = []
    for artist in search_results:
        artists.append(Artist(artist["id"], artist["name"]))
    return artists
Esempio n. 8
0
def get_album_tracks(album_id: str) -> [Track]:
    tracks = []
    query = "https://api.spotify.com/v1/albums/{}/tracks".format(album_id)
    params = {"limit": 50}
    try:
        tos = requests.get(query, params, headers = HEADERS).json()["items"]
    except KeyError:
        raise KeyExpiredError()
    for to in tos:
        artists = to["artists"]
        songs_artists = []
        for artist in artists:
            artist_object = Artist(artist["id"], artist["name"])
            songs_artists.append(artist_object)
        tracks.append(Track(songs_artists, to["explicit"], to["id"], to["name"]))
    return tracks
Esempio n. 9
0
def search_for_track(results: int) -> [Track]:
    query = "https://api.spotify.com/v1/search"
    params = {"q": input("Enter a track:\n"), "type": "track", "limit": results}
    tos = None
    try:
        tos = requests.get(query, params, headers = HEADERS).json()["tracks"]["items"]
    except KeyError:
            try:
                sleep(1)
                tos = requests.get(query, params, headers = HEADERS).json()["tracks"]["items"]
            except:
                print("Spotify did not respond as expected.")
                sys.exit()
    tracks = []
    for to in tos:
        artists = to["artists"]
        songs_artists = []
        for artist in artists:
            artist_object = Artist(artist["id"], artist["name"])
            songs_artists.append(artist_object)
        tracks.append(Track(songs_artists, to["explicit"], to["id"], to["name"]))
    return tracks
 def test_creating_song_adds_it_to_artists_songs_list(self):
     a = Artist('Ed Sheeran', 'UK')
     b = Album('Divide', 2017, 'pop', a)
     c = Song('Shape of You', a, 2017, 234, b)
     self.assertIn(c, a.songs)
 def test_repr_check(self):
     a = Artist('Ed Sheeran', 'UK')
     b = Album('Divide', 2017, 'pop', a)
     c = Song('Supermarket Flowers', a, 2017, 221, b)
     self.assertEqual(repr(c), c.name)
 def test_creating_song_adds_it_to_albums_songs_list(self):
     a = Artist('Ed Sheeran', 'UK')
     b = Album('Divide', 2017, 'pop', a)
     c = Song('New Man', a, 2017, 189, b)
     self.assertIn(c, b.songs)
 def test_creating_album_adds_it_to_artists_albums_list(self):
     a = Artist('Lana Del Ray', 'USA')
     b = Album('Born to Die', 2012, 'alternative/indie', a)
     self.assertIn(b, a.albums)
 def test_song_number_is_calculated_right(self):
     a = Artist('Ed Sheeran', 'UK')
     b = Album('Divide', 2017, 'pop', a)
     c = Song('Galway Girl', a, 2017, 248, b)
     d = Song('Castle on the Hill', a, 2017, 421, b)
     self.assertEqual(a.songs_number, 2)
Esempio n. 15
0
from classes import Artist, Album, Song
# import classes as clx

if __name__ == "__main__":
    art1 = Artist("Mettalica", "USA")
    alb1 = Album('Death magnetic', 2008, 'Thrash metal', art1)
    song1 = Song('That Was Just Your Life', 2008, 426, art1, alb1)
    song2 = Song('The End of the Line', 2008, 470,  art1, alb1)
    alb2 = Album('St. Anger', 2003, 'Thrash metal', art1)
    song3 = Song('Frantic', 2003, 348,  art1, alb2)
    art2 = Artist('Motorhead', "USA")
    song4 = Song('Enter Sandman', 1991, 330, art1)
    song4.add_artist(art2)

    print(art1.songs_number, alb1.duration,
          alb1.songs_number, art1.album_number,
          song4.features, art2.album_number)


 def test_albums_number_is_calculated_right(self):
     a = Artist('Panic At The Disco', 'USA')
     b = Album('Pray for the Wicked', 2018, 'rock/alternative', a)
     c = Album('Death of a Bachelor', 2016, 'rock/alternative', a)
     self.assertEqual(a.albums_number, 2)
Esempio n. 17
0
from classes import Artist, Album, Song

if __name__ == '__main__':
    a = Artist('Ed Sheeran', 'GB')
    s = Artist('Edn', 'GB')
    b = Album('Divide', 2015, 'pop', a)
    print(a.albums)
    print(a.songs)
    c = Song('Galway Girl', a, 2016, 150, b, 9)
    print(a.songs)

    print(c.duration)
Esempio n. 18
0
from classes import Artist, Album, Song

if __name__ == '__main__':
    artist1 = Artist('Steve', 'UK')
    artist2 = Artist('John', 'USA')

    steve_album1 = Album('s_album1', 2019, 'Indie rock', artist1)
    john_album1 = Album('j_album1', 2018, 'Alternative rock', artist2)
    john_album2 = Album('j_album2', 2017, 'Heavy metal', artist2)

    steve_song1 = Song('s_song1', artist1, [artist2], 2019, 180, steve_album1)
    steve_song2 = Song('s_song2', artist1, [artist2], 2019, 220)
    john_song1 = Song('j_song1', artist2, [artist1], 2017, 150, john_album1)
    john_song2 = Song('j_song2', artist2, [artist1], 2018, 300, john_album2)
    john_song3 = Song('j_song3', artist2, [artist1], 2019, 180, john_album1)
    # Exception:
    steve_song3 = Song('s_song3', artist1, [artist2], 2019, 210, john_album1)

    # print(artist1.albums)
    # print(artist2.albums)
    # print(steve_album1.songs)
    # print(john_album1.songs)
    # print(artist1.songs)
    # print(artist2.songs)
    # print(john_album1.duration)
 def test_duration_is_calculated_right(self):
     a = Artist('Lana Del Ray', 'USA')
     b = Album('Born to Die', 2012, 'alternative/indie', a)
     c = Song('Blue Jeans', a, 2012, 240, b)
     d = Song('Born to Die', a, 2012, 320, b)
     self.assertEqual(b.duration, c.duration + d.duration)
 def setUpClass(cls) -> None:
     cls.artist1 = Artist('Steve', 'UK')
     cls.album1 = Album('s_album1', 2019, cls.artist1)
     cls.song1 = Song('s_song', cls.artist1, 2018, 180, cls.album1)
 def test_repr_check(self):
     a = Artist('Nickelback', 'Canada')
     self.assertEqual(repr(a), a.name)
 def song_number_is_calculated_right(self):
     a = Artist('Lana Del Ray', 'USA')
     b = Album('Born to Die', 2012, 'alternative/indie', a)
     c = Song('Blue Jeans', a, 2012, 240, b)
     d = Song('Born to Die', a, 2012, 320, b)
     self.assertEqual(b.songs_number, 2)