jpyamamoto / Issuu-PDF-Downloader

Program on python which downloads Issuu files as PDF
36 stars 15 forks source link

dont overwrite existing files #8

Open estatistics opened 2 years ago

estatistics commented 2 years ago

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

estatistics commented 2 years ago

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
jpyamamoto commented 2 years ago

Feel free to open a PR with your changes

estatistics commented 2 years ago

PR? What is PR?

jpyamamoto commented 2 years ago

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.