Skip to content

Instantly share code, notes, and snippets.

@dayt0n
Last active January 10, 2019 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dayt0n/ea25232f7d086f73b659353a82bcfb20 to your computer and use it in GitHub Desktop.
Save dayt0n/ea25232f7d086f73b659353a82bcfb20 to your computer and use it in GitHub Desktop.
grab Dr. Rick Coleman's lecture slides for the day and combine into a PDF
#
# getColeman.py - grab Dr. Rick Coleman's lecture slides for the day and combine into a PDF
#
# made by Dayton Hasty
#
# (you may need to pip install fpdf)
#
import os
import requests
import re
from fpdf import FPDF
import datetime
course = "CS121" # change this to whatever your class is
now = datetime.datetime.now()
url = "https://www.cs.uah.edu/~rcoleman/" + course + "/TodaysClass/Images/"
print("Grabbing images...")
firstSubstr = "alt=\"[IMG]\"></td><td><a href=\""
r = requests.get(url)
lines = (r.text).split('\n')
images = []
for line in lines:
if firstSubstr in line:
tags = line.split("<") # break into smaller pieces for easier regex
for tag in tags:
if "a href" in tag:
images.append(re.search("\"(.*)\"",tag).group(1))
pdf = FPDF()
title = (images[0].split("_"))[0] # get title of lecture from image titles
for image in images:
print("Downloading %s" % image)
r = requests.get(url+image)
if r.status_code != 200: # make sure we have permisison to access the files
print("It doesn't look like the images for today are available. Please try again later.")
exit(-1)
with open(image,'wb') as f:
f.write(r.content)
pdf.add_page()
pdf.image(image,x = None, y = 75,w = 190) # this is a rough guess and check of the correct width and y position.
os.remove(image)
pdf.output(title+"_"+str(now.month)+"-"+str(now.day)+"-"+str(now.year)+".pdf","F") # write to PDF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment