def updateCourse(course): log.log("Updating %s" % str(course)) url = "https://registrar.princeton.edu/course-offerings/course_details.xml?courseid={}&term={}".format(course.number, TERM) html = urllib2.urlopen(url).read() soup = BeautifulSoup(html) rows = soup.find_all('tr')[1:] for row in rows: fields = row.find_all('td') classNumber = fields[0].get_text().strip() if not classNumber: continue theclass, created = Class.objects.get_or_create(number=classNumber, course=course) theclass.title = fields[1].get_text().strip() theclass.time = fields[2].get_text().strip().replace("\n", "") theclass.days = fields[3].get_text().strip().replace("\n", "") raw = fields[5].get_text().replace("\n","").replace(" ","") sindex = raw.find("Enrolled:") eindex = raw.find("Limit:") theclass.enroll = int(raw[sindex+9:eindex]) theclass.max = int(raw[eindex+6:] or 1000) isClosed = fields[6].get_text().strip() == "Closed" if not isClosed: freeSpots = theclass.max - theclass.enroll try: # don't tweet "P99" if theclass.title != "P99": twitter.tweet("%s has %d open spot(s)." % (str(theclass), freeSpots)) except Exception: pass log.log("Class %s is now open!" % str(classNumber)) for subscription in Subscription.objects.filter(theclass=theclass, active=True): log.log("Sending subscription %s." % str(subscription)) subscription.sendNotification() subscription.active = False subscription.save() theclass.isClosed = isClosed theclass.save()
def updateCourse(course): log.log("Updating %s" % str(course)) url = "https://registrar.princeton.edu/course-offerings/course_details.xml?courseid={}&term={}".format(course.number, TERM) html = urllib2.urlopen(url).read() soup = BeautifulSoup(html) rows = soup.find_all('tr')[1:] for row in rows: fields = row.find_all('td') classNumber = fields[0].get_text().strip() if not classNumber: continue theclass, created = Class.objects.get_or_create(number=classNumber, course=course) theclass.title = fields[1].get_text().strip() theclass.time = fields[2].get_text().strip().replace("\n", "") theclass.days = fields[3].get_text().strip().replace("\n", "") raw = fields[5].get_text().replace("\n","").replace(" ","") sindex = raw.find("Enrolled:") eindex = raw.find("Limit:") theclass.enroll = int(raw[sindex+9:eindex]) theclass.max = int(raw[eindex+6:] or 1000) isClosed = fields[6].get_text().strip() == "Closed" if not isClosed: freeSpots = theclass.max - theclass.enroll try: twitter.tweet("%s has %d open spot(s)." % (str(theclass), freeSpots)) except Exception: pass log.log("Class %s is now open!" % str(classNumber)) for subscription in Subscription.objects.filter(theclass=theclass, active=True): log.log("Sending subscription %s." % str(subscription)) subscription.sendNotification() subscription.active = False subscription.save() theclass.isClosed = isClosed theclass.save()
import sys,os sys.path.insert(0,os.path.abspath("/srv/tigerapps")) import settings from django.core.management import setup_environ setup_environ(settings) from pounce.models import Course, Class, Subscription, Entry, CoursesList import pounce.log as log from update import updateCourse, clean log.log('Running updateall.py') clean() for course in Course.objects.all(): updateCourse(course) list = CoursesList.objects.all()[0] list.cache()
entry, created = Entry.objects.get_or_create( courseNumber=courseNumber, section=section) if created or enroll != entry.totalEnroll or closed != entry.totalClosed: entry.totalEnroll = enroll entry.totalClosed = closed entry.save() course, created = Course.objects.get_or_create( number=courseNumber) course.code = ' / '.join([ code.strip().replace(" ", " ") for code in fields[1].text.split('\n \n') ]) course.title = fields[2].text.strip() course.save() updateCourse(course) except Exception: pass list = CoursesList.objects.all()[0] list.cache() log.log('Running updatenew.py') if (len(CoursesList.objects.all()) == 0): CoursesList().save() scrape()
import sys, os sys.path.insert(0, os.path.abspath("/srv/tigerapps")) import settings from django.core.management import setup_environ setup_environ(settings) from pounce.models import Course, Class, Subscription, Entry, CoursesList import pounce.log as log from update import updateCourse, clean log.log('Running updateall.py') clean() for course in Course.objects.all(): try: updateCourse(course) except Exception: print "Error updating course %s" % course.title list = CoursesList.objects.all()[0] list.cache()