Skip to content

Instantly share code, notes, and snippets.

@pautown
Created April 25, 2021 14:49
Show Gist options
  • Save pautown/10649398eba2a1fa72288d03816a6022 to your computer and use it in GitHub Desktop.
Save pautown/10649398eba2a1fa72288d03816a6022 to your computer and use it in GitHub Desktop.
Posts random pages from PDFS onto twitter
from pdf2image import convert_from_path, convert_from_bytes
from PyPDF2 import PdfFileReader
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
import os
import random
from time import sleep
import tweepy
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")
api = tweepy.API(auth)
def tweet_image(img_path,message):
api.update_with_media(img_path, message)
pdf_dictionary = {}
pdf_dictionary['pdfs/evolution.pdf'] = '"The Wisdom of Nature: An Evolutionary Heuristic for Human Enhancement" by Nick Bostrom (https://nickbostrom.com/evolution.pdf)'
pdf_dictionary['pdfs/It Is The Secret.pdf'] = '"It Is The Secret" by Paul Town (book.paul.town) '
pdf_dictionary['pdfs/DO NOT DISTRO- PAUL TOWN PARTIAL FSGN.pdf'] = '"Finally, Some Good News" by Delicious Tacos (https://www.amazon.com/Finally-Some-Good-Delicious-Tacos/dp/1790356229/) '
pdf_dictionary['pdfs/Logo Daedalus - Selfie, Suicide_ or Cairey Turnbull’s Blue Skidoo (2019).pdf'] = "Selfie, Suicide: or Cairey Turnbull's Blue Skiddoo by Logo Daedalus (https://www.amazon.com/dp/1797819178/ref=cm_sw_r_tw_dp_U_x_DxlqDbW84HT44)"
pdf_dictionary['pdfs/The Beginning Was The End.pdf'] = "The Beginning Was The End by OSCAR KISS MAERTH"
pdf_dictionary['pdfs/Marcus Aurelius - Meditations.pdf'] = "Meditations by Marcus Aurelius"
pdf_dictionary['pdfs/Bronze Age Mindset.pdf'] = "Bronze Age Mindset by Bronze Age Pervert (https://www.amazon.com/Bronze-Age-Mindset-Pervert/dp/1983090441)"
if __name__ == "__main__":
while 1 == 1:
sleep_seconds = 60*60*16 # every 16 hours
pdf_file_name = "pdfs/" + random.choice(os.listdir(os.getcwd() + "/pdfs/"))
pdf = PdfFileReader(open(pdf_file_name, "rb"))
pdf_pages = pdf.getNumPages()
random_page = random.randint(1, pdf_pages)
images = convert_from_path(pdf_file_name, first_page=random_page, last_page=random_page)
images[0].save('image_to_post.jpg')
tweet_image('image_to_post.jpg', pdf_dictionary[pdf_file_name])
print("Tweeted page " + str(random_page) + " " + pdf_dictionary[pdf_file_name])
print("Sleeping", str(sleep_seconds/60/60), "hours")
sleep(sleep_seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment