Ovear / kemono-dl

A simple kemono.party downloader using python.
109 stars 11 forks source link

Writting in the wrong folder #14

Closed Deses closed 2 months ago

Deses commented 11 months ago

I have a python script that calls kemono-dl. This script has all its paths defined with __file__, (but it also fails using the tilde ~, as in '~/user/kemono/downloads') which kemono-dl takes correctly but processes incorrectly, because it strips the first / and writes the files to the wrong location.

My simplified script:

script_dir = os.path.dirname(os.path.abspath(__file__)) <-- This is used to work in the script's folder regardless of where it is called.

filenamePattern = '\"[{id}] {title} - {index}.{ext}\"'
downloadFolder = os.path.join(script_dir, 'downloads/')
archiveFolder = os.path.join(script_dir, 'archives/')

for artistURL, data in artists.items(): <- 'data is a tuple of 2 elements: data[0] being the name of the folder.'
    outputFolder = os.path.join(downloadFolder, data[0]) <- 'Here, outputFolder == /home/user/kemono/downloads''

    url, cookie, artist = get_service(artistURL, data)

    os.system(f'python ~/git/kemono-dl/kemono-dl.py --verbose --archive {archiveFolder}archive-{artist}.txt '
              f'--cookies {cookie} --quiet --filename-pattern {filenamePattern} '
              f'--dirname-pattern {outputFolder} --links {url}')

--archive are succesfully writen to and --cookies are succesfully read from the correct path.

The fault seems it's here:

def compile_post_path(post_variables, template, ascii):
    drive, tail = os.path.splitdrive(template)
    tail = tail[1:] if tail[0] in {'/','\\'} else tail
    tail_split = re.split(r'\\|/', tail)        
    cleaned_path = drive + os.path.sep if drive else ''        <-------------------- HERE
    for folder in tail_split:
        print("----folder-----" + folder)
        if ascii:
            cleaned_path = os.path.join(cleaned_path, restrict_ascii(clean_folder_name(folder.format(**post_variables))))
        else:
            cleaned_path = os.path.join(cleaned_path, clean_folder_name(folder.format(**post_variables)))
    print("----cleaned_path-----" + cleaned_path)
    return cleaned_path

When splitting the path, it turns /home/user/kemono/downloads into home/user/kemono/downloads, this creating a whole tree of folders and writing to the wrong path.

I understand this is made to be compatible with both Windows and Linux, but I don't think this behavior is correct.

Changing the line with cleaned_path = drive + os.path.sep if drive else '' and adding a / to the else should fix the issue. cleaned_path = drive + os.path.sep if drive else '/'

Ovear commented 2 months ago

Hi,

Hmm, this should be platform related code, change that line may break in Windows. Let me see if there are any other workarounds.

Ovear commented 2 months ago

Thanks for your perfect investigation, it is caused by that line of code. Try to fix with not well-tested code, hope it won't break anything :>

Please check if works on your side.