def _status(self, msg): open_id = msg.source db = DbOperator() success, user = db.find_user(open_id) if not success: logger.info("_status can't find user:"******"没有找到邮箱绑定记录,请重新绑定" email = user['email'] return "你的账号现在绑定邮箱为:" + email
def update_article_status(article_id, valid, backup_path=None): # 做点准备工作 db = DbOperator() success, item = db.find_article(article_id) if not success: # log it print("can't find article by article_id: " + str(article_id)) logger.error("can't find article by article_id: " + str(article_id)) return False URL = item['URL'] open_id = item['open_id'] success, result = db.find_user(open_id) if not success: logger.error("article & user no match: " + " ".join(map(str, [article_id, open_id]))) return False email = result['email'] if not valid: # 当文章已不可访问 if item['backup_addr'] == FAKE_PATH_PLACE_HOLDER: backup_addr = None else: backup_addr = item['backup_addr'] notify_user(email, URL, backup_addr) db.archive_article(item) logger.info("article expired, move to archive: " + " ".join(map(str, [article_id, URL]))) else: prev_status = item['status'] if prev_status == 1: # 正常观察状态,无需额外操作 return True elif prev_status == 0: # 初次完成观察,制作备份 if backup_path == None: print("backup addr not valid") return False else: new_path = _backup_article(article_id, backup_path) if new_path == None: return False article_info = (article_id, new_path, 1) # 制作备份完成,状态更新成1 return db.update_article(article_info) else: print("unknown status!") logger.error( "article status Error: " + " ".join(map(str, [article_id, open_id, prev_status]))) return True
def _handle_URL(self, msg): open_id = msg.source URL = self._trim_URL(msg.content) db = DbOperator() success, result = db.find_my_article(open_id) if success: for item in result: if URL == item['URL']: reply = "目标已在观察列表中:" + URL return reply success = self.ob.ob_this_one(URL, open_id) if not success: reply = "目标初次访问异常,无法进行备份。若确定是误判,请联系管理员:[email protected]" else: reply = "收到!开始观察目标!" # 判断一下用户是否绑定邮箱,没绑定的话提供警告 success, _ = db.find_user(open_id) if not success: reply = reply + "\n--------------\n【警告】你的账号未绑定邮箱,无法收到备份及通知。请回复[help]查看规则" return reply
def _handle_email(self, msg): open_id = msg.source email = msg.content user = (open_id, email) send_flag = False db = DbOperator() success, _ = db.find_user(open_id) if success: # 已有记录,更新绑定关系 result = db.update_user(user) else: # 无记录,添加绑定关系 result = db.add_user(user) send_flag = send_user_check_email(email) if result: reply = "你的账号成功绑定邮箱:" + email if send_flag: reply += "\n--------\n" reply += """你是初次绑定邮箱,我发送了一封确认邮件给您 请检查邮箱,确认能收到我的通知邮件 (注意垃圾箱!)""" else: # db 操作失败的日志 db 那边会记录 reply = "绑定邮箱:" + email + "失败!请联系管理员处理:[email protected]" return reply
def update_article_status(article_id, valid, backup_path=None, optionals={}): """Do jobs when article status changed. Args: article_id : int valid : bool backup_path : str optionals : dict maybe contains: "reason" shows why status changed "title" shows article title detected Returns: success: bool """ # 做点准备工作 db = DbOperator() success, item = db.find_article(article_id) if not success: # log it print("can't find article by article_id: " + str(article_id)) logger.error("can't find article by article_id: " + str(article_id)) return False open_id = item['open_id'] success, result = db.find_user(open_id) if success: email = result['email'] user_registered_flag = True else: logger.info("article & user no match: " + " ".join(map(str, [article_id, open_id]))) user_registered_flag = False # 根据状态变化执行处理 if not valid: # 当文章已不可访问 if user_registered_flag: # 用户有绑定邮箱,则执行通知 update_info = (article_id, optionals) _stop_watching(update_info, item, email, db) else: # 没有绑定邮箱,记错误日志 logger.error("article invalid, can't notify user. " + " ".join(map(str, [article_id, open_id]))) else: prev_status = item['status'] if prev_status == STATUS_NORMAL_OB: # 正常观察状态,无需额外操作 return True elif prev_status == STATUS_NEW_UNKNOWN: # 初次完成观察,制作备份 if backup_path is None: print("backup addr not valid") return False else: new_path = _backup_article(article_id, backup_path) try: title = optionals["title"] except KeyError: title = '' if new_path is None: return False article_info = (article_id, new_path, STATUS_NORMAL_OB, title) # 制作备份完成,状态更新 return db.update_article(article_info) else: print("unknown status!") logger.error("article status Error: " + " ".join(map(str, [article_id, open_id, prev_status]))) return True
def update_article_status(article_id, valid, backup_path=None, optionals={}): """Do jobs when article status changed. Args: article_id : int valid : bool backup_path : str optionals : dict maybe contains: "reason" shows why status changed "title" shows article title detected Returns: success: bool """ # 做点准备工作 db = DbOperator() success, item = db.find_article(article_id) if not success: # log it print("can't find article by article_id: " + str(article_id)) logger.error("can't find article by article_id: " + str(article_id)) return False URL = item['URL'] open_id = item['open_id'] success, result = db.find_user(open_id) if not success: logger.error("article & user no match: " + " ".join(map(str, [article_id, open_id]))) return False email = result['email'] # 开始更新数据库 if not valid: # 当文章已不可访问 if item['backup_addr'] == FAKE_PATH_PLACE_HOLDER: backup_addr = None else: backup_addr = item['backup_addr'] try: notify_user(email, URL, backup_addr) item['status'] = reason2status[optionals["reason"]] db.archive_article(item) except: logger.error("update_article_status ERROR") return False logger.info("article expired, move to archive: " + " ".join(map(str, [article_id, URL]))) else: prev_status = item['status'] if prev_status == STATUS_NORMAL_OB: # 正常观察状态,无需额外操作 return True elif prev_status == STATUS_NEW_UNKNOWN: # 初次完成观察,制作备份 if backup_path == None: print("backup addr not valid") return False else: new_path = _backup_article(article_id, backup_path) if new_path == None: return False article_info = (article_id, new_path, STATUS_NORMAL_OB) # 制作备份完成,状态更新 return db.update_article(article_info) else: print("unknown status!") logger.error("article status Error: " + " ".join(map(str, [article_id, open_id, prev_status]))) return True