• Coding

    Python Learning: Automate Boring Stuff with Python | Chapter 13: PDF Paranoia

    “Paranoid? Probably. But just because you’re paranoid doesn’t mean there isn’t an invisible demon about to eat your face.” scary… 9:13:00 PM my solution [sourcecode language=”python”] #usage pdf_paranoia_encrypt.py <password> import PyPDF2, os, sys, logging logging.basicConfig(level=logging.DEBUG, format="%(asctime)s – %(levelname)s – %(message)s") #TODO: GET PASSWORD FROM COMMAND LINE if len(sys.argv) == 2: #TODO: OS WALK FROM CURRENT DIRECTORY password = sys.argv[1] for foldername, subfolders, files in os.walk("."): for file in files: #TODO: FIND PDFS if file.endswith(".pdf"): filename = os.path.abspath(os.path.join(foldername, file)) logging.info("Reading file: " + str(filename)) file = open(filename, "rb") pdfFileReader = PyPDF2.PdfFileReader(file) if not pdfFileReader.isEncrypted: pdfFileWriter = PyPDF2.PdfFileWriter() #TODO: COPY EVERY PAGE for pageNum in range(pdfFileReader.numPages): pdfFileWriter.addPage(pdfFileReader.getPage(pageNum)) #TODO: ENCRYPT EACH PDF FILE pdfFileWriter.encrypt(password)…

  • Coding

    “Python Learning: Automate Boring Stuff with Python | Chapter 11 : My Solution to Image Downloader from Imgur

    So you want to download images without a browser..  challenge accepted 3:43:41 PM   [sourcecode language=”python”] #Image Site Downloader #USAGE: python igmurdownloader.py <category> <limit> import sys, requests, os, logging, bs4, re logging.basicConfig(level=logging.DEBUG, format="%(asctime)s – %(levelname)s – %(message)s") logging.disable(logging.CRITICAL) if len(sys.argv) == 3: logging.info("Command line argument 3") #TODO: get category, and limit category = sys.argv[1] limit = sys.argv[2] imgur = "https://imgur.com/" try: #TODO: make request to https://imgur.com/search?q= res = requests.get(imgur + "search?q=" + category) res.raise_for_status() logging.info("Request successful") imgur_soup = bs4.BeautifulSoup(res.text, "html.parser") #TODO: find all img tages imgur_image_list = imgur_soup.select(".image-list-link > img") dir = None #CREATE DIRECTORY FOR IMAGES if not os.path.exists( os.path.abspath("imgur_images") ): dir = os.mkdir("imgur_images") else: dir = os.path.abspath("imgur_images") #TODO: loop…

Enjoy this blog? Please spread the word :)

error: Content is protected !!