def _(self, *arg, **kw): timeit = kw.pop('timeit', False) if not timeit: ret = func(self, *arg, **kw) if hasattr(ret, 'text'): try: data = json.loads(ret.text) except: data = ret.text else: data = ret if data: print info(purple('执行结果:')) print red( unicode_to_utf8(json.dumps( data, indent = 2, ensure_ascii = False )) ) else: st = time.time() for i in xrange(10000): func() print time.time() - st
def youdao_api(words: str): print() url = ("http://fanyi.youdao.com/openapi.do?keyfrom={}&key={}&" "type=data&doctype=json&version=1.1&q={}") try: resp = requests.get(url.format(CONF.youdao_key_from, CONF.youdao_key, words), headers=HEADERS).json() phonetic = "" basic = resp.get("basic", None) if basic and resp.get("basic").get("phonetic"): phonetic += huepy.purple(" [ " + basic.get("phonetic") + " ]") print(" " + words + phonetic + huepy.grey(" ~ fanyi.youdao.com")) print() translation = resp.get("translation", []) if len(translation) > 0: print(" - " + huepy.green(translation[0])) if basic and basic.get("explains", None): for item in basic.get("explains"): print(huepy.grey(" - ") + huepy.green(item)) print() web = resp.get("web", None) if web and len(web): for i, item in enumerate(web): print( huepy.grey(" " + str(i + 1) + ". " + highlight(item.get("key"), words))) print(" " + huepy.cyan(", ".join(item.get("value")))) except Exception: print(" " + huepy.red(ERR_MSG))
def print_env(self): self.username = redis_pool.get(SKEY+'_username') or USERNAME self.password = redis_pool.get(SKEY+'_pwd') or PASSWORD self.host = redis_pool.get(SKEY+'_host') or HOST print info(purple('环境:')) print info(lightpurple(('username: %s' % self.username))) print info(lightpurple('password: %s' % self.password)) print info(lightpurple('host: %s' % self.host)) print ''
def iciba_api(words: str): print() print(huepy.grey(" -------- ")) print() url = "http://dict-co.iciba.com/api/dictionary.php?key={key}&w={w}&type={type}" try: resp = requests.get(url.format(key=CONF.iciba_key, w=words, type="xml")) resp.encoding = "utf8" dct = xmltodict.parse(resp.text).get("dict") ps = dct.get("ps") or "" print(" " + words + " " + huepy.purple(ps) + huepy.grey(" ~ iciba.com")) print() pos = dct.get("pos") acceptation = dct.get("acceptation") if pos and acceptation: if not isinstance(pos, list) and not isinstance(acceptation, list): pos = [pos] acceptation = [acceptation] for p, a in zip([i for i in pos], [i for i in acceptation]): if a and p: print(" - " + huepy.green(p + " " + a)) print() index = 1 sent = dct.get("sent") if not sent: return if not isinstance(sent, list): sent = [sent] for item in sent: for k, v in item.items(): if k == "orig": print( highlight(huepy.grey(" {}. ".format(index) + v), words)) index += 1 elif k == "trans": print(highlight(huepy.cyan(" " + v), words)) print() except Exception: print(" " + huepy.red(ERR_MSG))