def parse_content(alias, content): content = remove_control_characters(content) content = content.replace('>', '>').replace('<', '<')\ .replace('&', '&').replace('<br/>', '').encode('utf-8') try: dom = ElementTree.fromstring(content) except ElementTree.ParseError: logger.error('parse error') logger.error(content) return data = [] for item in dom.iter('item'): title = item.findtext('title') url = item.findtext('url') # cover = item.findtext('cover') # pub_time = item.findtext('pub_time') data.append((alias, title, url)) if data: try: DB.executemany( "INSERT INTO blog (NAME,TITLE,URL) VALUES (?, ?, ?)", data) except sqlite3.IntegrityError: logger.warn(u'%s 的 %s: %s 重复记录' % (alias, data[0][1], data[0][2])) except sqlite3.OperationalError: logger.error(u'%s 的 %s: %s 记录失败' % (alias, data[0][1], data[0][2])) else: title = dom.findtext('appmsg/title') url = dom.findtext('appmsg/url') # cover = dom.findtext('appmsg/thumburl') # pub_time = str(time.time()).split('.')[0] try: DB.execute( "INSERT INTO blog (NAME,TITLE,URL) VALUES (?, ?, ?)", (alias, title, url)) except sqlite3.IntegrityError: logger.warn(u'%s 的 %s: %s 重复记录' % (alias, title, url)) except sqlite3.OperationalError: logger.error(u'%s 的 %s: %s 记录失败' % (alias, title, url)) try: DB.commit() logger.info(u'commit success') except sqlite3.OperationalError as e: logger.warn(u'commit error: %s' % str(e))
def render_GET(request): def get_arg(k): return request.args.get(k, [None])[0] res = {"status": False, "msg": ""} path = request.path if path.startswith('/v1/wxspider/qrcode/'): name = path.rsplit('qrcode/', 1)[-1] _client = CLIENTS.get(name) if _client: if _client.is_online(): res['status'] = False res['msg'] = u'已经登录' else: res = u'''<!DOCTYPE html><html><head><title>%s二维码</title></head> <body><img style="width:276px;height:276px;" src="/v1/wxspider/img/%s"></body></html>'''\ % (_client.client_name, _client.client_name) res = res.encode('utf-8') else: res['status'] = False res['msg'] = u'没找到客户端' elif path.startswith('/v1/wxspider/img/'): name = path.rsplit('img/', 1)[-1] _client = CLIENTS.get(name) if _client: if _client.is_online(): res['status'] = False res['msg'] = u'已经登录' else: with open(_client.qrcode_file, 'rb') as f: res = f.read() else: res['status'] = False res['msg'] = u'没找到客户端' elif path.startswith('/v1/wxspider/manage'): action = get_arg('action') _name = get_arg('clientName') if action == 'list': res['status'] = True res['msg'] = {} for _client in CLIENTS.itervalues(): res['msg'][_client.client_name] = _client.readable_status() elif action == 'add': if _name and _name not in CLIENTS: CLIENTS[_name] = WxClient(_name) DB.execute("INSERT INTO client (NAME) VALUES (?)", (_name, )) DB.commit() CLIENTS[_name].run() res['status'] = True res['msg'] = u'添加成功,请<a href="/v1/wxspider/qrcode/%s">点击</a>扫描二维码登录' % _name else: res['msg'] = u'名称未填写或冲突' elif action == 'del': if _name and _name in CLIENTS: CLIENTS[_name].stop() CLIENTS.pop(_name) DB.execute('delete from client where name=?', (_name, )) DB.commit() logger.info(u'移除客户端:%s...' % _name) res['status'] = True res['msg'] = u'删除成功' else: res['msg'] = u'名称不存在' elif action == 'reset': if _name and _name in CLIENTS: CLIENTS[_name].stop() res['status'] = True else: res['msg'] = u'名称不存在' elif path.startswith('/v1/wxspider/members/'): name = path.rsplit('members/', 1)[-1] mems = [] members = CLIENTS[name].members.copy() for k, v in members.iteritems(): mems.append({ "id": k, "Alias": v.get("Alias", ""), "NickName": v.get("NickName", ""), "RemarkName": v.get("RemarkName", "") }) res['msg'] = mems res['status'] = True if isinstance(res, dict): request.setHeader("Content-Type", "application/json;charset=UTF-8") res = json.dumps(res) elif path.startswith('/v1/wxspider/img/'): request.setHeader('Content-Type', 'image/jpeg') else: request.setHeader('Content-Type', 'text/html') return res
def init_clients(): cur = DB.execute('select * from client') for name, proxy, t in cur: CLIENTS[name] = WxClient(name)
# coding=utf-8 import datetime from conf import DB if __name__ == '__main__': stamp = datetime.datetime.now() - datetime.timedelta(days=3) DB.execute("delete from blog where save_time < ? ", (stamp.strftime('%Y-%m-%d %H:%M:%S'), )) DB.commit()