Last active
December 8, 2015 05:57
-
-
Save charsi/fd106d71b08092ad889b to your computer and use it in GitHub Desktop.
Gets a list of working hot-linkable URLs to images of puppies!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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