class Dirs(object): log_name = 'temp.log' # 临时日志 def __init__(self, path=None): self.path = path if path else config['path'] self.bak_path = config['bak'] self.log_path = config['log'] self.dir_files = DirFiles(self.bak_path) self.logs = Logs(config['log']) self.dir_files.create_dirs(self.log_path) self.year_month = YearMonth(config['start'], config['end']) self.current_month = config['current_month'] self.count = config['count'] self.disable_date = config['disable_date'] # log self.check_log = self.dir_files.get_check_path( self.log_path, self.year_month.create_date(status=1) + '_checked.log') self.hot_log = self.dir_files.get_check_path(self.log_path, 'hot.log') self.checked_lists = self.logs.read_log( self.check_log) if self.dir_files.check_file( self.check_log) else [] self.hot_lists = self.logs.read_log( self.hot_log) if self.dir_files.check_file(self.hot_log) else [] # 鉴黄sdk self.appid = config['appid'] self.secret_id = config['secret_id'] self.secret_key = config['secret_key'] self.sdk = SDK(self.appid, self.secret_id, self.secret_key) # 企业微信 self.weixin = WeiXin( corpid=config['weixin']['corpid'], secrect=config['weixin']['secrect'], agentid=config['weixin']['agentid'], touser=config['weixin']['touser'], product=config['product'], toparty=config['weixin']['toparty'], ) def filter_wait_check_img(self, path): filename = self.dir_files.get_basename(path) t = self.year_month.get_current_time() if filename in self.hot_lists: self.dir_files.cp_img(path, t) self.weixin.send(path + "\r\n") return False if filename not in self.checked_lists: self.checked_lists.append(filename) self.logs.write_log(self.check_log, filename) return True else: return False def add_hot_img(self, filename): if filename not in self.hot_lists: self.hot_lists.append(filename) # print(self.hot_lists) # 开始图片鉴别 def check_img(self, lists=None): length = len(lists) if lists else 0 if length == 0: return print(length) num = length // self.count + 2 # 保证所有图片都能被检测 for j in range(1, num): start = (j - 1) * self.count end = j * self.count wait_lists = list( filter(self.filter_wait_check_img, lists[start:end])) # print(wait_lists) if len(wait_lists) == 0: continue res = self.sdk.send(wait_lists) if res is False: continue self.process_http_result(res) # 分析处理检测结果 def process_http_result(self, res): try: for line in res: if line['code'] != 0: continue t = self.year_month.get_current_time() if line['data']['result'] == 0: mes = "{0} {1} {2}".format(t, '否', line) else: mes = "{0} {1} {2}".format(t, '是', line) self.weixin.send(line['filename'] + "\r\n") self.dir_files.cp_img(line['filename'], t) self.dir_files.cp_img(line['filename'].replace('/v/', '/'), t) # 鉴别为黄图单独写入文件 self.add_hot_img( self.dir_files.get_basename(line['filename'])) self.logs.write_log( self.hot_log, self.dir_files.get_basename(line['filename'])) self.logs.write_log(self.log_name, mes) except Exception as e: print('error', e) # 主程序 def main(self): if self.disable_date == 1: current_year_month = self.year_month.create_date(status=1) self.log_name = self.logs.create_log_name(current_year_month + '.log') for path in self.path: lists = self.dir_files.walk_dir(path) self.check_img(lists) else: if self.current_month == 1: year_months = [self.year_month.create_date()] else: year_months = self.year_month.get_start_end() if year_months is False or len(year_months) == 0: return for path in self.path: for year_month in year_months: self.log_name = self.logs.create_log_name( ''.join(year_month) + '.log') check_path = self.dir_files.get_check_path( path, ''.join(year_month)) if self.dir_files.check_dir(check_path) is False: check_path = self.dir_files.get_check_path( path, '/'.join(year_month)) lists = self.dir_files.walk_dir(check_path) self.check_img(lists)