def test(): error = None content = None show = False form = TestForm() if form.validate_on_submit(): try: url = request.form.get('url', None) selector_type = request.form.get('selector_type', None) selector = request.form.get('selector', None) is_chrome = request.form.get('is_chrome', None) regular_expression = request.form.get('regular_expression', None) headers = request.form.get('headers', None) if is_chrome == 'yes': show = True content = get_content(url, is_chrome, selector_type, selector, regular_expression, headers, debug=True) except ValidationError: pass except Exception as e: error = repr(e) return render_template('test.html', error=error, form=form, content=content, show=show)
def monitor(id, type): with app.app_context(): status = '成功执行但未监测到变化' try: if type == 'html': task = Task.query.filter_by(id=id).first() url = task.url selector_type = task.selector_type selector = task.selector is_chrome = task.is_chrome regular_expression = task.regular_expression mail = task.mail wechat = task.wechat name = task.name rule = task.rule headers = task.headers last = Content.query.filter_by(task_id=id, task_type=type).first() if not last: last = Content(id) last_content = last.content content = get_content(url, is_chrome, selector_type, selector, regular_expression, headers) if is_changed(rule, content, last_content): msg = wraper_msg(content, url) send_message(msg, name, mail, wechat) last.content = content db.session.add(last) db.session.commit() status = '监测到变化,最新值:' + content elif type == 'rss': rss_task = RSSTask.query.filter_by(id=id).first() url = rss_task.url name = rss_task.name mail = rss_task.mail wechat = rss_task.wechat last = Content.query.filter_by(task_id=id, task_type=type).first() if not last: last = Content(id, 'rss') last_guid = last.content item = get_rss_content(url) if item['guid'] != last_guid: content = wraper_rss_msg(item) send_message(content, name, mail, wechat) last.content = item['guid'] db.session.add(last) db.session.commit() status = '监测到变化,最新值:' + item['title'] except FunctionTimedOut: logger.error(traceback.format_exc()) status = '解析RSS超时' except Exception as e: logger.error(traceback.format_exc()) status = repr(e) task_status = TaskStatus.query.filter_by(task_id=id, task_type=type).first() task_status.last_run = datetime.now() task_status.last_status = status db.session.add(task_status) db.session.commit()
def monitor(id, type): with app.app_context(): status = '' global_content = None try: if type == 'html': task = Task.query.filter_by(id=id).first() url = task.url selector_type = task.selector_type selector = task.selector is_chrome = task.is_chrome regular_expression = task.regular_expression mail = task.mail wechat = task.wechat pushover = task.pushover name = task.name rule = task.rule headers = task.headers last = Content.query.filter_by(task_id=id, task_type=type).first() if not last: last = Content(id) last_content = last.content content = get_content(url, is_chrome, selector_type, selector, regular_expression, headers) global_content = content status_code = is_changed(rule, content, last_content) logger.info( 'rule: {}, content: {}, last_content: {}, status_code: {}'. format(rule, content, last_content, status_code)) if status_code == 1: status = '监测到变化,但未命中规则,最新值为{}'.format(content) last.content = content db.session.add(last) db.session.commit() elif status_code == 2: status = '监测到变化,且命中规则,最新值为{}'.format(content) msg = wraper_msg(content, url) send_message(msg, name, mail, wechat, pushover) last.content = content db.session.add(last) db.session.commit() elif status_code == 3: status = '监测到变化,最新值为{}'.format(content) msg = wraper_msg(content, url) send_message(msg, name, mail, wechat, pushover) last.content = content db.session.add(last) db.session.commit() elif status_code == 0: status = '成功执行但未监测到变化,当前值为{}'.format(content) elif type == 'rss': rss_task = RSSTask.query.filter_by(id=id).first() url = rss_task.url name = rss_task.name mail = rss_task.mail wechat = rss_task.wechat pushover = rss_task.pushover last = Content.query.filter_by(task_id=id, task_type=type).first() if not last: last = Content(id, 'rss') last_guid = last.content item = get_rss_content(url) global_content = item['guid'] if item['guid'] != last_guid: content = wraper_rss_msg(item) send_message(content, name, mail, wechat, pushover) last.content = item['guid'] db.session.add(last) db.session.commit() status = '监测到变化,最新值:' + item['guid'] status = '成功执行但未监测到变化,当前值为{}'.format(last_guid) except FunctionTimedOut: logger.error(traceback.format_exc()) status = '解析RSS超时' except PartNotificationError as e: logger.error(traceback.format_exc()) status = repr(e) last.content = global_content db.session.add(last) db.session.commit() except Exception as e: logger.error(traceback.format_exc()) status = repr(e) task_status = TaskStatus.query.filter_by(task_id=id, task_type=type).first() task_status.last_run = datetime.now() task_status.last_status = status db.session.add(task_status) db.session.commit()