mirror of
				https://github.com/KevinMidboe/spotify-downloader.git
				synced 2025-10-29 18:00:15 +00:00 
			
		
		
		
	Overwrite track file with unique tracks
This commit is contained in:
		@@ -158,6 +158,23 @@ def get_splits(url):
 | 
				
			|||||||
    return splits
 | 
					    return splits
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_unique_tracks(text_file):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Returns a list of unique tracks given a path to a
 | 
				
			||||||
 | 
					    file containing tracks.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with open(text_file, 'r') as listed:
 | 
				
			||||||
 | 
					        # Read tracks into a list and remove any duplicates
 | 
				
			||||||
 | 
					        lines = listed.read().splitlines()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Remove blank and strip whitespaces from lines (if any)
 | 
				
			||||||
 | 
					    lines = [line.strip() for line in lines if line.strip()]
 | 
				
			||||||
 | 
					    lines = remove_duplicates(lines)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return lines
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# a hacky way to user's localized music directory
 | 
					# a hacky way to user's localized music directory
 | 
				
			||||||
# (thanks @linusg, issue #203)
 | 
					# (thanks @linusg, issue #203)
 | 
				
			||||||
def get_music_dir():
 | 
					def get_music_dir():
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -65,15 +65,13 @@ def check_exists(music_file, raw_song, meta_tags):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def download_list(text_file):
 | 
					def download_list(text_file):
 | 
				
			||||||
    """ Download all songs from the list. """
 | 
					    """ Download all songs from the list. """
 | 
				
			||||||
    with open(text_file, 'r') as listed:
 | 
					
 | 
				
			||||||
        # read tracks into a list and remove any duplicates
 | 
					    log.info('Checking and removing any duplicate tracks')
 | 
				
			||||||
        lines = listed.read().splitlines()
 | 
					    lines = internals.get_unique_tracks(text_file)
 | 
				
			||||||
        lines = internals.remove_duplicates(lines)
 | 
					
 | 
				
			||||||
    # ignore blank lines in text_file (if any)
 | 
					    # override file with unique tracks
 | 
				
			||||||
    try:
 | 
					    with open(text_file, 'w') as listed:
 | 
				
			||||||
        lines.remove('')
 | 
					        listed.write('\n'.join(lines))
 | 
				
			||||||
    except ValueError:
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    log.info(u'Preparing to download {} songs'.format(len(lines)))
 | 
					    log.info(u'Preparing to download {} songs'.format(len(lines)))
 | 
				
			||||||
    downloaded_songs = []
 | 
					    downloaded_songs = []
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -80,3 +80,39 @@ class TestGetSeconds:
 | 
				
			|||||||
            internals.get_sec('10*05')
 | 
					            internals.get_sec('10*05')
 | 
				
			||||||
        with pytest.raises(ValueError):
 | 
					        with pytest.raises(ValueError):
 | 
				
			||||||
            internals.get_sec('02,28,46')
 | 
					            internals.get_sec('02,28,46')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					duplicate_tracks_test_table = [
 | 
				
			||||||
 | 
					    (('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
 | 
				
			||||||
 | 
					      'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ'),
 | 
				
			||||||
 | 
					     ('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    (('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
 | 
				
			||||||
 | 
					      '',
 | 
				
			||||||
 | 
					      'https://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD'),
 | 
				
			||||||
 | 
					     ('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
 | 
				
			||||||
 | 
					      'https://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD')),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    (('ncs fade',
 | 
				
			||||||
 | 
					      'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
 | 
				
			||||||
 | 
					      '',
 | 
				
			||||||
 | 
					      'ncs fade'),
 | 
				
			||||||
 | 
					     ('ncs fade',
 | 
				
			||||||
 | 
					      'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ')),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    (('ncs spectre ',
 | 
				
			||||||
 | 
					      '  https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
 | 
				
			||||||
 | 
					      ''),
 | 
				
			||||||
 | 
					     ('ncs spectre',
 | 
				
			||||||
 | 
					      'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ'))
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.parametrize("duplicates, expected", duplicate_tracks_test_table)
 | 
				
			||||||
 | 
					def test_get_unique_tracks(tmpdir, duplicates, expected):
 | 
				
			||||||
 | 
					    file_path = os.path.join(str(tmpdir), 'test_duplicates.txt')
 | 
				
			||||||
 | 
					    with open(file_path, 'w') as tin:
 | 
				
			||||||
 | 
					        tin.write('\n'.join(duplicates))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    unique_tracks = internals.get_unique_tracks(file_path)
 | 
				
			||||||
 | 
					    assert tuple(unique_tracks) == expected
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user