def next(self): """ see next page 's result simplely. """ if not self.next_url: LogControl.fail("no next url found !") return '' return self.proxy_to(self.host + self.next_url).json()
def display(code, content): ''' display content ''' if int(code / 200) == 1: res = '\n'.join([ i.text for i in self.xpath( content, "div[%s]" % Baidu.config['translate_tag'], 'td', '*') ]) LogControl.ok(res) else: LogControl.fail("not found this")
def _link(self, url, encoding='utf8'): try: agent = random_choice(AGS) res = to(url, headers={'user-agent': agent}) if res.status_code != 200: raise Error404("not reachable", res.status_code) if self.charset: return url, self.parser( res.content.decode(self.charset, 'ignore')) return url, self.parser(res.content.decode(encoding, 'ignore')) except HTTPError as e: LogControl.fail(url, e.code) return url, None except Exception as e: LogControl.wrn(url) LogControl.err(url, e, txt_color='yellow') return url, None else: LogControl.ok(url)
def display(code, content): ''' display content ''' if int(code / 200) == 1: try: tree_dict(content['trans_result'], single_display, 'base') if detail: tree_dict(content['dict_result'], single_display, 'dict') if example: tree_dict(content['liju_result'], single_display, 'sample') except KeyError: LogControl.fail("Only: ", *content.keys()) for k in content: LogControl.err(k, content[k]) else: LogControl.fail("not found this")
def create_multi_templates(debug=False): """ create a multi-cmd template. """ cmd_group_name = dinput("cmd group's name?\n[q: exit]>", default='q') if cmd_group_name == 'q': return False while 1: cmd_t = dinput('which cmd?\n[q :exit]>', default='q').split() if cmd_t[0] == 'q': break # print(cmd_t) vals = list(search_cmd(*cmd_t).values()) if len(vals) == 0: continue if isinstance(vals[0], list) : vals = [i.strip() for i in vals[0]] if not vals: continue for i, v in enumerate(vals): LogControl.ok(i, v, txt_color='cyan') cmd_d = vals[int(dinput('choose a ID , (deafult 0)', default=0))] k_in = 2 while 1: k_in = dinput('\n%s\nchoose parts to replace ,separate by ' ' [default 2] set :' % colored(cmd_d, attrs=['bold']), default='1') k = [int(i) for i in k_in.split()] cmd_args = cmd_d.decode('utf8').split() if isinstance(cmd_d, bytes) else cmd_d.split() mvals = {} for kv in k: cmd_args[kv] = colored(cmd_args[kv], attrs=['underline']) v = dinput('%s |[-r: reset parts, default: -r] %d=' % (' '.join(cmd_args), kv), default='-r') if v == '-r': LogControl.fail("reset") continue mvals[v] = kv st, key, out = create_single_template(cmd_d, debug=debug, **mvals) save_template_to_sqlite_db(cmd_group_name, st, key, out) break