Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dont overwrite existing files #8

Open
estatistics opened this issue Jun 10, 2022 · 4 comments
Open

dont overwrite existing files #8

estatistics opened this issue Jun 10, 2022 · 4 comments

Comments

@estatistics
Copy link

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
Copy link
Author

estatistics commented Jun 10, 2022

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
Copy link
Owner

Feel free to open a PR with your changes

@estatistics
Copy link
Author

PR? What is PR?

@jpyamamoto
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants