Changeset 541a2f0d76cd…
Parent 4079d777d3ba…
by
Changes to 2 files · Browse files at 541a2f0d76cd Showing diff from parent 4079d777d3ba Diff from another changeset...
|
|
@@ -0,0 +1,24 @@ + import json
+import os
+import urllib
+import urllib2
+
+def image_of(thing):
+ try:
+ os.mkdir('images')
+ except OSError as e:
+ if e.errno != 17:
+ raise
+ try:
+ j = json.load(urllib2.urlopen('http://api.duckduckgo.com/?q=%s&format=json' % urllib.quote(thing)))
+ image_url = j['Image']
+ if not image_url:
+ return None
+ image_path = 'images/%s' % image_url.split('/')[-1]
+ if not os.path.exists(image_path):
+ data = urllib2.urlopen(image_url).read()
+ with open(image_path, 'wb') as f:
+ f.write(data)
+ return image_path
+ except (OSError,):
+ return None
|
Loading...