Exemple #1
0
def forcePostProcess():

    if not headphones.DOWNLOAD_DIR:
        logger.error(
            'No DOWNLOAD_DIR has been set. Set "Music Download Directory:" to your SAB download directory on the settings page.'
        )
        return
    else:
        download_dir = headphones.DOWNLOAD_DIR.encode("utf-8")

    logger.info("Checking to see if there are any folders to process in download_dir: %s" % download_dir)
    # Get a list of folders in the download_dir
    folders = [d for d in os.listdir(download_dir) if os.path.isdir(os.path.join(download_dir, d))]

    if len(folders):
        logger.info("Found %i folders to process" % len(folders))
        pass
    else:
        logger.info("Found no folders to process in: %s" % download_dir)
        return

        # Parse the folder names to get artist album info
    for folder in folders:

        albumpath = os.path.join(download_dir, folder)
        folder = unicode(folder, headphones.SYS_ENCODING, errors="replace")

        logger.info("Processing: %s" % folder)

        try:
            name, album, year = helpers.extract_data(folder)
        except:
            logger.info("Couldn't parse " + folder + " into any valid format.")
            continue
        if name and album and year:

            myDB = db.DBConnection()
            release = myDB.action(
                "SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?",
                [name, album],
            ).fetchone()
            if release:
                logger.info(
                    "Found a match in the database: %s - %s. Verifying to make sure it is the correct album"
                    % (release["ArtistName"], release["AlbumTitle"])
                )
                verify(release["AlbumID"], albumpath)
            else:
                logger.info("Querying MusicBrainz for the release group id for: %s - %s" % (name, album))
                from headphones import mb

                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error("Can not get release information for this album")
                    continue
                if rgid:
                    verify(rgid, albumpath)
                else:
                    logger.info("No match found on MusicBrainz for: %s - %s" % (name, album))
Exemple #2
0
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING))
        
    logger.info('Checking to see if there are any folders to process in download_dir(s): %s' % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info('Found %i folders to process' % len(folders))
    else:
        logger.info('Found no folders to process in: %s' % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))

    # Parse the folder names to get artist album info
    for folder in folders:

        folder_basename = os.path.basename(folder).decode(headphones.SYS_ENCODING, 'replace')

        logger.info('Processing: %s' % folder_basename)
    
        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            logger.info("Couldn't parse " + folder_basename + " into any valid format.")
            continue
        if name and album and year:
            
            myDB = db.DBConnection()
            release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?', [name, album]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle']))
                verify(release['AlbumID'], folder)
            else:
                logger.info('Querying MusicBrainz for the release group id for: %s - %s' % (name, album))
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error('Can not get release information for this album')
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s' % (name, album))
def forcePostProcess():
	
	if not headphones.DOWNLOAD_DIR:
		logger.error('No DOWNLOAD_DIR has been set. Set "Music Download Directory:" to your SAB download directory on the settings page.')
		return
	else:
		download_dir = headphones.DOWNLOAD_DIR
		
	logger.info('Checking to see if there are any folders to process in download_dir: %s' % download_dir)
	# Get a list of folders in the download_dir
	folders = [d for d in os.listdir(download_dir) if os.path.isdir(os.path.join(download_dir, d))]
	
	if len(folders):
		logger.info('Found %i folders: %s' % (len(folders), str(folders)))
		pass
	else:
		logger.info('Found no folders to process in: %s' % download_dir)
		return
	
	# Parse the folder names to get artist album info
	for folder in folders:

		folder = unicode(folder)
	
		albumpath = os.path.join(download_dir, folder)
		
		try:
			name, album, year = helpers.extract_data(folder)
		except:
			logger.info("Couldn't parse " + folder + " into any valid format.")
			continue
		if name and album and year:
			
			myDB = db.DBConnection()
			release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName=? and AlbumTitle=?', [name, album]).fetchone()
			if release:
				logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle']))
				verify(release['AlbumID'], albumpath)
			else:
				logger.info('Querying MusicBrainz for the release group id for: %s - %s' % (name, album))
				from headphones import mb
				try:
					rgid = mb.findAlbumID(name, album)
				except:
					logger.error('Can not get release information for this album')
					continue
				if rgid:
					verify(rgid, albumpath)
Exemple #4
0
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING, 'replace'))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING, 'replace'))
        
    # If DOWNLOAD_DIR and DOWNLOAD_TORRENT_DIR are the same, remove the duplicate to prevent us from trying to process the same folder twice.
    download_dirs = list(set(download_dirs))
    
    logger.info('Checking to see if there are any folders to process in download_dir(s): %s' % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        if not os.path.isdir(download_dir):
            logger.warn('Directory ' + download_dir.decode(headphones.SYS_ENCODING, 'replace') + ' does not exist. Skipping')
            continue
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info('Found %i folders to process' % len(folders))
    else:
        logger.info('Found no folders to process in: %s' % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))

    # Parse the folder names to get artist album info
    myDB = db.DBConnection()
    
    for folder in folders:

        folder_basename = os.path.basename(folder).decode(headphones.SYS_ENCODING, 'replace')

        logger.info('Processing: %s' % folder_basename)
        
        # First try to see if there's a match in the snatched table, then we'll try to parse the foldername
        # TODO: Iterate through underscores -> spaces, spaces -> dots, underscores -> dots (this might be hit or miss since it assumes
        # all spaces/underscores came from sab replacing values
        snatched = myDB.action('SELECT AlbumID, Title, Kind, Status from snatched WHERE FolderName LIKE ?', [folder_basename]).fetchone()
        if snatched:
            if headphones.KEEP_TORRENT_FILES and snatched['Kind'] == 'torrent' and snatched['Status'] == 'Processed':
                logger.info(folder_basename + ' is a torrent folder being preserved for seeding and has already been processed. Skipping.')
                continue
            else:
                logger.info('Found a match in the database: %s. Verifying to make sure it is the correct album' % snatched['Title'])
                verify(snatched['AlbumID'], folder, snatched['Kind'])
                continue
        
        # Try to parse the folder name into a valid format
        # TODO: Add metadata lookup
        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            name = None

        if name and album and year:
            
            release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?', [name, album]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle']))
                verify(release['AlbumID'], folder)
            else:
                logger.info('Querying MusicBrainz for the release group id for: %s - %s' % (name, album))
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error('Can not get release information for this album')
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s' % (name, album))
                    continue
                    
        else:
            try:
                possible_rgid = folder_basename[-36:]
                # re pattern match: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} 
                rgid = uuid.UUID(possible_rgid)
                    
            except:
                logger.info("Couldn't parse " + folder_basename + " into any valid format. If adding albums from another source, they must be in an 'Artist - Album [Year]' format, or end with the musicbrainz release group id")
                continue
            
            
            if rgid:
                rgid = possible_rgid
                release = myDB.action('SELECT ArtistName, AlbumTitle, AlbumID from albums WHERE AlbumID=?', [rgid]).fetchone()
                if release:
                    logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle']))
                    verify(release['AlbumID'], folder, forced=True)
                else:
                    logger.info('Found a (possibly) valid Musicbrainz identifier in album folder name - continuing post-processing')
                    verify(rgid, folder, forced=True)
Exemple #5
0
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(
            headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING, 'replace'))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(
            headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING,
                                                   'replace'))

    # If DOWNLOAD_DIR and DOWNLOAD_TORRENT_DIR are the same, remove the duplicate to prevent us from trying to process the same folder twice.
    download_dirs = list(set(download_dirs))

    logger.info(
        'Checking to see if there are any folders to process in download_dir(s): %s'
        % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info('Found %i folders to process' % len(folders))
    else:
        logger.info(
            'Found no folders to process in: %s' %
            str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))

    # Parse the folder names to get artist album info
    myDB = db.DBConnection()

    for folder in folders:

        folder_basename = os.path.basename(folder).decode(
            headphones.SYS_ENCODING, 'replace')

        logger.info('Processing: %s' % folder_basename)

        # First try to see if there's a match in the snatched table, then we'll try to parse the foldername
        # TODO: Iterate through underscores -> spaces, spaces -> dots, underscores -> dots (this might be hit or miss since it assumes
        # all spaces/underscores came from sab replacing values
        snatched = myDB.action(
            'SELECT AlbumID, Title, Kind, Status from snatched WHERE FolderName LIKE ?',
            [folder_basename]).fetchone()
        if snatched:
            if headphones.KEEP_TORRENT_FILES and snatched[
                    'Kind'] == 'torrent' and snatched['Status'] == 'Processed':
                logger.info(
                    folder_basename +
                    ' is a torrent folder being preserved for seeding and has already been processed. Skipping.'
                )
                continue
            else:
                logger.info(
                    'Found a match in the database: %s. Verifying to make sure it is the correct album'
                    % snatched['Title'])
                verify(snatched['AlbumID'], folder, snatched['Kind'])
                continue

        # Try to parse the folder name into a valid format
        # TODO: Add metadata lookup
        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            name = None

        if name and album and year:

            release = myDB.action(
                'SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?',
                [name, album]).fetchone()
            if release:
                logger.info(
                    'Found a match in the database: %s - %s. Verifying to make sure it is the correct album'
                    % (release['ArtistName'], release['AlbumTitle']))
                verify(release['AlbumID'], folder)
            else:
                logger.info(
                    'Querying MusicBrainz for the release group id for: %s - %s'
                    % (name, album))
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name),
                                          helpers.latinToAscii(album))
                except:
                    logger.error(
                        'Can not get release information for this album')
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s' %
                                (name, album))
                    continue

        else:
            try:
                possible_rgid = folder_basename[-36:]
                # re pattern match: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
                rgid = uuid.UUID(possible_rgid)

            except:
                logger.info(
                    "Couldn't parse " + folder_basename +
                    " into any valid format. If adding albums from another source, they must be in an 'Artist - Album [Year]' format, or end with the musicbrainz release group id"
                )
                continue

            if rgid:
                rgid = possible_rgid
                release = myDB.action(
                    'SELECT ArtistName, AlbumTitle, AlbumID from albums WHERE AlbumID=?',
                    [rgid]).fetchone()
                if release:
                    logger.info(
                        'Found a match in the database: %s - %s. Verifying to make sure it is the correct album'
                        % (release['ArtistName'], release['AlbumTitle']))
                    verify(release['AlbumID'], folder)
                else:
                    logger.info(
                        'Found a (possibly) valid Musicbrainz identifier in album folder name - continuing post-processing'
                    )
                    verify(rgid, folder)
Exemple #6
0
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(
            headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING, 'replace'))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(
            headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING,
                                                   'replace'))

    logger.info(
        'Checking to see if there are any folders to process in download_dir(s): %s'
        % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info('Found %i folders to process' % len(folders))
    else:
        logger.info(
            'Found no folders to process in: %s' %
            str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))

    # Parse the folder names to get artist album info
    myDB = db.DBConnection()

    for folder in folders:

        folder_basename = os.path.basename(folder).decode(
            headphones.SYS_ENCODING, 'replace')

        logger.info('Processing: %s' % folder_basename)

        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            name = None

        if name and album and year:

            release = myDB.action(
                'SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?',
                [name, album]).fetchone()
            if release:
                logger.info(
                    'Found a match in the database: %s - %s. Verifying to make sure it is the correct album'
                    % (release['ArtistName'], release['AlbumTitle']))
                verify(release['AlbumID'], folder)
            else:
                logger.info(
                    'Querying MusicBrainz for the release group id for: %s - %s'
                    % (name, album))
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name),
                                          helpers.latinToAscii(album))
                except:
                    logger.error(
                        'Can not get release information for this album')
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s' %
                                (name, album))
                    continue

        else:
            try:
                possible_rgid = folder_basename[-36:]
                # re pattern match: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
                rgid = uuid.UUID(possible_rgid)

            except:
                logger.info(
                    "Couldn't parse " + folder_basename +
                    " into any valid format. If adding albums from another source, they must be in an 'Artist - Album [Year]' format, or end with the musicbrainz release group id"
                )
                continue

            if rgid:
                rgid = possible_rgid
                release = myDB.action(
                    'SELECT ArtistName, AlbumTitle, AlbumID from albums WHERE AlbumID=?',
                    [rgid]).fetchone()
                if release:
                    logger.info(
                        'Found a match in the database: %s - %s. Verifying to make sure it is the correct album'
                        % (release['ArtistName'], release['AlbumTitle']))
                    verify(release['AlbumID'], folder)
                else:
                    logger.info(
                        'Found a (possibly) valid Musicbrainz identifier in album folder name - continuing post-processing'
                    )
                    verify(rgid, folder)
Exemple #7
0
def forcePostProcess():

    if not headphones.DOWNLOAD_DIR:
        logger.error(
            'No DOWNLOAD_DIR has been set. Set "Music Download Directory:" to your SAB download directory on the settings page.'
        )
        return
    else:
        processing = "nzb"
        processing_next = "torrent"
        while processing != "done":
            if headphones.DOWNLOAD_DIR and processing == "nzb":
                download_dir = headphones.DOWNLOAD_DIR.encode('utf-8')
                if not headphones.DOWNLOAD_TORRENT_DIR:
                    processing_next = "done"
            if headphones.DOWNLOAD_TORRENT_DIR and processing == "torrent":
                download_dir = headphones.DOWNLOAD_TORRENT_DIR.encode('utf-8')
                processing_next = "done"
            if not headphones.DOWNLOAD_DIR and processing == "nzb":
                download_dir = headphones.DOWNLOAD_TORRENT_DIR.encode('utf-8')
                processing_next = "done"

            logger.info(
                'Checking to see if there are any folders to process in download_dir: %s'
                % download_dir)
            # Get a list of folders in the download_dir
            folders = [
                d for d in os.listdir(download_dir)
                if os.path.isdir(os.path.join(download_dir, d))
            ]

            if len(folders):
                logger.info('Found %i folders to process' % len(folders))
                pass
            else:
                logger.info('Found no folders to process in: %s' %
                            download_dir)
                return

            # Parse the folder names to get artist album info
            for folder in folders:

                albumpath = os.path.join(download_dir, folder)
                folder = unicode(folder,
                                 headphones.SYS_ENCODING,
                                 errors='replace')

                logger.info('Processing: %s' % folder)

                try:
                    name, album, year = helpers.extract_data(folder)
                except:
                    logger.info("Couldn't parse " + folder +
                                " into any valid format.")
                    continue
                if name and album and year:

                    myDB = db.DBConnection()
                    release = myDB.action(
                        'SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?',
                        [name, album]).fetchone()
                    if release:
                        logger.info(
                            'Found a match in the database: %s - %s. Verifying to make sure it is the correct album'
                            % (release['ArtistName'], release['AlbumTitle']))
                        verify(release['AlbumID'], albumpath)
                    else:
                        logger.info(
                            'Querying MusicBrainz for the release group id for: %s - %s'
                            % (name, album))
                        from headphones import mb
                        try:
                            rgid = mb.findAlbumID(helpers.latinToAscii(name),
                                                  helpers.latinToAscii(album))
                        except:
                            logger.error(
                                'Can not get release information for this album'
                            )
                            continue
                        if rgid:
                            verify(rgid, albumpath)
                        else:
                            logger.info(
                                'No match found on MusicBrainz for: %s - %s' %
                                (name, album))
            processing = processing_next
Exemple #8
0
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(
            headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(
            headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING))

    logger.info(
        'Checking to see if there are any folders to process in download_dir(s): %s'
        % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info('Found %i folders to process' % len(folders))
    else:
        logger.info(
            'Found no folders to process in: %s' %
            str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))

    # Parse the folder names to get artist album info
    for folder in folders:

        folder_basename = os.path.basename(folder).decode(
            headphones.SYS_ENCODING, 'replace')

        logger.info('Processing: %s' % folder_basename)

        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            logger.info("Couldn't parse " + folder_basename +
                        " into any valid format.")
            continue
        if name and album and year:

            myDB = db.DBConnection()
            release = myDB.action(
                'SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?',
                [name, album]).fetchone()
            if release:
                logger.info(
                    'Found a match in the database: %s - %s. Verifying to make sure it is the correct album'
                    % (release['ArtistName'], release['AlbumTitle']))
                verify(release['AlbumID'], folder)
            else:
                logger.info(
                    'Querying MusicBrainz for the release group id for: %s - %s'
                    % (name, album))
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name),
                                          helpers.latinToAscii(album))
                except:
                    logger.error(
                        'Can not get release information for this album')
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s' %
                                (name, album))
def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None):

    if album_dir:
        folders = [album_dir.encode(headphones.SYS_ENCODING, 'replace')]

    else:
        download_dirs = []
        if dir:
            download_dirs.append(dir.encode(headphones.SYS_ENCODING, 'replace'))
        if headphones.DOWNLOAD_DIR and not dir:
            download_dirs.append(headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING, 'replace'))
        if headphones.DOWNLOAD_TORRENT_DIR and not dir:
            download_dirs.append(headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING, 'replace'))

        # If DOWNLOAD_DIR and DOWNLOAD_TORRENT_DIR are the same, remove the duplicate to prevent us from trying to process the same folder twice.
        download_dirs = list(set(download_dirs))

        logger.info('Checking to see if there are any folders to process in download_dir(s): %s', download_dirs)
        # Get a list of folders in the download_dir
        folders = []

        for download_dir in download_dirs:
            if not os.path.isdir(download_dir):
                logger.warn('Directory %s does not exist. Skipping', download_dir)
                continue
            for folder in os.listdir(download_dir):
                path_to_folder = os.path.join(download_dir, folder)

                if os.path.isdir(path_to_folder):
                    subfolders = helpers.expand_subfolders(path_to_folder)

                    if expand_subfolders and subfolders is not None:
                        folders.extend(subfolders)
                    else:
                        folders.append(path_to_folder)

        if len(folders):
            logger.info('Found %i folders to process', len(folders))
        else:
            logger.info('Found no folders to process in: %s', download_dirs)

    # Parse the folder names to get artist album info
    myDB = db.DBConnection()
    
    for folder in folders:
        folder_basename = os.path.basename(folder).decode(headphones.SYS_ENCODING, 'replace')
        logger.info('Processing: %s', folder_basename)

        # Attempt 1: First try to see if there's a match in the snatched table,
        # then we'll try to parse the foldername.
        # TODO: Iterate through underscores -> spaces, spaces -> dots,
        # underscores -> dots (this might be hit or miss since it assumes all
        # spaces/underscores came from sab replacing values
        logger.debug('Attempting to find album in the snatched table')
        snatched = myDB.action('SELECT AlbumID, Title, Kind, Status from snatched WHERE FolderName LIKE ?', [folder_basename]).fetchone()

        if snatched:
            if headphones.KEEP_TORRENT_FILES and snatched['Kind'] == 'torrent' and snatched['Status'] == 'Processed':
                logger.info('%s is a torrent folder being preserved for seeding and has already been processed. Skipping.', folder_basename)
                continue
            else:
                logger.info('Found a match in the database: %s. Verifying to make sure it is the correct album', snatched['Title'])
                verify(snatched['AlbumID'], folder, snatched['Kind'])
                continue

        # Attempt 2a: parse the folder name into a valid format
        try:
            logger.debug('Attempting to extract name, album and year from folder name')
            name, album, year = helpers.extract_data(folder_basename)
        except Exception as e:
            name = album = year = None

        if name and album:
            release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?', [name, album]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album', release['ArtistName'], release['AlbumTitle'])
                verify(release['AlbumID'], folder)
                continue
            else:
                logger.info('Querying MusicBrainz for the release group id for: %s - %s', name, album)
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error('Can not get release information for this album')
                    rgid = None

                if rgid:
                    verify(rgid, folder)
                    continue
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s', name, album)

        # Attempt 2b: deduce meta data into a valid format
        try:
            logger.debug('Attempting to extract name, album and year from metadata')
            name, album, year = helpers.extract_metadata(folder)
        except Exception as e:
            name = album = year = None

        if name and album:
            release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?', [name, album]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album', release['ArtistName'], release['AlbumTitle'])
                verify(release['AlbumID'], folder)
                continue
            else:
                logger.info('Querying MusicBrainz for the release group id for: %s - %s', name, album)
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error('Can not get release information for this album')
                    rgid = None

                if rgid:
                    verify(rgid, folder)
                    continue
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s', name, album)

        # Attempt 3: strip release group id from filename
        try:
            logger.debug('Attempting to extract release group from folder name')
            possible_rgid = folder_basename[-36:]
            # re pattern match: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
            rgid = uuid.UUID(possible_rgid)
        except:
            logger.info("Couldn't parse '%s' into any valid format. If adding albums from another source, they must be in an 'Artist - Album [Year]' format, or end with the musicbrainz release group id", folder_basename)
            rgid = possible_rgid = None

        if rgid:
            rgid = possible_rgid
            release = myDB.action('SELECT ArtistName, AlbumTitle, AlbumID from albums WHERE AlbumID=?', [rgid]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album', release['ArtistName'], release['AlbumTitle'])
                verify(release['AlbumID'], folder, forced=True)
                continue
            else:
                logger.info('Found a (possibly) valid Musicbrainz identifier in album folder name - continuing post-processing')
                verify(rgid, folder, forced=True)
                continue

        # Attempt 4: Hail mary. Just assume the folder name is the album name if it doesn't have a separator in it
        if '-' not in folder:
            release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE AlbumTitle LIKE ?', [folder]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album', release['ArtistName'], release['AlbumTitle'])
                verify(release['AlbumID'], folder)
                continue
            else:
                logger.info('Querying MusicBrainz for the release group id for: %s', folder)
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(album=helpers.latinToAscii(folder))
                except:
                    logger.error('Can not get release information for this album')
                    rgid = None

                if rgid:
                    verify(rgid, folder)
                    continue
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s', name, album)
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING, "replace"))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING, "replace"))

    # If DOWNLOAD_DIR and DOWNLOAD_TORRENT_DIR are the same, remove the duplicate to prevent us from trying to process the same folder twice.
    download_dirs = list(set(download_dirs))

    logger.info(
        "Checking to see if there are any folders to process in download_dir(s): %s"
        % str(download_dirs).decode(headphones.SYS_ENCODING, "replace")
    )
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info("Found %i folders to process" % len(folders))
    else:
        logger.info(
            "Found no folders to process in: %s" % str(download_dirs).decode(headphones.SYS_ENCODING, "replace")
        )

    # Parse the folder names to get artist album info
    myDB = db.DBConnection()

    for folder in folders:

        folder_basename = os.path.basename(folder).decode(headphones.SYS_ENCODING, "replace")

        logger.info("Processing: %s" % folder_basename)

        # First try to see if there's a match in the snatched table, then we'll try to parse the foldername
        snatched = myDB.action("SELECT AlbumID, Title from snatched WHERE FolderName LIKE ?", [folder_basename])
        if snatched:
            logger.info(
                "Found a match in the database: %s. Verifying to make sure it is the correct album" % snatched["Title"]
            )
            verify(snatched["AlbumID"], folder)
            continue

        # Try to parse the folder name into a valid format
        # TODO: Add metadata lookup
        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            name = None

        if name and album and year:

            release = myDB.action(
                "SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?",
                [name, album],
            ).fetchone()
            if release:
                logger.info(
                    "Found a match in the database: %s - %s. Verifying to make sure it is the correct album"
                    % (release["ArtistName"], release["AlbumTitle"])
                )
                verify(release["AlbumID"], folder)
            else:
                logger.info("Querying MusicBrainz for the release group id for: %s - %s" % (name, album))
                from headphones import mb

                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error("Can not get release information for this album")
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info("No match found on MusicBrainz for: %s - %s" % (name, album))
                    continue

        else:
            try:
                possible_rgid = folder_basename[-36:]
                # re pattern match: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
                rgid = uuid.UUID(possible_rgid)

            except:
                logger.info(
                    "Couldn't parse "
                    + folder_basename
                    + " into any valid format. If adding albums from another source, they must be in an 'Artist - Album [Year]' format, or end with the musicbrainz release group id"
                )
                continue

            if rgid:
                rgid = possible_rgid
                release = myDB.action(
                    "SELECT ArtistName, AlbumTitle, AlbumID from albums WHERE AlbumID=?", [rgid]
                ).fetchone()
                if release:
                    logger.info(
                        "Found a match in the database: %s - %s. Verifying to make sure it is the correct album"
                        % (release["ArtistName"], release["AlbumTitle"])
                    )
                    verify(release["AlbumID"], folder)
                else:
                    logger.info(
                        "Found a (possibly) valid Musicbrainz identifier in album folder name - continuing post-processing"
                    )
                    verify(rgid, folder)
Exemple #11
0
def forcePostProcess():

    download_dirs = []
    if headphones.DOWNLOAD_DIR:
        download_dirs.append(headphones.DOWNLOAD_DIR.encode(headphones.SYS_ENCODING, 'replace'))
    if headphones.DOWNLOAD_TORRENT_DIR:
        download_dirs.append(headphones.DOWNLOAD_TORRENT_DIR.encode(headphones.SYS_ENCODING, 'replace'))
        
    logger.info('Checking to see if there are any folders to process in download_dir(s): %s' % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))
    # Get a list of folders in the download_dir
    folders = []
    for download_dir in download_dirs:
        for folder in os.listdir(download_dir):
            path_to_folder = os.path.join(download_dir, folder)
            if os.path.isdir(path_to_folder):
                folders.append(path_to_folder)

    if len(folders):
        logger.info('Found %i folders to process' % len(folders))
    else:
        logger.info('Found no folders to process in: %s' % str(download_dirs).decode(headphones.SYS_ENCODING, 'replace'))

    # Parse the folder names to get artist album info
    myDB = db.DBConnection()
    
    for folder in folders:

        folder_basename = os.path.basename(folder).decode(headphones.SYS_ENCODING, 'replace')

        logger.info('Processing: %s' % folder_basename)
        
        try:
            name, album, year = helpers.extract_data(folder_basename)
        except:
            name = None

        if name and album and year:
            
            release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE ArtistName LIKE ? and AlbumTitle LIKE ?', [name, album]).fetchone()
            if release:
                logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle']))
                verify(release['AlbumID'], folder)
            else:
                logger.info('Querying MusicBrainz for the release group id for: %s - %s' % (name, album))
                from headphones import mb
                try:
                    rgid = mb.findAlbumID(helpers.latinToAscii(name), helpers.latinToAscii(album))
                except:
                    logger.error('Can not get release information for this album')
                    continue
                if rgid:
                    verify(rgid, folder)
                else:
                    logger.info('No match found on MusicBrainz for: %s - %s' % (name, album))
                    continue
                    
        else:
            try:
                possible_rgid = folder_basename[-36:]
                # re pattern match: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} 
                rgid = uuid.UUID(possible_rgid)
                    
            except:
                logger.info("Couldn't parse " + folder_basename + " into any valid format. If adding albums from another source, they must be in an 'Artist - Album [Year]' format, or end with the musicbrainz release group id")
                continue
            
            
            if rgid:
                rgid = possible_rgid
                release = myDB.action('SELECT ArtistName, AlbumTitle, AlbumID from albums WHERE AlbumID=?', [rgid]).fetchone()
                if release:
                    logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album' % (release['ArtistName'], release['AlbumTitle']))
                    verify(release['AlbumID'], folder)
                else:
                    logger.info('Found a (possibly) valid Musicbrainz identifier in album folder name - continuing post-processing')
                    verify(rgid, folder)