diff --git a/requirements.txt b/requirements.txt index 38c2083..ee07df1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -yaml +PyYaml praw -optparse zipfile \ No newline at end of file diff --git a/riddle.py b/riddle.py index 87adce3..df67e5f 100644 --- a/riddle.py +++ b/riddle.py @@ -169,6 +169,22 @@ def download_images(images: list, dl_dir: str): (realcount, imgcount, dl_dir, preexist)) +def filter_zip_files(images: list, zip_fname: str): + """ + Removes the images that already exist in the zip-file + :param images: + :param zip_fname: + :return: + """ + if os.path.isfile(zip_fname): + zfile = zipfile.ZipFile(zip_fname, 'r') + zfnames = [f.filename for f in zfile.infolist()] + print('[~] Removing entries already in zip-file') + return [img for img in images if img.split('/')[-1] not in zfnames] + else: + return images + + def compress_folder(folder: str, zip_fname: str): """ Zips the contents of a folder to the destination zipfile name. @@ -221,9 +237,10 @@ def main(): dldest = options.output # uses the -o output destination images = get_images(client, subreddit, limit=options.count, nsfw=options.nsfw) - if options.zip: # download to zip instead - download_images(images, '.cache') # download to cache - compress_folder('.cache', dldest + '.zip') # add to zip + if options.zip: # downloads to a cache-folder first before compressing it to zip + images = filter_zip_files(images, dldest+'.zip') + download_images(images, '.cache') + compress_folder('.cache', dldest+'.zip') shutil.rmtree('.cache') else: download_images(images, dldest) diff --git a/test.zip b/test.zip new file mode 100644 index 0000000..3458a09 Binary files /dev/null and b/test.zip differ