def _get_account_info(self, data_type, with_prefix=True): """ 返回用户该类型的端口密码 """ type_name = {0: "public_", 1: "login_", 2: "donate_"} prefix = with_prefix and type_name[data_type] or "" data = {} if data_type <= 1: data = db.get( "select port as %sport ,password as %spassword from ssserver where state>0 and type=%s order by used limit 1;" % (prefix, prefix, data_type)) if data: db.execute("update ssserver set used=used+1 where port=%s", data["%sport" % prefix]) elif data_type == 2: data = db.get( "select port as %sport ,password as %spassword from ssserver where state>0 and type=2 and owner=\"%s\" limit 1" % (prefix, prefix, self.get_current_user())) if not data: """ 尝试分配 """ db.execute( "update ssserver set owner=%s where owner is null and type=2 limit 1", self.get_current_user()) data = db.get( "select port as %sport ,password as %spassword from ssserver where state>0 and type=2 and owner=\"%s\" " % (prefix, prefix, self.get_current_user())) return data
def post(self): roomchannel = self.get_secure_cookie("roomId") userid = str(self.get_secure_cookie("userid")) user = db.get("SELECT username FROM user WHERE id=%s", int(userid)) message = str(self.get_argument("message")) msg_type = int(self.get_argument("type")) createtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') data = json_encode({ 'username': user.username, 'msg': message, "userId": userid, "bglong": 0, "type": msg_type, "created_time": createtime }) db.execute( "insert into message(userid, msg, roomid, created_time, type) values (%s, %s, %s, %s, %s)", userid, message, roomchannel, createtime, msg_type) #收到将消息publish到Redis #print data redis_client.connect() redis_client.publish(roomchannel, data) self.write(json_encode({'result': True})) self.finish()
def index(): ''' :desc:重构索引 ''' logger.info('重构索引...') start_time = time() resource_count = db.get('select count(*) as count from resource')['count'] logger.info('收录数据 {} 条'.format(resource_count)) sql = 'select * from resource limit 10000 offset %s' counter = 0 writer = IndexWriter(INDEX_DIR, ANALYZER, True, IndexWriter.MaxFieldLength.UNLIMITED) for offset in range(0, resource_count, 10000): items = db.query(sql, offset) for item in items: doc = Document() doc.add( Field('title', item['title'], Field.Store.YES, Field.Index.ANALYZED)) doc.add( Field('url', item['url'], Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('feed_time', str(item['feed_time']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('feed_username', item['feed_username'], Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('feed_user_uk', str(item['feed_user_uk']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('origin', item['origin'], Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('size', str(item['size']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('v_cnt', str(item['v_cnt']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('d_cnt', str(item['d_cnt']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add( Field('t_cnt', str(item['t_cnt']), Field.Store.YES, Field.Index.NOT_ANALYZED)) writer.addDocument(doc) counter += 1 if counter % 10000 == 0: logger.info('计数 {} / {}'.format(counter, resource_count)) writer.close() cost_time = int(time() - start_time) logger.info('重构索引完毕,耗时 {}'.format(cost_time, )) index_time = int(time()) db.insert( 'insert into index_log (index_time, count, cost_time) values (%s, %s, %s)', index_time, resource_count, cost_time)
def get(self): template_data = self.get_user_info() self.update_err_code(template_data) if self.get_argument("log_out", False): self.clear_cookie("user") self.clear_cookie("user_type") self.redirect("/") return data = db.get( "select used_flow,total_flow,donate_money,create_time from user where user=%s limit 1", self.current_user) my_log.info("user page : username:%s" % (self.current_user, )) template_data.update(data) self.render("user.html", **template_data)
def get(self, roomId): userid = str(self.get_secure_cookie("userid")) if not db.query("SELECT * FROM room WHERE id=%s", roomId): return self.redirect("/index") self.set_secure_cookie("roomId", roomId) room = db.get("SELECT roomname FROM room WHERE id=%s", int(roomId)) latelyRoom = db.query( "SELECT room.id, roomname, created_time, user.username " "FROM room, user WHERE user.id=room.owner_id " "AND room.id IN (SELECT roomid FROM message WHERE message.userid=%s " "GROUP BY roomid )", userid) owner_msg = db.query( "SELECT user.id, user.username, msg, created_time, type FROM message, user " "WHERE user.id=message.userid AND have_read=0 AND roomid=%s " "ORDER BY created_time;", roomId) room_msg = [] userid_group = [] user_group = [] for msg_item in owner_msg: msg_group = {} if msg_item.type == 1: continue if msg_item.id not in userid_group: userid_group.append(msg_item.id) user_group.append({ "username": msg_item.username, "userid": msg_item.id }) msg_group["username"] = msg_item.username msg_group["created_time"] = msg_item.created_time.strftime( '%Y-%m-%d %H:%M:%S') msg_group["msg"] = msg_item.msg msg_group["type"] = str(msg_item.type) if str(msg_item.id) == str(userid): msg_group["belong"] = 1 else: msg_group["belong"] = 0 room_msg.append(msg_group) self.render("chatroom.html", room=room, msg=room_msg, users=user_group, latelyRoom=latelyRoom)
def post(self): user = self.get_argument("user") password = self.get_argument("password").lower() err_code = 0 user_type = 0 if EMAIL_PAT.match(user) or PHONE_PAT.match(user): # password=hashlib.sha256(password).hexdigest() if not re.match("^\w+$", password): err_code = 1003 # old user elif db.query("select * from user where user=%s", user): result = db.get( "select type from user where user=%s and password=%s limit 1", user, password) if result: user_type = result["type"] my_log.info("login page : login success user:%s" % user) else: err_code = 1001 my_log.info("login page : password error user:%s" % user) # new user else: db.execute( "insert into user(user,password,create_time,total_flow) values (%s,%s,current_timestamp,0)", user, password) user_type = 1 err_code = False my_log.info("login page : new user:%s" % user) else: err_code = 1000 my_log.info("login page : username error user:%s" % user) if not err_code: user = self.set_secure_cookie("user", self.get_argument("user")) user_type = self.set_secure_cookie("user_type", str(user_type)) self.redirect("/") else: self.redirect("/?err_code=%s" % err_code)
def post(self): roomname = self.get_argument("roomname") password = self.get_argument("password") if roomname: room = db.query("SELECT * FROM room WHERE roomname=%s", roomname) if room: return self.write(json_encode({'result': 2})) # try: createtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') roomId = db.execute( "INSERT INTO room (roomname, created_time, owner_id) VALUES (%s, %s, %s)", roomname, createtime, self.get_secure_cookie("userid")) print roomId room = db.get( "SELECT room.id, roomname, created_time, username FROM room, user WHERE room.id=%s and room.owner_id=user.id", int(roomId)) roomInfo = {} roomInfo["roomId"] = room.id roomInfo["roomname"] = room.roomname roomInfo["created_time"] = room.created_time.strftime( '%Y-%m-%d %H:%M:%S') roomInfo["username"] = room.username return self.write(roomInfo)
def index(): ''' :desc:重构索引 ''' logger.info('重构索引...') start_time = time() resource_count = db.get('select count(*) as count from resource')['count'] logger.info('收录数据 {} 条'.format(resource_count)) sql = 'select * from resource limit 10000 offset %s' counter = 0 writer = IndexWriter(INDEX_DIR, ANALYZER, True, IndexWriter.MaxFieldLength.UNLIMITED) for offset in range(0, resource_count, 10000): items = db.query(sql, offset) for item in items: doc = Document() doc.add(Field('title', item['title'], Field.Store.YES, Field.Index.ANALYZED)) doc.add(Field('url', item['url'], Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('feed_time', str(item['feed_time']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('feed_username', item['feed_username'], Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('feed_user_uk', str(item['feed_user_uk']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('origin', item['origin'], Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('size', str(item['size']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('v_cnt', str(item['v_cnt']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('d_cnt', str(item['d_cnt']), Field.Store.YES, Field.Index.NOT_ANALYZED)) doc.add(Field('t_cnt', str(item['t_cnt']), Field.Store.YES, Field.Index.NOT_ANALYZED)) writer.addDocument(doc) counter += 1 if counter % 10000 == 0: logger.info('计数 {} / {}'.format(counter, resource_count)) writer.close() cost_time = int(time() - start_time) logger.info('重构索引完毕,耗时 {}'.format(cost_time,)) index_time = int(time()) db.insert('insert into index_log (index_time, count, cost_time) values (%s, %s, %s)', index_time, resource_count, cost_time)
def get_stocks(): stocks = db.get(constant.REDIS_DB_KEY) return get_json_data(stocks)[:constant.DATA_LIMIT]
def get_filter_result(search): search = search + '*' keys = db.keys(search.upper()) stocks = [get_json_data(db.get(key)) for key in keys] return stocks
def __init__(self): client = MongoClient(db.get("MONGODB_IP"), db.get("MONGODB_PORT")) client.admin.authenticate(db.get("USER"), db.get("PASS")) self.table = client.zlist.tasklist
def raw_get(cls, sql, *args): """执行原生的sql语句,分包装的sql查询结果,适用于联合查询, 仅返回一条数据""" result = conn.get(sql, *args) return result
def findone(cls, **kwargs): where = " AND ".join(["`{0}`=%s".format(k) for k in kwargs]) query = "SELECT * FROM `{table}` WHERE {where} LIMIT 1".format(table=cls.__table__, where=where) logger.info('the SQL is {0}'.format(query)) d = conn.get(query, *kwargs.values()) return cls(**d) if d else None
def getone(cls, pk): query = "SELECT * FROM `{table}` WHERE id=%s LIMIT 1".format(table=cls.__table__) logger.info('The SQL is {0}'.format(query)) result = conn.get(query, pk) return cls(**result) if result else None
def get_current_user(self): user_id = self.get_secure_cookie("userid") if not user_id: return None return db.get("SELECT * FROM user WHERE id=%s", int(user_id))