""" getURLinfo.py
 J Cardell 2011, D. Thiebaut, 2009
 CSC111 Example, April 14, 2011

 Class definition for getURLinfo. After initializing an object's instance
 variables, the .getInfo() method will return the text from the given
 URL that is between the beginning and ending markers.
 
 Instance variables: url, begMarker, endMarker
 Methods: constructor, setURL, setMarkers, getInfo
"""

import urllib2

class getURLinfo:
    
    def __init__( self ):
        """Constructor, creates the object and assigns "None" to the
        instance variables."""
        self.url         = None
        self.begMarker = None
        self.endMarker   = None

    def setURL( self, u ):
        """Mutator: assign value to the instance variable, URL"""   
        self.url = u

    def setMarkers( self, begin, end ):
        """Mutator: assign values to the instance variables begMarker
        and endMarker"""   
        self.begMarker = begin
        self.endMarker = end

    def getInfo( self, identifier ):
        """Return the text between the two markers for the specific URL
        indicated by the 'identifier' parameter."""
        
        # Create the exact URL string and retrieve the indicated html text 
        f = urllib2.urlopen( self.url % identifier )
        html = f.read()

        # Locate the markers in the htmlText string 
        begIndex = html.find( self.begMarker )
        endIndex = html.find( self.endMarker, begIndex )

        # Include some simple error checking if the markers are not found 
        if begIndex== -1 or endIndex== -1:
            return None

        # Advance the starting index to the actual beginning of the desired text
        begIndex = begIndex + len( self.begMarker )

        # Slice out and return the desired text 
        return html[ begIndex : endIndex ]


# ---------------------------------------------------------
#   Testing Area
# ---------------------------------------------------------

if __name__=="__main__":

    # Create an object of type getURLinfo
    zodSite = getURLinfo()

    # Initialize the instance variables
    zodSite.setURL("http://my.horoscope.com/astrology/free-daily-horoscope-%s.html")
    zodSite.setMarkers('id="textline">',"</div>")

    # Retrieve the horoscope and display it
    horos = zodSite.getInfo("capricorn")
    print "Your horoscope is: \n",horos
