def deploy(): """ 部署模式,不发送新通知,仅发送部署通知邮件 """ print('deploying') header={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'} if os.path.exists('notifier.log') == False: env_build.create_log() if os.path.exists('TJ_notice.db') == False: env_build.create_database() if os.path.exists('TJ') == False: env_build.create_TJID() if os.path.exists('mail') == False: env_build.create_mail() s=requests.session() try: login(header,s) except: env_build.write_to_log('failed to log in 4m3') exit(3) note_dict = get_table(header,s) notice_list = go.act_with_database(note_dict) get_detail(header,s,notice_list) ex_SSE.deploy_SSE() ex_FAO.deploy_FAO() go.send_to_user({'1':'deploy succeed'},{'1':'begin service...'}) s.close() print('deploy succeed!')
def run_service(): """ 正常运行模式 """ header={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'} while True: if os.path.exists('notifier.log') == False: env_build.create_log() if os.path.exists('TJ_notice.db') == False: env_build.create_database() if os.path.exists('TJ') == False: env_build.create_TJID() if os.path.exists('mail') == False: env_build.create_mail() now_hour = datetime.datetime.utcnow().hour if now_hour is 4 or now_hour is 10: # 北京时间12点和18点 s=requests.session() try: login(header,s) except: env_build.write_to_log('failed to log in 4m3') exit(3) note_dict = get_table(header,s) notice_list = go.act_with_database(note_dict) detail_dict = get_detail(header,s,notice_list) go.send_to_user(note_dict,detail_dict,'4m3') ex_SSE.get_SSE_run() ex_FAO.get_FAO_run() time.sleep(3700) else: time.sleep(100)
def run_now(): """ 立刻检查4m3并将新通知通过邮件发送,仅执行一次 """ header={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'} if os.path.exists('notifier.log') == False: env_build.create_log() if os.path.exists('TJ_notice.db') == False: env_build.create_database() if os.path.exists('TJ') == False: env_build.create_TJID() if os.path.exists('mail') == False: env_build.create_mail() s=requests.session() print('running...') try: login(header,s) except: env_build.write_to_log('failed to log in 4m3') exit(3) note_dict = get_table(header,s) notice_list = go.act_with_database(note_dict) detail_dict = get_detail(header,s,notice_list) go.send_to_user(note_dict,detail_dict,'4m3') ex_SSE.get_SSE_run() ex_FAO.get_FAO_run() print('succeed!')
def send_to_user(note_dict, detail_dict, note_type='TJ_notifier'): """ 向每个用户发送通知邮件 """ """ note_dict是一个id-标题的字典,detail_dict是一个id-正文的字典,note_type指代通知的来源 """ conn = sqlite3.connect('TJ_notice.db') cursor = conn.cursor() cursor.execute('select * from user') result = cursor.fetchall() with open('mail', 'r') as fp: host_address = fp.readline().strip('\n') password = fp.readline().strip('\n') for key in detail_dict: title = '【{}】'.format(note_type) + note_dict[key] body = detail_dict[key] mail_list = [] for mail_address in result: mail_list.append(mail_address[0]) try: mail.sendMail(host_address, password, mail_list, title, body) env_build.write_to_log('send a mail {}'.format(note_dict[key])) except: env_build.write_to_log('failed to send a mail {}'.format( note_dict[key])) cursor.close() conn.commit() conn.close()
def act_with_database(note_dict): """ 连接数据库并查询通知 """ """ 参数是一个id-标题的字典,返回值是包含所有新通知id的list """ conn = sqlite3.connect('TJ_notice.db') cursor = conn.cursor() notice_list = [] for key in note_dict: content = note_dict[key] cursor.execute('select * from notice where id=?', (key, )) result = cursor.fetchall() if len(result) == 0: # 未查询到结果 cursor.execute('insert into notice (id, title) values (?, ?)', (key, content)) print('insert one tuple : {}'.format(key)) notice_list.append(key) env_build.write_to_log('find new notification: {}'.format( note_dict[key])) cursor.close() conn.commit() conn.close() return notice_list