PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/duck.py

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