SenZmaKi / Senpwai

A desktop app for tracking and batch downloading anime
https://youtu.be/dQw4w9WgXcQ
GNU General Public License v3.0
178 stars 18 forks source link

IndexError in DownloadEpisodes function #1

Closed Noordicsea closed 1 year ago

Noordicsea commented 1 year ago

I encountered an IndexError while using the senpwai.py script to download anime episodes. The error occurred in the DownloadEpisodes function on line 1008. The code attempts to access the last element of the list "file_paths", but the list appears to be empty, resulting in an IndexError.

Expected behavior: The script should download the anime episodes to the specified folder without any errors.

Actual behavior: The script encounters an IndexError in the DownloadEpisodes function, indicating that the list "file_paths" is empty.

Suggested solution: The issue may be caused by the download folder path being incorrect or empty. It would be helpful to add some error checking to ensure that the download folder path is valid and contains files before attempting to access the last element of the list "file_paths".

Environment: Operating System: Windows 10 Python Version: 3.9.7 senpwai.py Version: 1.0.0

Noordicsea commented 1 year ago

Here's an example of how to check if a list is empty before attempting to access its last element with an index of -1:

file_paths = list(pathlib.Path(download_folder_path).glob("*")) if not file_paths: raise ValueError("No files found in download folder") elif len(file_paths) < 2: raise ValueError("At least two files required in download folder") else: downloaded_file = file_paths[-1]

SenZmaKi commented 1 year ago

Thank you for pointing this out to me

That section of code only runs when at least one file has been downloaded since it comes right after I call StillDownloading in a while loop on line 997 so my assumption was there's no need to check if the folder/directory is empty cause that block of code is only executed after StillDownloading returns False meaning the file has been downloaded.

Can you clarify to me on whether the file was downloaded or not, cause if it failed to download and the program still went on to attempt to rename the file which does not exist then that means there's a problem in StillDownloading

Noordicsea commented 1 year ago

The download was not successful. I have recorded a video demonstrating the issue and how I encountered it: https://youtu.be/ZO6GOYMm2TY.

A few things to take note of: When attempting to download the series, I experienced some hiccups after requesting it to detect missing episodes. I abandoned the series download and tried a movie instead, which quickly detected the missing files since it was essentially just one episode. Therefore, the lag may be attributed to the number of episodes in a series. After getting past that point, the program attempted to verify my download location, but then presented a UI window after it was set.

The video also displays the specific error message.

Additionally, the relevant issue occurs around the 3:26 mark in the video. I had specified the folder "anime", but the program prompted me to save it again. Interestingly, when attempting to save the movie in the File UI, the error appeared in the console.

SenZmaKi commented 1 year ago

Ahhh I see now this a bug a friend of mine also experienced, what's happening is, because the program uses a headless browser for some reason on some systems before the download begins Chrome opens the save download as page, where it asks you to save it in a specific location, even though I explicitly tell it not to when I set up the headless browser since the save location is the one the user chooses. My previous attempts at fixing it failed cause I had failed to replicate the bug on my end so I just assumed it won't happen often. Thank you for bringing this to light I'll try my best to fix it. Gambarimasu.

As for the case of the long anime that usually takes a while (about 5 minutes max) cause the anime has 148 episodes and I set up the program such that it gets the links to the files before it even starts downloading to avoid any weird bugs such that it knows first hand what it's dealing with before anything happens.

What version of Chrome do you have cause I want to attempt to replicate the bug?

SenZmaKi commented 1 year ago

Ok so I've managed to somewhat replicate the bug, it seems the folder you're giving it is a folder that requires administrator access so the program fails to place the downloads in that folder hence Chrome opens the save downloads page, could you try and give it folder that doesn't require administrator access like instead of C:\Users\gtnoo use C:\Users\PC\Anime cause the former requires admin access.

Do this experiment open Chrome, go to settings, set your download folder to C:\Users\gtnoo then set ask where to download each file to false, then try downloading sth, Chrome will pop up the save file dialog box cause it doesn't have access to that folder, now try the exact same experiment but run Chrome as administrator, it won't pop up the dialog box cause now it has access to that folder and finally do the same experiment run Chrome in normal mode but set the download folder as C:\Users\PC\Anime, again Chrome won't open the dialog box cause it has access to this folder.

There are two possible fixes either I make it such that the program runs as administrator which is the equivalent of nuking a planet just to kill an ant or I just tell the user to avoid folders that require admin access, in my opinion the latter seems like the smarter choice

SenZmaKi commented 1 year ago

I've implemented the second fix and updated the repo and the app, tell me if you ran into any other issues