Output dir choosable

This commit is contained in:
2025-01-01 18:04:02 -08:00
parent c0c7475b36
commit ef34f48409
2 changed files with 18 additions and 10 deletions
+5 -3
View File
@@ -19,10 +19,12 @@ How to use
To run with command line args, run:
```
./downloader.py <comic_id>
./downloader.py <comic_id> [dir]
```
If you would like to put comics in a certain directory, provide the `dir`
argument.
### Edit the code
The start of the file contains the variable `comic_id`. Look at the comment
above it.
The start of the file contains the variables `comic_id` and `directory`. Look
at the comment above it.
+13 -7
View File
@@ -34,12 +34,16 @@ comic_id = False
### If comic_id is false, get id from command line. If command line has no args,
### exit with error.
if comic_id == False:
if len(argv) == 2:
if len(argv) > 1:
comic_id = argv[1]
else:
print("Error: Comic ID not provided via command line. Set ID in source or provide ID.")
exit(1)
### If second arg provided, put comics in there (otherwise use `comic_id`)
directory = (lambda: argv[2] if len(argv) > 2 else comic_id)()
### Check that comic actually exists (bonus: gocomics does the input
### sanitization for you; as long as it exists, you can assume the input is
### sanitized)
@@ -85,14 +89,16 @@ print(f"Start date: {first_comic}")
### labled one
resuming = False
if not exists(comic_id):
mkdir(comic_id)
if not exists(directory):
mkdir(directory)
else: ## Dir exists
## Make sure there are files
## This means "Get the list of files in the `comic_id` folder and sort them.
## Set `listing` to them."
## This means "Set listing to the sorted list of files in the img dir, and
## if it's not empty, get the file marked most recent and start from that date"
## Potential bug arises when adding dates that don't exist manually or
## putting images into a dir that has non-comic files
if (listing := sorted(
[x for x in listdir(comic_id) if isfile(f"{comic_id}/{x}")])) != []:
[x for x in listdir(directory) if isfile(f"{directory}/{x}")])) != []:
## Start from most recent lable (remove .gif and replace - with /)
current_comic = listing[-1][0:-4].replace('-','/')
@@ -119,7 +125,7 @@ while True: # Just going to use `break` to exit
## mode. GoComics hosts images as gif, so download it as a gif. Also replace
## '/' with '-'. The := operator both sets a var and evaluates the
## condition.
if not exists(file := f"{comic_id}/{current_comic.replace('/','-')}.gif"):
if not exists(file := f"{directory}/{current_comic.replace('/','-')}.gif"):
## Get comic data. If not success, try again.
status = False
while status != 200: