Created
April 26, 2011 14:31
-
-
Save jbowes/942358 to your computer and use it in GitHub Desktop.
grab a random image. hook it up to your irc client for good times!
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
# | |
# Here's my alias: | |
# trout = "me slaps $0 around a bit with a large trout"; | |
# image = "exec -out python ~/randimage.py \"$-\""; | |
# "image-arg" = "exec -out python ~/randimage.py -showargs \"$-\""; | |
import sys | |
import urllib | |
import simplejson | |
import random | |
BITLY_LOGIN = "YOUR LOGIN GOES HERE" | |
BITLY_APIKEY = "YOUR KEY GOES HERE" | |
sys.argv.pop(0) | |
if (len(sys.argv) > 0 and sys.argv[0] == "-showargs"): | |
show_args = True | |
sys.argv.pop(0) | |
else: | |
show_args = False | |
if (len(sys.argv) > 0): | |
term = sys.argv[0] | |
else: | |
term = sys.stdin.read() | |
google_url = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" | |
bitly_url = "http://api.bitly.com/v3/shorten?login=%s&apiKey=%s&format=txt&longUrl=" % (BITLY_LOGIN, BITLY_APIKEY) | |
res = urllib.urlopen(google_url + urllib.quote(term)) | |
results = simplejson.load(res) | |
try: | |
image_url = random.choice(results["responseData"]["results"])["url"] | |
except: | |
sys.exit(0) | |
short = urllib.urlopen(bitly_url + image_url).read() | |
if show_args: | |
print "[image (%s)] %s" % (term.strip(), short.strip()) | |
else: | |
print "[image] " + short.strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment