Created
December 1, 2014 01:15
-
-
Save fuzzy/5803255a3c92d686535a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import sys, json, urllib2, random | |
if sys.argv[1] == '--usage': | |
print 'Usage: !search <search terms>' | |
sys.exit(0) | |
nick, host, args = sys.argv[1], sys.argv[2], sys.argv[3] | |
data = json.loads(urllib2.urlopen( | |
'http://api.duckduckgo.com/?q=%s&format=json' % urllib2.quote(args) | |
).read()) | |
if 'AbstractURL' in data.keys(): | |
if len(data['AbstractURL']) != 0: | |
print '%s Related: %s' % ( | |
data['AbstractURL'], | |
data['RelatedTopics'][random.randint(0, len(data['RelatedTopics'])-1)]['FirstURL'] | |
) | |
else: | |
print 'No results found.' | |
else: | |
print 'No results found.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment