Skip to content

Instantly share code, notes, and snippets.

@jeremyosborne
Created August 1, 2013 22:24
Show Gist options
  • Save jeremyosborne/6135897 to your computer and use it in GitHub Desktop.
Save jeremyosborne/6135897 to your computer and use it in GitHub Desktop.
# Download pictures of cats.
# 1) Ask the user for width of the cat.
# 2) Ask the user for the height of the cat.
# 3) Ask the user for the name of the cat image.
# 4) Download the cat image.
# 5) ONLY IF... the name of the cat image doesn't already
# exist on disk.
# BONUS
# * Build a webpage with ALLLLLL of the images downloaded.
import os
import urllib2
width = raw_input("Gimme x pixels: ")
height = raw_input("Gimme y pixels: ")
cat_name = raw_input("Gimme cat name: ") + ".jpg"
url = "http://placekitten.com/%s/%s" % (width, height)
if os.path.exists(cat_name):
print "I do not want to overwrite:", cat_name, "exiting."
else:
catdata = urllib2.urlopen(url)
f = open(cat_name, "wb")
f.write(catdata.read())
f.close()
print "Made", cat_name, "for you."
# EXTRA CREDIT below.....
f = open("list_of_cats.txt", "a")
f.write(cat_name + "\n")
f.close()
f = open("list_of_cats.txt", "r")
list_of_cats = []
for cat in f:
cat_img = "<img src='%s'/>" % cat
list_of_cats.append(cat_img)
f.close()
f = open("cats.html", "w")
f.write("\n".join(list_of_cats))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment