#! /python21/python
"""
program: google_image.py
creator: Rob Chavers
created: Feb 13, 2002

purpose: download the web page from google and
         figure out which graphic they are using
         today.  convert it to a thumbnal and
         store the thumbnail on my server
"""

import os
import re
import sys
import httplib
import StringIO
from PIL import Image
from string import join

find = '<img src='
rex = find + '.+?\s'
url = 'www.google.com'
savefile = 'c:/html/images/google.gif'

# since I am using the thumbnail method
# of Image, the new image will be one that
# preserves the aspect of the original image.
# So, the new image will not be *larger*
# than the newsize given.
#
newsize = (150, 40) #(maxwidth, maxheight)


# create a connection to the site
# and "GET" the main page's html source
#
conn = httplib.HTTPConnection(url)
conn.request("GET", "/")
resp = conn.getresponse()
html = resp.read()

# search the html data for our regular expression
#
m = re.compile(rex).search(html.lower(), re.DOTALL)
if not m:
    print 'Error: image source not found using the regular expression!'
    conn.close()
    sys.exit(0)

# Ok, now we have the image file location
# from the first instance of <img src=
# 
start = m.start() + len(find)
end = m.end()
img_src = html[start:end].strip()

# remove the quotes (if they exist)
#
img_src = join(img_src.split('"'), '')

if not img_src:
    print 'Error: no image source string found!'
    conn.close()
    sys.exit(0)

# add a forward slash if we don't have one
# this is required for the request() method
#
img_src = os.path.join('/', img_src)


# let's get today's google image
#
conn.request("GET", img_src)
resp = conn.getresponse()
image = resp.read()
conn.close()

# create the thumbnail of the image,
# then save the thumbnail
#
im = Image.open(StringIO.StringIO(image))
im.thumbnail(newsize)
im.save(savefile)

print 'image src :', url + img_src
print 'orig size :', len(image)
print 'saved as  :', savefile
print 'new size  :', os.stat(savefile)[6]

"""
    # alternative to "os.stat(savefile)[6]" above
    # (incase you wanted to know)
    #
    file = StringIO.StringIO()
    im.save(file, "GIF")
    data = file.getvalue()
    print 'new size  :', len(data)

    # with unix, you could also use this
    # (but not in windows -- unless you
    # see my counter.py for an explanation)
    #
    print 'new size  :', len(im.tostring())    
"""

