def tools_outsideMp3sToSpotifyPlaylist(dir=None):
    if not dir:
        clipboardText = getClipboardText()
        defaultDir = clipboardText if (files.exists(clipboardText)) else getDefaultDirectorySpotifyToFilenames()
        dir = getInputStringGui('choose input directory:', defaultDir)
        if not dir:
            return
    
    # first, associate all files with Spotify
    for fullpathdir, shortdir in files.recursedirs(dir):
        filepaths = [filepath for (filepath, fileshort) in files.listfiles(fullpathdir) if getFieldForFile(filepath, False)]
        for filepath in filepaths:
            if '__MARKAS' not in filepath:
                assertTrue(False, 'cannot continue, file does not have __MARKAS. ' + filepath)
            if '__MARKAS__16.' in filepath:
                alert('for file ' + filepath + ' just delete instead of using __MARKAS__16.')
        
        tags = [CoordMusicAudioMetadata(filepath) for filepath in filepaths]
        countFilesWithMissingNumber = len(filter(lambda tag: tag.get_or_default('tracknumber', None), tags))
        parsed = []
        for tag in tags:
            if not tag.get_or_default('tracknumber', None):
                num = getInputInt('%s\nenter tracknum for "%s" (%d files missing tracknum)' %
                    (fullpathdir, tag.short, countFilesWithMissingNumber))
                tag.set('tracknumber', num)
                tag.save()
            parsed.append(Bucket(short=tag.short, tracknumber=int(tag.get('tracknumber')),
                artist=tag.get_or_default('artist', None), title=tag.get_or_default('title', None),
                album=shortdir, discnumber=int(tag.get_or_default('discnumber', 1))))
        
        alreadyAll = all(('spotify:' in tag.getLink() for tag in tags))
        recurring_linkspotify.linkspotify(
            len(tags) > 0 and not alreadyAll, fullpathdir, tags, parsed, True, getSpotifyGeographicMarketName())
    
    # check that all files have a spotify link
    for fullpath, short in files.recursefiles(dir):
        if getFieldForFile(filepath, False):
            assertTrue('spotify:' in CoordMusicAudioMetadata(filepath).getLink())
    
    # add uris to list
    addToPlaylist = []
    for fullpath, short in files.recursefiles(dir):
        if getFieldForFile(fullpath, False) and '__MARKAS__24.' not in short:
            link = CoordMusicAudioMetadata(fullpath).getLink()
            if 'spotify:track:' in link:
                addToPlaylist.append(link)
    
    # add uris to Spotify playlist
    if len(addToPlaylist):
        playlistId = tools_getPlaylistId()
        trace('adding %d tracks' % len(addToPlaylist))
        for batch in takeBatch(addToPlaylist, 10):
            spotipyconn().user_playlist_add_tracks(getSpotifyUsername(), playlistId, [item for item in batch])
            time.sleep(0.2)
def checkFilenamesMain(fullpathdir, dirsplit, tags, helpers):
    if not len(tags):
        return

    shorts = [tag.short for tag in tags]
    checkDeleteUrlsInTheWayOfM4as(fullpathdir, shorts)
    checkForLowBitrates(fullpathdir, tags, False)
    checkFilenameIrregularities(fullpathdir, shorts)
    checkUrlContents(fullpathdir, shorts)
    helpers.removeRemasteredString.check(fullpathdir, shorts)
    
    userAllowsBulkSet = dict()
    parsedNames = [parseAFilename(short) for short in shorts]
    [fillFieldsFromContext(parsed, fullpathdir, dirsplit, helpers) for parsed in parsedNames]
    [checkTagAndNameConsistency(fullpathdir, dirsplit, tag, parsed, userAllowsBulkSet) for tag, parsed in zip(tags, parsedNames)]
    checkStyleConsistency(fullpathdir, parsedNames)
    checkDuplicatedTrackNumbers(fullpathdir, parsedNames)
    seenTracknumber, seenWithoutSpotify = checkRequiredFieldsSet(fullpathdir, dirsplit, tags, parsedNames)
    recurring_linkspotify.linkspotify(seenWithoutSpotify, fullpathdir, tags, parsedNames, seenTracknumber, helpers.market)
    helpers.music_to_url.go(fullpathdir, tags, parsedNames)
    checkForLowBitrates(fullpathdir, tags, True)
def testsLinkSpotifyInteractive():
    from recurring_linkspotify import linkspotify, launchSpotifyUri
    if not getInputBool('Run interactive LinkSpotify test (requires Spotify connection)?'):
        return
    
    # testing files outside of an album
    trace('testing files outside of an album')
    createOrClearDirectory(tmpdir)
    files.makedirs(tmpdir + '/classic rock/collection')
    files.copy(dirtestmedia + '/test_02_52.m4a',
        tmpdir + '/classic rock/collection/Queen - You\'re My Best Friend.m4a', False)
    parsedNames = [Bucket(short='Queen - You\'re My Best Friend.m4a',
        artist='Queen', title='You\'re My Best Friend', discnumber=1, tracknumber=None)]
    tags = [CoordMusicAudioMetadata(tmpdir + '/classic rock/collection/' + parsed.short) for parsed in parsedNames]
    for tag, parsed in zip(tags, parsedNames):
        tag.short = parsed.short
        
    # add a url, which should be ignored
    writeUrlFile(tmpdir + '/classic rock/collection/artist - ignored.url', 'spotify:track:7o4i6eiHjZqbASnSy3pKAq')
    assertTrue(files.exists(tmpdir + '/classic rock/collection/artist - ignored.url'))
    linkspotify(True, tmpdir + '/classic rock/collection', tags, parsedNames, False, 'US')
    if getInputBool('spotifylink was set to ' + str(tags[0].getLink()) + ' open?'):
        launchSpotifyUri(tags[0].getLink())
    
    # testing files inside of an album with one disc
    trace('testing files inside of an album with one disc')
    createOrClearDirectory(tmpdir)
    files.makedirs(tmpdir + '/classic rock/Queen/1975, A Night At The Opera')
    parsedNames = []
    files.copy(dirtestmedia + '/test_02_52.m4a',
        tmpdir + '/classic rock/Queen/1975, A Night At The Opera/04 You\'re My Best Friend.m4a', False)
    parsedNames.append(Bucket(short='04 You\'re My Best Friend.m4a',
        artist='Queen', album='A Night At The Opera', title='You\'re My Best Friend', discnumber=1, tracknumber=4))
    files.copy(dirtestmedia + '/test_03_31.m4a',
        tmpdir + '/classic rock/Queen/1975, A Night At The Opera/05 \'39.m4a', False)
    parsedNames.append(Bucket(short='05 \'39.m4a',
        artist='Queen', title='\'39', discnumber=1, tracknumber=5))
        
    tags = [CoordMusicAudioMetadata(tmpdir + '/classic rock/Queen/1975, A Night At The Opera/' + parsed.short) for parsed in parsedNames]
    for tag, parsed in zip(tags, parsedNames):
        tag.short = parsed.short
    
    # add a url, which should be ignored
    writeUrlFile(tmpdir + '/classic rock/Queen/1975, A Night At The Opera/01 Ignored.url', 'spotify:track:7o4i6eiHjZqbASnSy3pKAq')
    parsedNames.append(Bucket(short='01 Ignored.url'))
    tags.append(Bucket(short='01 Ignored.url'))
    linkspotify(True, tmpdir + '/classic rock/Queen/1975, A Night At The Opera', tags, parsedNames, True, 'US')
    if getInputBool('first spotifylink was set to ' + str(tags[0].getLink()) + ' open?'):
        launchSpotifyUri(tags[0].getLink())
    
    # testing files inside of an album with several discs
    trace('testing files inside of an album with several discs')
    trace('"I Want to Break Free" is intentionally 04_09 instead of 02_05, to test misnumbering')
    trace('"Barcelona" is intentionally too short, to test checking mismatched duration')
    createOrClearDirectory(tmpdir)
    files.makedirs(tmpdir + '/classic rock/Queen/2002, The Platinum Collection')
    parsedNames = []
    
    files.copy(dirtestmedia + '/test_02_52.m4a',
        tmpdir + '/classic rock/Queen/2002, The Platinum Collection/01 06 You\'re My Best Friend.m4a', False)
    parsedNames.append(Bucket(short='01 06 You\'re My Best Friend.m4a',
        artist='Queen', album='The Platinum Collection', title='You\'re My Best Friend', discnumber=1, tracknumber=6))
        
    files.copy(dirtestmedia + '/test_04_19.m4a',
        tmpdir + '/classic rock/Queen/2002, The Platinum Collection/04 09 I Want to Break Free.m4a', False)
    parsedNames.append(Bucket(short='04 09 I Want to Break Free.m4a',
        artist='Queen', title='I Want to Break Free', discnumber=4, tracknumber=9))
        
    files.copy(dirtestmedia + '/test_03_31.m4a',
        tmpdir + '/classic rock/Queen/2002, The Platinum Collection/03 03 Barcelona.m4a', False)
    parsedNames.append(Bucket(short='03 03 Barcelona.m4a',
        artist='Queen', title='Barcelona', discnumber=3, tracknumber=3))
        
    tags = [CoordMusicAudioMetadata(tmpdir + '/classic rock/Queen/2002, The Platinum Collection/' + parsed.short) for parsed in parsedNames]
    for tag, parsed in zip(tags, parsedNames):
        tag.short = parsed.short
        
    # add a url, which should be ignored
    writeUrlFile(tmpdir + '/classic rock/Queen/2002, The Platinum Collection/01 01 Ignored.url', 'spotify:track:7o4i6eiHjZqbASnSy3pKAq')
    parsedNames.append(Bucket(short='01 01 Ignored.url'))
    tags.append(Bucket(short='01 01 Ignored.url'))
    linkspotify(True, tmpdir + '/classic rock/Queen/2002, The Platinum Collection', tags, parsedNames, True, 'US')
    if getInputBool('second spotifylink was set to ' + str(tags[1].getLink()) + ' open?'):
        launchSpotifyUri(tags[1].getLink())