CSC111 getAstrology.py

From CSclasswiki
Jump to: navigation, search

--Thiebaut 22:19, 15 April 2009 (UTC)


# getAstrology.py
# D. Thiebaut
#
# The page at http://horoscopes.astrology.com/index/dailyindex.html
# contains 12 links to the 12 astrological signs.
# Each sign is associated with a URL.
# For example, Aries is associated with
# http://horoscopes.astrology.com/dailyaries.html
# and Leo is associated with
# http://horoscopes.astrology.com/dailyleo.html
#
# Looking at one of these, we find that that information
# we are interested in is found in the Web page in the
# following form:
#
# <h1>LEO</h1>
# <h3>July 23-August 22</h3><br />
# <p><span id="intelliTxt">A small event this morning blossoms
# into a full-scale epic adventure and you have the time of your
# life. If things aren't happening by mid-afternoon, you may need
# to jump-start them.
# </span>
#
# This program is given the name of sign and returns the astrological
# forecast for the day. :-)
#

import urllib2
import sys

BASEURL = "http://horoscopes.astrology.com/daily%s.html"
BEGINSECTION = "<h1>%s</h1>"
ENDSECTION = "</span>"

def getDatesAndFuture( sign ):
   # given a sign, the function fetches the astrology page for this                                                                         
   # sign and extracts the dates for the sign, and the "fortune"                                                                            
 
   url = BASEURL % ( sign.lower() )
   print url
   f = urllib2.urlopen( url )
   htmlText = f.read()
   #print htmlText

   beginIndex = htmlText.find( BEGINSECTION % sign.upper() )
   endIndex   = htmlText.find( ENDSECTION, beginIndex )

   if beginIndex ==-1 or endIndex==-1:
       print "Error: beginIndex = %d endIndex = %d" \
             % ( beginIndex, endIndex )
       return "Dates not found", "Future not found"

   htmlText = htmlText[ beginIndex : endIndex ]
   dates = htmlText[ htmlText.find("<h3>")+4 : htmlText.find( "</h3>" ) ]
   #print dates
   future = htmlText[ htmlText.find( "intelliTxt" )+12 : ]
   #print future
   return dates, future
   def main( ):
   sign =   raw_input( "enter sign: " ).lower()
   dates, future = getDatesAndFuture( sign )
   print "%s (%s)" % ( sign, dates )
   print "Your fortune of the day: %s" % (future)


main()