def downloadAlbum(self, images):
        folderName = GLOBAL.config['filename'].format(**self.post)
        folderDir = self.directory / folderName

        imagesLenght = images["count"]
        howManyDownloaded = 0
        duplicates = 0

        try:
            if not os.path.exists(folderDir):
                os.makedirs(folderDir)
        except FileNotFoundError:
            folderDir = self.directory / self.post['POSTID']
            os.makedirs(folderDir)

        print(folderName)

        for i in range(imagesLenght):

            extension = self.validateExtension(images["images"][i]["ext"])

            imageURL = self.IMGUR_IMAGE_DOMAIN + images["images"][i][
                "hash"] + extension

            filename = "_".join([
                str(i + 1),
                nameCorrector(images["images"][i]['title']),
                images["images"][i]['hash']
            ]) + extension
            shortFilename = str(i + 1) + "_" + images["images"][i]['hash']

            print("\n  ({}/{})".format(i + 1, imagesLenght))

            try:
                getFile(filename, shortFilename, folderDir, imageURL, indent=2)
                howManyDownloaded += 1
                print()

            except FileAlreadyExistsError:
                print("  The file already exists" + " " * 10, end="\n\n")
                duplicates += 1

            except TypeInSkip:
                print("  Skipping...")
                howManyDownloaded += 1

            except Exception as exception:
                print("\n  Could not get the file")
                print(
                    "  " +
                    "{class_name}: {info}\nSee CONSOLE_LOG.txt for more information"
                    .format(class_name=exception.__class__.__name__,
                            info=str(exception)) + "\n")
                print(GLOBAL.log_stream.getvalue(), noPrint=True)

        if duplicates == imagesLenght:
            raise FileAlreadyExistsError
        elif howManyDownloaded + duplicates < imagesLenght:
            raise AlbumNotDownloadedCompletely(
                "Album Not Downloaded Completely")
Beispiel #2
0
    def downloadAlbum(self, images, count):
        folderName = GLOBAL.config['filename'].format(**self.post)
        folderDir = self.directory / folderName

        howManyDownloaded = 0
        duplicates = 0

        try:
            if not os.path.exists(folderDir):
                os.makedirs(folderDir)
        except FileNotFoundError:
            folderDir = self.directory / self.post['POSTID']
            os.makedirs(folderDir)

        print(folderName)

        for i in range(count):
            path = urllib.parse.urlparse(images[i]['url']).path
            extension = os.path.splitext(path)[1]

            filename = "_".join([
                str(i + 1), images[i]['id']
            ]) + extension
            shortFilename = str(i + 1) + "_" + images[i]['id']

            print("\n  ({}/{})".format(i + 1, count))

            try:
                getFile(filename, shortFilename, folderDir,
                        images[i]['url'], indent=2)
                howManyDownloaded += 1
                print()

            except FileAlreadyExistsError:
                print("  The file already exists" + " " * 10, end="\n\n")
                duplicates += 1

            except TypeInSkip:
                print("  Skipping...")
                howManyDownloaded += 1

            except Exception as exception:
                print("\n  Could not get the file")
                print(
                    "  " +
                    "{class_name}: {info}\nSee CONSOLE_LOG.txt for more information".format(
                        class_name=exception.__class__.__name__,
                        info=str(exception)) +
                    "\n")
                print(GLOBAL.log_stream.getvalue(), noPrint=True)

        if duplicates == count:
            raise FileAlreadyExistsError
        if howManyDownloaded + duplicates < count:
            raise AlbumNotDownloadedCompletely(
                "Album Not Downloaded Completely"
            )
    def __init__(self, directory, post):
        try:
            IMAGES = self.getLinks(post['postURL'])
        except urllib.error.HTTPError:
            raise NotADownloadableLinkError("Not a downloadable link")

        imagesLenght = len(IMAGES)
        howManyDownloaded = imagesLenght
        duplicates = 0

        if imagesLenght == 1:

            extension = getExtension(IMAGES[0])

            title = nameCorrector(post['postTitle'])
            print(post["postSubmitter"] + "_" + title + "_" + post['postId'] +
                  extension)

            fileDir = directory / (post["postSubmitter"] + "_" + title + "_" +
                                   post['postId'] + extension)
            tempDir = directory / (post["postSubmitter"] + "_" + title + "_" +
                                   post['postId'] + ".tmp")

            imageURL = "https:" + IMAGES[0]

            try:
                getFile(fileDir, tempDir, imageURL)
            except FileNameTooLong:
                fileDir = directory / (post['postId'] + extension)
                tempDir = directory / (post['postId'] + '.tmp')
                getFile(fileDir, tempDir, imageURL)

        else:
            title = nameCorrector(post['postTitle'])
            print(post["postSubmitter"] + "_" + title + "_" + post['postId'],
                  end="\n\n")

            folderDir = directory / (post["postSubmitter"] + "_" + title +
                                     "_" + post['postId'])

            try:
                if not os.path.exists(folderDir):
                    os.makedirs(folderDir)
            except FileNotFoundError:
                folderDir = directory / post['postId']
                os.makedirs(folderDir)

            for i in range(imagesLenght):

                extension = getExtension(IMAGES[i])

                fileName = str(i + 1)
                imageURL = "https:" + IMAGES[i]

                fileDir = folderDir / (fileName + extension)
                tempDir = folderDir / (fileName + ".tmp")

                print("  ({}/{})".format(i + 1, imagesLenght))
                print("  {}".format(fileName + extension))

                try:
                    getFile(fileDir, tempDir, imageURL, indent=2)
                    print()
                except FileAlreadyExistsError:
                    print("  The file already exists" + " " * 10, end="\n\n")
                    duplicates += 1
                    howManyDownloaded -= 1

                except Exception as exception:
                    # raise exception
                    print("\n  Could not get the file")
                    print("  " + "{class_name}: {info}".format(
                        class_name=exception.__class__.__name__,
                        info=str(exception)) + "\n")
                    exceptionType = exception
                    howManyDownloaded -= 1

            if duplicates == imagesLenght:
                raise FileAlreadyExistsError
            elif howManyDownloaded + duplicates < imagesLenght:
                raise AlbumNotDownloadedCompletely(
                    "Album Not Downloaded Completely")
    def __init__(self, directory, post):
        self.imgurClient = self.initImgur()

        imgurID = self.getId(post['postURL'])
        content = self.getLink(imgurID)

        if not os.path.exists(directory): os.makedirs(directory)

        if content['type'] == 'image':

            try:
                post['mediaURL'] = content['object'].mp4
            except AttributeError:
                post['mediaURL'] = content['object'].link

            post['postExt'] = getExtension(post['mediaURL'])

            title = nameCorrector(post['postTitle'])
            print(post["postSubmitter"] + "_" + title + "_" + post['postId'] +
                  post['postExt'])

            fileDir = directory / (post["postSubmitter"] + "_" + title + "_" +
                                   post['postId'] + post['postExt'])

            tempDir = directory / (post["postSubmitter"] + "_" + title + "_" +
                                   post['postId'] + ".tmp")

            try:
                getFile(fileDir, tempDir, post['mediaURL'])
            except FileNameTooLong:
                fileDir = directory / post['postId'] + post['postExt']
                tempDir = directory / post['postId'] + '.tmp'
                getFile(fileDir, tempDir, post['mediaURL'])

        elif content['type'] == 'album':
            exceptionType = ""
            images = content['object'].images
            imagesLenght = len(images)
            howManyDownloaded = imagesLenght
            duplicates = 0

            title = nameCorrector(post['postTitle'])
            print(post["postSubmitter"] + "_" + title + "_" + post['postId'],
                  end="\n\n")

            folderDir = directory / (post["postSubmitter"] + "_" + title +
                                     "_" + post['postId'])

            try:
                if not os.path.exists(folderDir):
                    os.makedirs(folderDir)
            except FileNotFoundError:
                folderDir = directory / post['postId']
                os.makedirs(folderDir)

            for i in range(imagesLenght):
                try:
                    imageURL = images[i]['mp4']
                except KeyError:
                    imageURL = images[i]['link']

                images[i]['Ext'] = getExtension(imageURL)

                fileName = (str(i + 1) + "_" +
                            nameCorrector(str(images[i]['title'])) + "_" +
                            images[i]['id'])

                fileDir = folderDir / (fileName + images[i]['Ext'])
                tempDir = folderDir / (fileName + ".tmp")

                print("  ({}/{})".format(i + 1, imagesLenght))
                print("  {}".format(fileName + images[i]['Ext']))

                try:
                    getFile(fileDir, tempDir, imageURL, indent=2)
                    print()
                except FileAlreadyExistsError:
                    print("  The file already exists" + " " * 10, end="\n\n")
                    duplicates += 1
                    howManyDownloaded -= 1

                # IF FILE NAME IS TOO LONG, IT WONT REGISTER
                except FileNameTooLong:
                    fileName = (str(i + 1) + "_" + images[i]['id'])
                    fileDir = folderDir / (fileName + images[i]['Ext'])
                    tempDir = folderDir / (fileName + ".tmp")
                    try:
                        getFile(fileDir, tempDir, imageURL, indent=2)
                    # IF STILL TOO LONG
                    except FileNameTooLong:
                        fileName = str(i + 1)
                        fileDir = folderDir / (fileName + images[i]['Ext'])
                        tempDir = folderDir / (fileName + ".tmp")
                        getFile(fileDir, tempDir, imageURL, indent=2)

                except Exception as exception:
                    print("\n  Could not get the file")
                    print("  " + "{class_name}: {info}".format(
                        class_name=exception.__class__.__name__,
                        info=str(exception)) + "\n")
                    exceptionType = exception
                    howManyDownloaded -= 1

            if duplicates == imagesLenght:
                raise FileAlreadyExistsError
            elif howManyDownloaded + duplicates < imagesLenght:
                raise AlbumNotDownloadedCompletely(
                    "Album Not Downloaded Completely")
    def __init__(self, directory, post):
        try:
            IMAGES = self.getLinks(post['CONTENTURL'])
        except urllib.error.HTTPError:
            raise NotADownloadableLinkError("Not a downloadable link")

        imagesLenght = len(IMAGES)
        howManyDownloaded = imagesLenght
        duplicates = 0

        if imagesLenght == 1:

            extension = getExtension(IMAGES[0])
            """Filenames are declared here"""

            filename = GLOBAL.config['filename'].format(
                **post) + post["EXTENSION"]
            shortFilename = post['POSTID'] + extension

            imageURL = IMAGES[0]
            if 'https://' not in imageURL or 'http://' not in imageURL:
                imageURL = "https://" + imageURL

            getFile(filename, shortFilename, directory, imageURL)

        else:
            filename = GLOBAL.config['filename'].format(**post)

            print(filename)

            folderDir = directory / filename

            try:
                if not os.path.exists(folderDir):
                    os.makedirs(folderDir)
            except FileNotFoundError:
                folderDir = directory / post['POSTID']
                os.makedirs(folderDir)

            for i in range(imagesLenght):

                extension = getExtension(IMAGES[i])

                filename = str(i + 1) + extension
                imageURL = IMAGES[i]
                if 'https://' not in imageURL and 'http://' not in imageURL:
                    imageURL = "https://" + imageURL

                print("  ({}/{})".format(i + 1, imagesLenght))
                print("  {}".format(filename))

                try:
                    getFile(filename, filename, folderDir, imageURL, indent=2)
                    print()
                except FileAlreadyExistsError:
                    print("  The file already exists" + " " * 10, end="\n\n")
                    duplicates += 1
                    howManyDownloaded -= 1

                except Exception as exception:
                    # raise exception
                    print("\n  Could not get the file")
                    print("  " + "{class_name}: {info}".format(
                        class_name=exception.__class__.__name__,
                        info=str(exception)) + "\n")
                    howManyDownloaded -= 1

            if duplicates == imagesLenght:
                raise FileAlreadyExistsError
            elif howManyDownloaded + duplicates < imagesLenght:
                raise AlbumNotDownloadedCompletely(
                    "Album Not Downloaded Completely")
Beispiel #6
0
    def __init__(self, directory, post):
        self.imgurClient = self.initImgur()

        imgurID = self.getId(post['CONTENTURL'])
        content = self.getLink(imgurID)

        if not os.path.exists(directory): os.makedirs(directory)

        if content['type'] == 'image':

            try:
                post['MEDIAURL'] = content['object'].mp4
            except AttributeError:
                post['MEDIAURL'] = content['object'].link

            post['EXTENSION'] = getExtension(post['MEDIAURL'])

            filename = GLOBAL.config['filename'].format(
                **post) + post["EXTENSION"]
            shortFilename = post['POSTID'] + post['EXTENSION']

            getFile(filename, shortFilename, directory, post['MEDIAURL'])

        elif content['type'] == 'album':
            images = content['object'].images
            imagesLenght = len(images)
            howManyDownloaded = imagesLenght
            duplicates = 0

            filename = GLOBAL.config['filename'].format(**post)

            print(filename)

            folderDir = directory / filename

            try:
                if not os.path.exists(folderDir):
                    os.makedirs(folderDir)
            except FileNotFoundError:
                folderDir = directory / post['POSTID']
                os.makedirs(folderDir)

            for i in range(imagesLenght):
                try:
                    imageURL = images[i]['mp4']
                except KeyError:
                    imageURL = images[i]['link']

                images[i]['Ext'] = getExtension(imageURL)

                filename = (str(i + 1) + "_" +
                            nameCorrector(str(images[i]['title'])) + "_" +
                            images[i]['id'])

                shortFilename = (str(i + 1) + "_" + images[i]['id'])

                print("\n  ({}/{})".format(i + 1, imagesLenght))

                try:
                    getFile(filename,
                            shortFilename,
                            folderDir,
                            imageURL,
                            indent=2)
                    print()
                except FileAlreadyExistsError:
                    print("  The file already exists" + " " * 10, end="\n\n")
                    duplicates += 1
                    howManyDownloaded -= 1

                except Exception as exception:
                    print("\n  Could not get the file")
                    print("  " + "{class_name}: {info}".format(
                        class_name=exception.__class__.__name__,
                        info=str(exception)) + "\n")
                    howManyDownloaded -= 1

            if duplicates == imagesLenght:
                raise FileAlreadyExistsError
            elif howManyDownloaded + duplicates < imagesLenght:
                raise AlbumNotDownloadedCompletely(
                    "Album Not Downloaded Completely")