def test_parser_main_mission_original(): base_dir = os.path.dirname(__file__) data_path = os.path.join(base_dir, './testdata', 'data_mission.html') with open(data_path, 'r') as f: conts = f.read() ParserConf() p = Parser() res = p.parse('mission_check', conts) req = res.get('req_data', None) info = res.get('info', None) assert res.get('info', None) is not None assert res.get('stamina_cur', None) is None assert res.get('req_action', None) is not None assert res.get('boss_flag', None) == False assert info is not None assert info.get('q_energy', None) == 120 assert info.get('q_exp', None) == 12 assert info.get('exp_cur', None) == 212 assert info.get('card_all', None) == 70 assert info.get('stamina_max', None) == 116 assert info.get('exp_max', None) == 580 assert info.get('stamina_cur', None) == 3 assert info.get('card_cur', None) == 24 assert info.get('st_flag', None) == False assert info.get('progress', None) == 23 assert info.get('q_stamina', None) == 12 assert req is not None assert req.get('chip_id', None) == '84' assert req.get('now_page', None) == '21' assert req.get('_method', None) == 'POST'
def test_parser_main_battle_parse(): base_dir = os.path.dirname(__file__) data_path = os.path.join(base_dir, './testdata', 'data_battle.html') with open(data_path, 'r') as f: conts = f.read() ParserConf() p = Parser() target_uid, user_hash, form = p.parse('battle', conts) assert target_uid == 400000 assert len(user_hash.keys()) == 5 assert user_hash.get(target_uid, None) is not None user_info = user_hash.get(target_uid, None) assert user_info.get('url', None) is not None assert user_info.get('user_lv', None) == 47 assert user_info.get('cost', None) == 102 assert user_info.get('user_name', None) is not None assert form.get('action', None) is not None req = form.get('req_data', None) assert req is not None assert req.get('del_treasure', None) == b'__1__' assert req.get('_method', None) == b'POST'
def test_parser_main_battle_confirm_parse(): base_dir = os.path.dirname(__file__) data_path = os.path.join(base_dir, './testdata', 'data_battle_confirm.html') with open(data_path, 'r') as f: conts = f.read() ParserConf() p = Parser() req_data, form_act = p.parse('battle_check', conts) assert req_data.get('reinforce1', None) is not None assert req_data.get('sel_deck_name', None) is not None assert req_data.get('post_param', None) == b'0' assert req_data.get('_method', None) == b'POST' assert 'http://localhost' == form_act
def test_parser_main_mypage_original(): base_dir = os.path.dirname(__file__) data_path = os.path.join(base_dir, './testdata', 'data_mypage.html') with open(data_path, 'r') as f: conts = f.read() ParserConf() p = Parser() res = p.parse('mypage', conts) assert res.get('chee_pt', False) == 7310 assert res.get('energy', False) == 4595 assert res.get('stamina_max', False) == 116 assert res.get('riv_cur', False) == 0 assert res.get('riv_max', False) == 20 assert res.get('stamina_cur', False) == 8 assert res.get('event', False) == {} assert res.get('lv', False) == 53 assert res.get('next_lv', False) == 464 assert res.get('card_cur', False) == 30 assert res.get('card_max', False) == 70 assert res.get('power_cur', False) == 13 assert res.get('power_max', False) == 104
def __init__(self, cj_filename, user_conf=None): """Initialize the automator. :param user_conf: filename for configuration :param cj_filename: filename for cookiejar :returns: None """ Cf() if user_conf is not None: if not os.path.exists(user_conf): raise exc.FileNotFoundError("configuration file not found.") else: Cf.update(user_conf) if not os.path.exists(cj_filename): raise exc.FileNotFoundError("cookie-jar file not found.") self._parser = Parser() self._cj_file = cj_filename self._cj = cookiejar.LWPCookieJar() if os.path.isfile(self._cj_file): self._cj.load(self._cj_file) cookie_proc = urllib2.HTTPCookieProcessor(self._cj) self.opener = urllib2.build_opener(cookie_proc) self._header = { "Accept-Language": Cf.get('user.lang'), "Accept": Cf.get('user.mime'), "Accept-Charset": Cf.get('user.charset'), "User-Agent": Cf.get('user.ua'), } self.url_prefix = Cf.get('service.url_prefix') self.path_prefix = self.url_prefix + self.__get_prefix() Cf.set('service.path_prefix', self.path_prefix)
class Automator: """Automator is a class to crawl a website and execute some actions.""" def __init__(self, cj_filename, user_conf=None): """Initialize the automator. :param user_conf: filename for configuration :param cj_filename: filename for cookiejar :returns: None """ Cf() if user_conf is not None: if not os.path.exists(user_conf): raise exc.FileNotFoundError("configuration file not found.") else: Cf.update(user_conf) if not os.path.exists(cj_filename): raise exc.FileNotFoundError("cookie-jar file not found.") self._parser = Parser() self._cj_file = cj_filename self._cj = cookiejar.LWPCookieJar() if os.path.isfile(self._cj_file): self._cj.load(self._cj_file) cookie_proc = urllib2.HTTPCookieProcessor(self._cj) self.opener = urllib2.build_opener(cookie_proc) self._header = { "Accept-Language": Cf.get('user.lang'), "Accept": Cf.get('user.mime'), "Accept-Charset": Cf.get('user.charset'), "User-Agent": Cf.get('user.ua'), } self.url_prefix = Cf.get('service.url_prefix') self.path_prefix = self.url_prefix + self.__get_prefix() Cf.set('service.path_prefix', self.path_prefix) def __get_prefix(self): req = urllib2.Request(self.url_prefix, None, self._header) res = self.opener.open(req) conts = res.read() return self._parser.parse('init', conts) def get_info(self): conts = self._get('mypage') return self._parser.parse('mypage', conts) def get_mission_status(self): conts = self._get('mission') return self._parser.parse('mission_check', conts) def do_recent_mission(self): conts = self._get('mission') res = self._parser.parse('mission_check', conts) if res['info']['st_flag']: l.debug('post req') self._post(res['req_data'], res['req_action']) return res def list_players(self): conts = self._get('battle') user_id, user_hash, form = self._parser.parse('battle', conts) if len(user_hash.keys()) <= 3: res = self._post(form['req_data'], form['action']) user_id, user_hash, form = self._parser.parse('battle', res) print(user_hash) def loop(self, display): init_info = self.get_info() display.show_info(init_info) sleep_base = int(Cf.get('automator.sleep_base')) sleep_rand = int(Cf.get('automator.sleep_rand')) interval = int(Cf.get('automator.interval')) iv_range = int(Cf.get('automator.interval_range')) time.sleep(sleep_base + random.randrange(sleep_rand)) st_flag = True if Cf.get('automator.event_priority') == '0': # mission status = self.do_recent_mission() st_flag = status['info']['st_flag'] display.show_mission_status(status, action='do_mission') time.sleep(sleep_base + random.randrange(sleep_rand)) info = self.get_info() # battle if info['power_cur'] == info['power_max']: l.info("%sbattle%s" % (Color.YELLOW, Color.END_COLOR)) self.battle() time.sleep(sleep_base + random.randrange(sleep_rand)) after_info = self.get_info() gain = after_info['energy'] - info['energy'] l.info(" - gain:%d" % gain) display.show_info(after_info) display.show_guild_info(self.get_guild_info()) # investment time.sleep(sleep_base + random.randrange(sleep_rand)) if info['energy'] > int(Cf.get('automator.investment_threshold')): l.info(' - investment 1k') self.get_investment1k() if not st_flag: interval_sec = interval + random.randrange(iv_range) time.sleep(interval_sec) else: l.warn('Not implemented: event_priority > 0') time.sleep(600) def battle(self): conts = self._get('battle') user_id, user_hash, form = self._parser.parse('battle', conts) if len(user_hash.keys()) <= 10: # change gear conts = self._post(form['req_data'], form['action']) user_id, user_hash, form = self._parser.parse('battle', conts) conts = self._get('battle%2Fcheck%2F' + str(user_id) + '%2F0') req_data, form_action = self._parser.parse('battle_check', conts) self._post(req_data, form_action) def get_guild_info(self): conts = self._get('guild%2Fjoinindex') res = self._parser.parse('guild', conts) return res def get_investment1k(self): conts = self._get('guild%2Fchkinvestment%2F1000') req_data, form_action = self._parser.parse('investment1k', conts) res = self._post(req_data, form_action) return res def run(self, name, display): if name == 'quest_info': display.show_mission_status(self.get_mission_status()) elif name == 'battle': self.list_players() elif name == 'quest': display.show_mission_status(self.do_recent_mission(), action='do_mission') display.show_mission_status(self.get_mission_status()) elif name == 'info': display.show_info(self.get_info()) elif name == 'guild': display.show_guild_info(self.get_guild_info()) elif name == 'investment1k': self.get_investment1k() elif name == 'loop': while True: self.loop(display) def _post(self, param, full_url): req = urllib2.Request(full_url, urllib.urlencode(param), self._header) res = self.opener.open(req) conts = res.read() return conts def _get(self, page): url = self.path_prefix + page req = urllib2.Request(url, None, self._header) res = self.opener.open(req) conts = res.read() return conts
def test_parser_main_battle_invalid_case(): ParserConf() p = Parser() res = p.parse('battle', '......')
def test_parser_main_mission_invalid_case(): ParserConf() p = Parser() res = p.parse('mission_check', '<dummy>')
def test_parser_main_invalid_pagename(): ParserConf() p = Parser() p.parse('<invalid_name>', '<dummy>')