예제 #1
0
def _sample_songs(input_queue, result_queue):
    logging.basicConfig(
        level=logging.DEBUG,
        format=
        '[%(asctime)s]%(processName)s:%(name)s:%(levelname)s - %(message)s')

    while True:
        try:
            song_path_id, is_good_song, song_hash, song_path = input_queue.get_nowait(
            )
        except queue.Empty:
            break
        else:
            song = Song(song_path, True)

            __logger__.debug("Sampling sequences for song: [{}]".format(
                song.path))

            samples = song.song.get_array_of_samples()
            frame_rate = song.song.frame_rate
            left_samples = np.array(samples[0::2])
            right_samples = np.array(samples[1::2])

            left_samples_sets, right_samples_sets, starting_sample_indexes = _select_random_samples_sets(
                NUMBER_OF_RANDOM_SAMPLES, SECONDS_PER_RANDOM_SAMPLES,
                left_samples, right_samples, frame_rate)

            result_queue.put(
                (song_path_id, song_hash, is_good_song,
                 SongSamplesDatabase.SongSamplesODBC.serialize_object(
                     left_samples_sets),
                 SongSamplesDatabase.SongSamplesODBC.serialize_object(
                     right_samples_sets), starting_sample_indexes))

    result_queue.put(1)
예제 #2
0
 def get_favorites(cls, ids=None, check_collection=False, score=None):
     """Gets a list of favorite songs from the database and collection from the given ids.
     If no ids are given it will return all favorites.
     Args:
        ids([int]): The database ids of requested favorite songs.
        check_collection(bool): indicates if the file system should be checked(True) or not(False).
        score(float): minimum score of songs to search
     Returns:
         ([Song], [Song]): tuple of :
             list of favorite songs that matches the arguments specified
             list of invalid songs that fail to run the Song validation
     """
     # TODO: create the interface to retrieve by ids on the server and complete it here
     try:
         if score:
             result = cls._music_db.api_songs_get_songs(score=score)
         else:
             result = cls._music_db.api_songs_get_songs()
     except Exception as ex:
         config.logger.exception('Exception when getting favorite songs')
         raise ex
     if check_collection:
         return cls._get_songs_in_fs(result.songs)
     else:
         return [Song(song) for song in result.songs], []
    def get_queue(self):
        queue = []

        for song in self.songs:
            queue.append(Song(song))

        data = {'amount_songs': self.queue.qsize()}
        return queue[0].create_embed(song_queue=queue, data=data)
예제 #4
0
def build_song_indexes():
    #: :type: unqlite.Collection
    good_songs_collection = SongInfoDatabase.db.collection(
        database.DB_GOOD_SONGS)
    #: :type: unqlite.Collection
    bad_songs_collection = SongInfoDatabase.db.collection(
        database.DB_BAD_SONGS)

    for collection in [good_songs_collection, bad_songs_collection]:
        if not collection.exists():
            collection.create()

    every_hash = set()
    with SongInfoDatabase.db.transaction():
        for collection, root_dir in [
            (good_songs_collection, FileStore.good_songs_dir),
            (bad_songs_collection, FileStore.bad_songs_dir)
        ]:
            all_elements = collection.all()
            every_hash.update({
                SongInfoDatabase.SongInfoODBC.SONG_HASH.get_value(elem)
                for elem in all_elements
            })
            for dir_name, subdir_list, file_list in os.walk(root_dir,
                                                            topdown=False):
                for fn in file_list:
                    song_path = os.path.join(root_dir, dir_name, fn)
                    song = Song(song_path)
                    song_path_hash = Song.compute_path_hash(song_path.encode())
                    if not song_path_hash:
                        raise ValueError(
                            "Failed to hash path: [{}]".format(song_path))

                    if song_path_hash not in every_hash:
                        record = SongInfoDatabase.SongInfoODBC.initialize_record(
                            song_path_hash, song_path, None,
                            song.song.getframerate())
                        collection.store(record)
                        __logger__.debug("Adding song: [{}]".format(song_path))
                        every_hash.add(song_path_hash)

    __logger__.debug("[{}] good songs".format(len(good_songs_collection)))
    __logger__.debug("[{}] bad songs".format(len(bad_songs_collection)))
	async def play(self, ctx, *, args):
		try:
			voice_channel = self.get_voice_channel(ctx, ctx.message.author)
		except TypeError:
			await ctx.send(ctx.message.author.mention + 
						"You must be in a voice channel if you want to headbang bro")
			return

		if not await self.bot_connected(voice_channel):
			self.music_player = Music(ctx)

		data_source, data = await self.music_player.create_source(args)
		song = Song(data)
		data['file_path'] = data_source
		data['requester'] = ctx.message.author

		await self.music_player.add_to_queue(discord.FFmpegPCMAudio(data_source), data)
 def get_current_song(self):
     song = Song(self.current_song[1])
     return song.create_embed()
예제 #7
0
    def _get_songs_in_fs(cls, song_list):
        """Gets songs from the given list that exist on the File System.
        Returns:
            list with the songs that exist on the file system
        """

        existing_songs = []
        wrong_songs = []
        if not isinstance(song_list, list):
            config.logger.error(f'song_list is not of list type')
        else:
            # get albums from collection to search for this song list
            albums_dict, _, _ = cls._update_albums_from_collection()
            # get the music directory tree all at once.
            # When checking for existence of a song file it's more efficient
            music_file_tree = cls._get_music_directory_tree()

            # for every song in the database result search the album info in the database
            for db_song in song_list:
                # if it's the same band then we continue, otherwise there might be a mistake
                song = Song(
                    db_song
                )  # merge the database info with the info got from the filesystem
                band_key = db_song.album.band.casefold()
                if band_key in albums_dict.keys():
                    found_album = False
                    # now check for all albums for this band in the collection to that one with same database id
                    # show the band name instead of the key
                    for album in albums_dict[band_key].values():
                        if album.id == db_song.album.id:
                            found_album = True
                            song.album = Album()
                            song.album.merge(album)
                            song.album.path = album.path
                            found_song = False
                            if song.file_name:
                                abs_path = os.path.join(
                                    album.path, song.file_name)
                                file_path, song_file_name = os.path.split(
                                    abs_path)
                                relative_path = relpath(
                                    file_path, cls._collection_root)
                                relative_path = relative_path.split('/')
                                files = cls._check_path_in_music_tree(
                                    music_file_tree, relative_path)
                                if song_file_name in files:
                                    song.set_abs_path(abs_path)
                                    found_song = True
                                else:
                                    songs_logger.info(
                                        f'Could not find song with title {song.title} and file {song.file_name} '
                                        f'from album title {song.album.title} and band {song.album.band} '
                                        f'among the files of the corresponding album'
                                    )
                                    # search for the song in the album path
                                    found_song = cls._search_and_update_song_file_name(
                                        song)
                            else:
                                songs_logger.info(
                                    f'Song with title {song.title} from album title '
                                    f'{song.album.title} and band {song.album.band} '
                                    f'does not have a file name in the database'
                                    f'Trying to find the corresponding file')
                                # search for the song in the album path
                                found_song = cls._search_and_update_song_file_name(
                                    song)

                            if found_song:
                                existing_songs.append(song)
                                break
                            else:
                                wrong_songs.append(song)

                    if not found_album:
                        songs_logger.info(
                            f"Album with database id {db_song.album.id} not found in "
                            f"database for song title {db_song.title} and {db_song.id}. "
                            f"Albums of band {db_song.album.band} are:"
                            f"{albums_dict[band_key].values()}")
                        wrong_songs.append(song)
                else:
                    songs_logger.info(
                        f"Band {db_song.album.band} not found in the list of bands"
                    )
                    wrong_songs.append(song)
        return existing_songs, wrong_songs
예제 #8
0
    def add_songs_from_reviews(cls):
        """Checks the reviews of all albums to analyze the favorite songs written on them. These are written with double
         quotes in the review. With this it will check if the song exists in the album and try to identify the filename.
         With this it will:
        1) Check if the song is already added as favorite.
        2) If not, add the song to the database with the corresponding info if the file is found
        3) If not found, add to a list to return to the user to find later on.
        Returns:
            [song]: list of songs from the reviews that were not found in the collection.
        """

        songs_not_found = []

        # if the search for the collection was not performed before it will do it now
        if not cls._valid_albums:
            cls._update_albums_from_collection()
        result = cls._music_db.api_songs_get_songs()
        song_db_list = result.songs
        # create a dictionary with album ids as keys and list of favorite songs belonging to that album as values
        song_db_dict = {}
        for song_db in song_db_list:
            if song_db.album.id in song_db_dict:
                song_db_dict[song_db.album.id].append(song_db)
            else:
                song_db_dict[song_db.album.id] = [song_db]
        for band in cls._valid_albums:
            for album in cls._valid_albums[band].values():
                if not album.review:
                    album_logger.error(
                        f'No review for album "{album.title}. Please add a review."'
                    )
                else:
                    song_title_list = re.findall(r'"([^"]*)"', album.review)
                    if not song_title_list:
                        album_logger.warning(
                            f'Album {album.title} from band {album.band} has no favorites in the review'
                        )
                    else:
                        songs_of_album = song_db_dict.get(album.id)
                        for song in song_title_list:
                            config.logger.info(
                                f'Checking song with title "{song}"')
                            song_found = False
                            if songs_of_album:
                                for song_in_album in songs_of_album:
                                    if song_in_album.title.casefold().strip(
                                    ) == song.casefold().strip():
                                        song_found = True
                                        songs_of_album.remove(song_in_album)
                                        break
                            if song_found:
                                config.logger.info(
                                    f'Song {song} was found for album {album.title}'
                                )
                            else:
                                new_song = Song()
                                new_song.album = album
                                new_song.title = song
                                # TODO: debatable if it should be initialized this way but it's compulsory so we need
                                #  something
                                new_song.score = album.score
                                if not cls._search_and_update_song_file_name(
                                        new_song):
                                    songs_not_found.append(new_song)
        return songs_not_found