tuling.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. """
  3. 图灵机器人API
  4. """
  5. from __future__ import division
  6. from ..__load__ import *
  7. from .base import Base
  8. class Tuling(Base):
  9. def __init__(self):
  10. super(self.__class__, self).__init__()
  11. def setting(self, key, **args):
  12. self.key = key
  13. def chat(self, fp):
  14. msg = ''.join(texts)
  15. try:
  16. url = "http://www.tuling123.com/openapi/api"
  17. userid = str(get_mac())[:32]
  18. body = {'key': self.key, 'info': msg, 'userid': userid}
  19. r = requests.post(url, data=body)
  20. respond = json.loads(r.text)
  21. result = ''
  22. if respond['code'] == 100000:
  23. result = respond['text'].replace('<br>', ' ')
  24. result = result.replace(u'\xa0', u' ')
  25. elif respond['code'] == 200000:
  26. result = respond['url']
  27. elif respond['code'] == 302000:
  28. for k in respond['list']:
  29. result = result + u"【" + k['source'] + u"】 " +\
  30. k['article'] + "\t" + k['detailurl'] + "\n"
  31. else:
  32. result = respond['text'].replace('<br>', ' ')
  33. result = result.replace(u'\xa0', u' ')
  34. return self.chatLog(1, result)
  35. except Exception:
  36. return self.chatLog(2, msg)