Public » Miscellaneous » Twenty
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

add image_of and more ignores

Changeset 541a2f0d76cd

Parent 4079d777d3ba

by Benjamin Pollack

Changes to 2 files · Browse files at 541a2f0d76cd Showing diff from parent 4079d777d3ba Diff from another changeset...

Change 1 of 1 Show Entire File .hgignore Stacked
 
1
2
3
 
 
1
2
3
4
@@ -1,3 +1,4 @@
 syntax: glob  *.pyc  *.db +images
Change 1 of 1 Show Entire File magic.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@@ -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