jpyamamoto/Issuu-PDF-Downloader

dont overwrite existing files

Opened this issue · 4 comments

I have downloaded 1 to 50 jpg... and a network error occured.
Then all files become overwritten

u may update ypur code in order dont overwrite existing files

solved

import urllib
import requests
import core.pdf as pdf
import os.path

def exists(path):
    # Check that image exists
    r = requests.head(path)
    return r.status_code == requests.codes.ok


def downloader(url):
    print('Started download process...')
    # List to create the PDF
    files = []
    formatter = url.replace('page_1.jpg', '')   
    changer = 1

    while os.path.exists(str(changer) + '.jpg'):
       changer += 1
       print(str(changer) + '.jpg')
    else:
        while True:
            changer2 = formatter + 'page_' + str(changer) + '.jpg'
            filename = str(changer) + '.jpg'
            if exists(changer2):
                # Download images
                opener = open(str(changer) + '.jpg', 'wb')
                opener.write(urllib.request.urlopen(changer2).read())
                # Save images
                opener.close()
                print('Correctly saved file ' + filename + ' From URI: ' + changer2)
                # Add filename to list, so we make the pdf later
                files.append(filename)
                # Go for the next one
                changer += 1
            else:
            # No more images
                break

    print('Completed download process...')
    # Time to create the pdf
    return files


Feel free to open a PR with your changes

PR? What is PR?

A Pull Request is a way for everyone to contribute to projects hosted on GitHub. This way, you make some changes to the code, and if the maintainers of the project accept, they are integrated in the source code.

Here's a guide on how to create a Pull Request.