PageRenderTime 124ms CodeModel.GetById 90ms app.highlight 5ms RepoModel.GetById 26ms app.codeStats 0ms

/duck.py

https://github.com/crazedpsyc/crazedpsyc
Python | 52 lines | 40 code | 11 blank | 1 comment | 12 complexity | 7dcf3f5f3be4d98f48e6fc157d96af0b MD5 | raw file
 1#!/usr/bin/env python
 2from urllib import urlopen
 3
 4def main(line, ld, conf):
 5    if len(line) > 1:
 6        print 'Searching %s for %s...' % (conf.search_engine['name'], ' '.join(line[1:]))
 7        queryurl = conf.search_engine['url'] % '+'.join(line[1:])
 8
 9        try: raw = urlopen(queryurl).read()
10        except: return 'error'
11        tmp = open(ld + '/dostmp.py', 'w')
12        tmp.write('data = ' + raw)
13        tmp.close()
14        
15        import dostmp
16        reload(dostmp)
17        if os.name == 'nt':
18            os.popen('del dostmp.py dostmp.pyc')
19        else:
20            os.popen('rm dostmp.py dostmp.pyc')
21        resulted = False
22        
23        if dostmp.data['Definition']: 
24            print 'Definition:', dostmp.data['Definition']
25            resulted = True
26        if dostmp.data['Answer']: 
27            print 'Answer:', dostmp.data['Answer']
28            resulted = True
29        if dostmp.data['AbstractText']: 
30            print 'Abstract Text:', dostmp.data['AbstractText']
31            resulted = True
32        if dostmp.data['Abstract'] and dostmp.data['Abstract'] != dostmp.data['AbstractText']: 
33            print 'Abstract:', dostmp.data['Abstract']
34            resulted = True
35            
36        if resulted: return
37
38        if dostmp.data['RelatedTopics']:
39            print 'Related topics:'
40            for res in dostmp.data['RelatedTopics']:
41                print res['Text']
42                
43
44                
45                
46class conf: # a quick class to simulate the configuration module for running without DOSprompt
47    search_engine = {'name': 'DuckDuckGo', 'url': 'https://api.duckduckgo.com/?q=%s&o=json'}
48    
49if __name__ == '__main__':
50    import sys, os
51    sys.path += [os.getcwd()]
52    main(sys.argv, os.getcwd(), conf)