import os

import requests


class SSCConnection(object):
    def __init__(self):
        self.base_url = "https://courses.students.ubc.ca/cs/main"
        self.cache_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "__cache__"
        )
        if not os.path.exists(self.cache_path):
            os.mkdir(self.cache_path)

    def get_course_page(self, dept="CPSC", course="304", sessyr="2014", sesscd="W"):
        page_name = "_".join(map(lambda x: str(x).lower(), [dept, course, sessyr, sesscd]))
        page = self._retrieve_cached_page(page_name)
        if page is None:
            r = requests.get(self.base_url, params=dict(
                pname="subjarea",
                tname="subjareas",
                req="3",
                dept=dept,
                course=course,
                sessyr=sessyr,
                sesscd=sesscd
            ))
            page_data = r.text
            self._cache_page(page_name, page_data)
            return page_data
        else:
            return page

    def _cache_page(self, name, text):
        filename = os.path.join(self.cache_path, name)
        with open(filename, 'w+') as f:
            f.write(text)

    def _retrieve_cached_page(self, name):
        filename = os.path.join(self.cache_path, name)
        if os.path.exists(filename):
            with open(filename, 'r') as f:
                return f.read()
        else:
            return None

@@ -3,17 +3,28 @@
 port os%0A
+import time
 %0A
-
 import r
@@ -58,24 +58,141 @@
 on(object):%0A
+    %22%22%22Connection to UBC SSC%0A%0A    :param cache_period: Life of cache before invalidation (number of seconds)%0A    %22%22%22%0A
     def __in
@@ -200,16 +200,36 @@
 t__(self
+, cache_period=86400
 ):%0A     
@@ -289,16 +289,57 @@
 s/main%22%0A
+        self.cache_period = cache_period%0A
         
@@ -633,16 +633,321 @@
 d=%22W%22):%0A
+        %22%22%22Get course page from SSC%0A%0A        Retrieved pages are cached for a %60%60self.cache_period%60%60, and then invalidated.%0A%0A        :type dept: str%0A        :type course: str%7Cint%0A        :type sessyr: str%7Cint%0A        :type sesscd: str%0A        :returns: Text of SSC course page for given course%0A        %22%22%22%0A
         
@@ -1030,16 +1030,66 @@
 sscd%5D))%0A
+        # Attempt to retrieve already cached page%0A
         
@@ -1129,24 +1129,85 @@
 (page_name)%0A
+        # If not already cached, retrieve, cache, and return%0A
         if p
@@ -1905,32 +1905,83 @@
 che_path, name)%0A
+        # First case, cache does not already exist%0A
         if os.pa
@@ -1975,16 +1975,20 @@
      if 
+not 
 os.path.
@@ -2021,44 +2021,264 @@
     
-with open(filename, 'r') as f:%0A     
+return None%0A        # Check if existing cache is invalid (if so, remove and return None)%0A        last_modified = os.path.getmtime(filename)%0A        period = time.time() - last_modified%0A        if period %3E self.cache_period:%0A            os.remove(filename)%0A
     
@@ -2288,36 +2288,109 @@
     
+ 
 return 
-f.read()%0A        else
+None%0A        # Cache exists, and is valid, so return it%0A        with open(filename, 'r') as f
 :%0A  
@@ -2390,29 +2390,33 @@
 s f:%0A            return 
-None
+f.read()
 %0A

Add cache invalidation to SSCConnection