Advertisement
evandrix

[css] - <img src="..."

May 9th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os, re, urllib
  4. from urllib2 import Request, urlopen, URLError, HTTPError
  5.  
  6. def main():
  7.     base_url = "http://www.google.com/events/io/2011/static/css/"
  8.     source = "style.css"
  9.     file = open(source)
  10.  
  11.     # download images referenced in stylesheet
  12.     for line in file:
  13.         urls = re.findall("url\((.*)\)", line)
  14.         if (urls and len(urls) > 0):
  15.             fullpath = base_url + urls[0]
  16.             filename = os.path.basename(fullpath)
  17.             if not os.path.isfile(filename):
  18.                 stealStuff(urls[0], "", base_url)
  19.             else:
  20.                 print "Skipping %s" % filename
  21.  
  22.     # remove relative pathnames in stylesheet
  23.     input = open(source, 'r')
  24.     output = open(source + ".tmp", 'w')
  25.     for s in input:
  26.         urls = re.findall("url\((.*)\)", s)
  27.         if (urls and len(urls) > 0):
  28.             img = os.path.basename(urls[0])
  29.             output.write(re.sub("url\((.*)\)", "url(" + img + ")", s))
  30.         else:
  31.             output.write(s) # otherwise leave line unchanged
  32.  
  33.     # replace original file with new stylesheet
  34.     #os.rename(source + ".tmp", source)
  35.  
  36. def stealStuff(file_name, file_mode, base_url):
  37.     #create the url and the request
  38.     url = base_url + file_name
  39.     # remove all occurrences of the '/../' pattern
  40.     url = re.sub(r'(.*)/(.*)/[.]{2}/(.*)/(.*)', r'\1/\3/\4', url)#.replace("//","/")
  41.     req = Request(url)
  42.  
  43.     # Open the url
  44.     try:
  45.         f = urlopen(req)
  46.         print "downloading " + url
  47.  
  48.         # Open our local file for writing
  49.         local_file = open(os.path.basename(file_name), "w" + file_mode)
  50.         #Write to our local file
  51.         local_file.write(f.read())
  52.         local_file.close()
  53.  
  54.     #handle errors
  55.     except HTTPError, e:
  56.         print "HTTP Error:", e.code , url
  57.         sys.exit(0)
  58.     except URLError, e:
  59.         print "URL Error:", e.reason , url
  60.         sys.exit(0)
  61.  
  62. if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement