# -*- coding: utf-8 -*-
"""
图灵机器人API
"""
from __future__ import division
from ..__load__ import *
from .base import Base

class Tuling(Base):

	def __init__(self):
		super(self.__class__, self).__init__()

	def setting(self, key, **args):
		self.key = key

	def chat(self, fp):
		msg = ''.join(texts)
		try:
			url = "http://www.tuling123.com/openapi/api"
			userid = str(get_mac())[:32]
			body = {'key': self.key, 'info': msg, 'userid': userid}
			r = requests.post(url, data=body)
			respond = json.loads(r.text)
			result = ''
			if respond['code'] == 100000:
				result = respond['text'].replace('<br>', '  ')
				result = result.replace(u'\xa0', u' ')
			elif respond['code'] == 200000:
				result = respond['url']
			elif respond['code'] == 302000:
				for k in respond['list']:
					result = result + u"【" + k['source'] + u"】 " +\
						k['article'] + "\t" + k['detailurl'] + "\n"
			else:
				result = respond['text'].replace('<br>', '  ')
				result = result.replace(u'\xa0', u' ')
			return self.chatLog(1, result)
		except Exception:
			return self.chatLog(2, msg)