Advertisement
Guest User

Untitled

a guest
Mar 5th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import os.path
  2. import os
  3. import sys
  4. import BeautifulSoup
  5. import urllib2
  6. import urllib
  7. import urlparse
  8. from clint import textui as text
  9.  
  10. #a really basic version control.
  11. version = '1.0.3'
  12.  
  13. #Get input from commandline switch - print an error if none are given.
  14. if len(sys.argv) < 2:
  15.     print text.colored.red('> No arguments given!') #Just some fun with clint for pretty text.
  16.     print text.colored.red('> Usage: $image.py http://url') #Same for here.
  17.     sys.exit(1)
  18.  
  19. #Check for --version & print out the version number if needed.
  20. if sys.argv[1] == "--version":
  21.     print "version - "+version
  22.     exit(0)
  23.  
  24. #Get actual output from commandline.
  25. url = sys.argv[1]
  26.  
  27. #Some error handing if the URL does not work!
  28. try:
  29.     image_url = urllib2.urlopen(url).read()
  30. except Exception, err:
  31.     print 'Could not connect to url: '+text.colored.red(err)
  32.     sys.exit(1)
  33. soup = BeautifulSoup.BeautifulSoup(''.join(image_url))
  34. images = soup.findAll('img')
  35.  
  36. #Loop to go through images, should probably be a function later.
  37. for image in images:
  38.     relative_url = "%(src)s" % image
  39.     filename = image["src"].split("/")[-1]
  40.     relative_url_string = str(relative_url)
  41.     if relative_url_string.startswith('http:') == True:
  42.         print "Filname: "+text.colored.green(relative_url_string)
  43.         output_dir = os.getcwd()
  44.         out_path = os.path.join(output_dir,filename)
  45.         print "Saving file to : "+text.colored.blue(out_path)
  46.         urllib.urlretrieve(relative_url_string, out_path)
  47.         #Still need to figure out how to enumerate these images.
  48.     elif relative_url_string.startswith('http') == False:
  49.         try:
  50.             parsed_image_url = urlparse.urljoin(url,image['src'])
  51.             output_dir = os.getcwd()
  52.             out_path = os.path.join(output_dir,filename)
  53.             print "Filename: "+text.colored.green(parsed_image_url)
  54.             print "Saving file to : "+text.colored.blue(out_path)
  55.             #urllib.urlretrieve(parsed_image_url, out_path)
  56.         except Exception, err:
  57.             print 'Could not parse image: '+text.colored.red(err)
  58.         #print text.colored.red('URL does not start with http:')
  59.         #print text.colored.red(relative_url_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement