def _reply(self): url = self._get_reply_url() data = { "message": self._msg, "posttime": self._get_args("posttime"), "formhash": self._get_args("formhash"), "sesig": self._get_args("usesig"), "subject": self._get_args("subject"), } # if the self._msg is exist in the page, do nothing if self._exist(): Recorder.info(self, self._user, "reply exist already") return while self._max_retry: try: Recorder.debug(self, self._user, "try to submit comment") r = self._session.post(url, data=data) r.raise_for_status() Recorder.info(self, self._user, "submit succ") break except Exception as e: msg = "_reply failed[{}]".format(self._max_retry) Recorder.warn(self, self._user, msg, e) else: Recorder.error(self, self._user, "run out of reply request retries")
def locate_post_url(self): while True: url = self._locate() if not url: msg = "mismatch and sleep for {}".format(self._interval) Recorder.debug(self, self._user, msg) time.sleep(self._interval) continue msg = "hit the pattern: {}".format(url) Recorder.info(self, self._user, msg) return url
def __init__(self, session, user, config): try: self._session = session self._user = user self._domain = config["domain"] self._page_url = config["topic_page_url"] self._interval = config["topic_interval"] self._post_regex = config["post_regex"] Recorder.info(self, self._user, "init") except Exception as e: Recorder.error(self, self._user, "init failed", e) raise Exception(e)
def dispatch(fpath): Recorder.info("<dispatch>", "main", "ready to dispatch") configs = configparser.ConfigParser() configs.read(fpath) dog_ls = [] for user, config in configs.iteritems(): if user == "DEFAULT": continue dog = WatchDog(user, config) dog_ls.append(gevent.spawn(dog.monit)) gevent.joinall(dog_ls) Recorder.info("<dispatch>", "main", "all is finished")
def __init__(self, session, user, config, post_url): try: self._session = session self._user = user self._page_url = post_url self._domain = config["domain"] self._name = config["name"] self._msg = config["msg"] self._max_retry = config["post_max_retry"] self._reply_args_regex = config["post_reply_args_regex"] self._reply_url_regex = config["post_reply_url_regex"] Recorder.info(self, self._user, "init") except Exception as e: Recorder.error(self, self._user, "init failed", e) raise Exception(e)
def __init__(self, session, user, config): try: self._session = session self._user = user self._name = config["name"] self._passwd = config["passwd"] self._page_url = config["login_page_url"] self._submit_url = config["login_submit_url"] self._referer_url = config["login_referer_url"] self._succ_flag = config["login_succ_flag"] self._formhash_regex = config["formhash_regex"] Recorder.info(self, self._user, "init") except Exception as e: Recorder.error(self, self._user, "init failed", e) raise Exception(e)
def confirm(self): try: r = self._session.get(self._page_url) data = { "formhash": (re.search(self._formhash_regex, r.text).group(1)), "referer": self._referer_url, "loginfield": "username", "username": self._name, "password": self._passwd, "questionid": 0, "answer": '', "cookietime": 2592000, } r = self._session.post(self._submit_url, data=data) # check if login successed # XXX can't handle Verification Code if self._succ_flag not in r.text: raise Exception("mismatch key words in the page") Recorder.info(self, self._user, "confirm succ") except Exception as e: Recorder.error(self, self._user, "confirm failed", e) raise Exception(e)
def __init__(self, user, config): self._session = requests.Session() self._user = user self._config = config Recorder.info(self, self._user, "init")