def isPostExists(POST): """Figure out a file's name and checks if the file already exists""" title = nameCorrector(POST['postTitle']) PATH = GLOBAL.directory / POST["postSubreddit"] possibleExtensions = [".jpg", ".png", ".mp4", ".gif", ".webm", ".md"] """If you change the filenames, don't forget to add them here. Please don't remove existing ones """ for extension in possibleExtensions: OLD_FILE_PATH = PATH / (title + "_" + POST['postId'] + extension) FILE_PATH = PATH / (POST["postSubmitter"] + "_" + title + "_" + POST['postId'] + extension) SHORT_FILE_PATH = PATH / (POST['postId'] + extension) if OLD_FILE_PATH.exists() or \ FILE_PATH.exists() or \ SHORT_FILE_PATH.exists(): return True else: return False
def __init__(self, directory, POST): try: POST['mediaURL'] = self.getLink(POST['postURL']) except IndexError: raise NotADownloadableLinkError("Could not read the page source") except Exception as exception: raise NotADownloadableLinkError("Could not read the page source") POST['postExt'] = getExtension(POST['mediaURL']) if not os.path.exists(directory): os.makedirs(directory) 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'])
def __init__(self, directory, POST): POST['postExt'] = getExtension(POST['postURL']) if not os.path.exists(directory): os.makedirs(directory) title = nameCorrector(POST['postTitle']) print(title + "_" + POST['postId'] + POST['postExt']) fileDir = title + "_" + POST['postId'] + POST['postExt'] fileDir = directory / fileDir tempDir = title + "_" + POST['postId'] + ".tmp" tempDir = directory / tempDir getFile(fileDir, tempDir, POST['postURL'])
def postExists(POST): """Figure out a file's name and checks if the file already exists""" title = nameCorrector(POST['postTitle']) FILENAME = title + "_" + POST['postId'] PATH = GLOBAL.directory / POST["postSubreddit"] possibleExtensions = [".jpg", ".png", ".mp4", ".gif", ".webm"] for i in range(2): for extension in possibleExtensions: FILE_PATH = PATH / (FILENAME + extension) if FILE_PATH.exists(): return True else: FILENAME = POST['postId'] else: return False
def __init__(self, directory, POST): try: POST['mediaURL'] = self.getLink(POST['postURL']) except IndexError: raise NotADownloadableLinkError except Exception as exception: raise NotADownloadableLinkError POST['postExt'] = getExtension(POST['mediaURL']) if not os.path.exists(directory): os.makedirs(directory) title = nameCorrector(POST['postTitle']) print(title + "_" + POST['postId'] + POST['postExt']) fileDir = directory / (title + "_" + POST['postId'] + POST['postExt']) tempDir = directory / (title + "_" + POST['postId'] + ".tmp") getFile(fileDir, tempDir, POST['mediaURL'])
def __init__(self, directory, POST): POST['postExt'] = getExtension(POST['postURL']) if not os.path.exists(directory): os.makedirs(directory) 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['postURL']) except FileNameTooLong: fileDir = directory / (POST['postId'] + POST['postExt']) tempDir = directory / (POST['postId'] + ".tmp") getFile(fileDir, tempDir, POST['postURL'])
def __init__(self, directory, post): if not os.path.exists(directory): os.makedirs(directory) title = nameCorrector(post['postTitle']) print(post["postSubmitter"] + "_" + title + "_" + post['postId'] + ".md") fileDir = directory / (post["postSubmitter"] + "_" + title + "_" + post['postId'] + ".md") if Path.is_file(fileDir): raise FileAlreadyExistsError try: self.writeToFile(fileDir, post) except FileNotFoundError: fileDir = post['postId'] + ".md" fileDir = directory / fileDir self.writeToFile(fileDir, post)
def __init__(self,directory,post): if not os.path.exists(directory): os.makedirs(directory) title = nameCorrector(post['postTitle']) """Filenames are declared here""" print("Self: " + str(GLOBAL.picNumber) + "_" + post["postSubmitter"]+"_"+title+"_"+post['postId']+".md") fileDir = directory / ( str(GLOBAL.picNumber) + "_" + post["postSubmitter"]+"_"+title+"_"+post['postId']+".md" ) if Path.is_file(fileDir): raise FileAlreadyExistsError try: self.writeToFile(fileDir,post) except FileNotFoundError: fileDir = post['postId']+".md" fileDir = directory / fileDir self.writeToFile(fileDir,post)
def __init__(self,directory,POST): POST['postExt'] = getExtension(POST['postURL']) if not os.path.exists(directory): os.makedirs(directory) title = nameCorrector(POST['postTitle']) """Filenames are declared here""" print('Direct: ' + str(GLOBAL.picNumber) + "_" + POST["postSubmitter"]+"_"+title+"_"+POST['postId']+POST['postExt']) fileDir = directory / ( str(GLOBAL.picNumber) + "_" + POST["postSubmitter"]+"_"+title+"_"+POST['postId']+POST['postExt'] ) tempDir = directory / ( str(GLOBAL.picNumber) + "_" + POST["postSubmitter"]+"_"+title+"_"+POST['postId']+".tmp" ) try: getFile(fileDir,tempDir,POST['postURL']) except FileNameTooLong: fileDir = directory / (POST['postId']+POST['postExt']) tempDir = directory / (POST['postId']+".tmp") getFile(fileDir,tempDir,POST['postURL'])
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")