예제 #1
0
def save_deezer_data_to_db(input):
    # "https://api.deezer.com/search" + "?", params = {'q': name}

    for key, value in input.items():
        if key == 'data':
            number_of_items = len(value)

    # iterate all objects and save to data
    for i in range(number_of_items):
        each_object = input['data'][i]

        artist = Artist()
        artist.Name = each_object['artist']['name']
        artist.PictureURL = each_object['artist']['picture_medium']
        artist.ArtistDeezerID = each_object['artist']['id']

        try:
            artist = Artist.objects.get(Name=artist.Name)
        except Artist.DoesNotExist:
            artist.save()
        except Artist.MultipleObjectsReturned:
            pass

        album = Album()
        album.Artist = artist
        album.Title = each_object['album']['title']
        album.PictureURL = each_object['album']['cover_medium']
        album.AlbumDeezerID = each_object['album']['id']
        album.ArtistDeezerID = each_object['artist']['id']
        try:
            album = Album.objects.get(Title=album.Title)
        except Album.DoesNotExist:
            album.save()
        except Album.MultipleObjectsReturned:
            pass

        song = Song()
        song.Album = album
        song.Artist = artist
        song.Title = each_object['title']
        song.PictureURL = each_object['album']['cover_medium']
        song.PreviewURL = each_object['preview']
        song.AlbumDeezerID = each_object['album']['id']
        song.ArtistDeezerID = each_object['artist']['id']
        song.SongDeezerID = each_object['id']
        try:
            song = Song.objects.get(Title=song.Title)
        except Song.DoesNotExist:
            song.save()
        except Song.MultipleObjectsReturned:
            pass
예제 #2
0
def save_deezer_album_songs_to_db(result, album_id, artist_id):
    for data in result['data']:
        album = Album.objects.get(AlbumDeezerID__exact=album_id)
        artist = Artist.objects.get(ArtistDeezerID__exact=artist_id)

        song = Song()
        song.Album = album
        song.Artist = artist
        song.Title = data['title']
        song.PictureURL = album.PictureURL
        song.PreviewURL = data['preview']
        song.AlbumDeezerID = album.AlbumDeezerID
        song.ArtistDeezerID = artist.ArtistDeezerID
        song.SongDeezerID = data['id']

        try:
            song = Song.objects.get(Title=song.Title)
        except Song.DoesNotExist:
            song.save()
        except Song.MultipleObjectsReturned:
            pass
예제 #3
0
def searchRequest(name="", conn="", artist="", album=""):

    if conn == "":
        searchRequest(name, conn="artist")
        searchRequest(name, conn="album")
        searchRequest(name, conn="track")

    # Send request to search information
    result = requests.get("https://api.deezer.com/search/" + conn + "?",
                          params={'q': name})

    # Check if the HTTP response is OK
    if result.status_code == 200:
        result = result.json()

        # Don't need to populate to much
        if (result['total'] > 25):
            nbr_results = 25
        else:
            nbr_results = result['total']

        # Browse the different element in the JSON answer
        for i in range(len(result['data'])):
            data = result['data'][i]

            # If an artist field is found, search his album
            if data['type'] == "artist":

                print(data['name'], ",", data['type'])

                # Create a new artist
                artistDB = Artist()
                artistDB.Name = data['name']
                artistDB.PictureURL = data['picture_medium']
                artistDB.ArtistDeezerID = data['id']

                # If the artist is not already in the DB, save it
                try:
                    artistDB = Artist.objects.get(Name=artistDB.Name)
                except Artist.DoesNotExist:
                    artistDB.save()

                # Search the albums of the artist
                searchRequest(name=data['name'],
                              artist=data['name'],
                              conn="album")

                # Populate the database only with the first artist found
                break

            # If an album field is found, search his tracks
            if data['type'] == "album" and\
              (data['artist']['name'] == artist or artist == ""):

                print(artist, ",", data['title'], ",", data['type'])

                artistDB = Artist.objects.get(Name=artist)

                # Create a new album
                albumDB = Album()
                albumDB.Artist = artistDB
                albumDB.Title = data['title']
                albumDB.PictureURL = data['cover_medium']
                albumDB.AlbumDeezerID = data['id']
                albumDB.ArtistDeezerID = data['artist']['id']

                # If the album is not already in the DB, save it
                try:
                    albumDB = Album.objects.get(Title=albumDB.Title,
                                                Artist=artistDB)
                except Album.DoesNotExist:
                    albumDB.save()

                # Search the songs of the artist
                searchRequest(name=data['title'],
                              conn="track",
                              artist=artist,
                              album=data['title'])

                # Populate the database only with the four first albums found
                if i > 3:
                    break

            if data['type'] == "track" and\
              (data['artist']['name'] == artist or artist == "") and\
              (data['album']['title'] == album  or album  == ""):

                print(artist, ",", album, ",", data['title'], ",",
                      data['type'])

                artistDB = Artist.objects.get(Name=artist)
                albumDB = Album.objects.get(Title=album, Artist=artistDB)

                # Create a new song
                songDB = Song()
                songDB.Album = albumDB
                songDB.Artist = artistDB
                songDB.Title = data['title']
                songDB.PictureURL = data['album']['cover_medium']
                songDB.PreviewURL = data['preview']
                songDB.AlbumDeezerID = data['album']['id']
                songDB.ArtistDeezerID = data['artist']['id']
                songDB.SongDeezerID = data['id']

                # If the song is not already in the DB, save it
                try:
                    songDB = Song.objects.get(Title=songDB.Title,
                                              Album=albumDB,
                                              Artist=artistDB)
                except Song.DoesNotExist:
                    songDB.save()