Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writting in the wrong folder #14

Closed
Deses opened this issue Aug 10, 2023 · 2 comments
Closed

Writting in the wrong folder #14

Deses opened this issue Aug 10, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@Deses
Copy link

Deses commented Aug 10, 2023

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 '/'

@Deses Deses added the bug Something isn't working label Aug 10, 2023
@Ovear
Copy link
Owner

Ovear commented May 1, 2024

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 Ovear closed this as completed in c42a657 May 1, 2024
@Ovear
Copy link
Owner

Ovear commented May 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants