Skip to content

Instantly share code, notes, and snippets.

@charsi
Last active December 8, 2015 05:57
Show Gist options
  • Save charsi/fd106d71b08092ad889b to your computer and use it in GitHub Desktop.
Save charsi/fd106d71b08092ad889b to your computer and use it in GitHub Desktop.
Gets a list of working hot-linkable URLs to images of puppies!
# Fetches random puppy images and save sthem to images.txt
# Useful for populating dummy data
import urllib
import json
import requests
numberOfImages = 141
validateUrls = True # slows script execution
def imgExists(path):
r = requests.head(path)
if (
r.status_code == requests.codes.ok and
r.headers['content-type'] == 'image/jpeg'
):
return True
else:
return False
def getPuppyPic():
apiUrl = "http://www.thepuppyapi.com/puppy"
response = urllib.urlopen(apiUrl)
data = json.loads(response.read())
return data['puppy_url']
imgs = []
while(len(imgs) < numberOfImages):
try:
url = getPuppyPic()
# skip ebay images
if url.find('ebay') != -1:
continue
if validateUrls:
# skip if url doesn't work
if not imgExists(url):
continue
# add url to imgs list
imgs.append('"'+url+'"')
print str(len(imgs))+" image URLs fetched so far.."
print str(url)
except requests.exceptions.RequestException as e:
print e
file = open('images.txt', 'w')
urls = "["+(", ".join(imgs))+"]"
file.write(urls)
file.close()
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment