def txt_last_id(user_id): cursor = connection.cursor() cursor.execute('select id from user_note where user_id=%s and state>=%s order by view_time desc limit 1',( user_id, USER_NOTE.DEFAULT)) r = cursor.fetchone() if r: return r[0] return 0
def url_id_list_by_user_id(user_id, limit, offset): cursor = connection.cursor() cursor.execute( 'select url_id from user_note where state>=%s and user_id = %s ' 'order by view_time DESC limit %s offset %s', (USER_NOTE.DEFAULT, user_id, limit, offset)) return [str(i[0]) for i in cursor]
def txt_hide(user_id, url_id): cursor = connection.cursor() cursor.execute('select state from user_note where url_id=%s and user_id=%s',(url_id, user_id)) r = cursor.fetchone() cursor.execute('update user_note set state=%s where url_id=%s and user_id=%s',(USER_NOTE.RM,url_id, user_id)) if r and r[0] > USER_NOTE.RM: _mc_flush(user_id)
def txt_view_id_state(user_id, url_id): cursor = connection.cursor() cursor.execute( 'select id,state from user_note where url_id=%s and user_id=%s' % (url_id, user_id)) r = cursor.fetchone() return r
def url_by_id(id): cursor = connection.cursor() cursor.execute( 'select url from url where id=%s', id ) url = cursor.fetchone() return url[0] if url else None
def url_id_list_by_user_id(user_id, limit, offset): cursor = connection.cursor() cursor.execute( 'select url_id from user_note where state>=%s and user_id = %s ' 'order by view_time DESC limit %s offset %s', (USER_NOTE.DEFAULT,user_id, limit, offset) ) return [str(i[0]) for i in cursor]
def user_id_by_email(email): cursor = connection.cursor() cursor.execute('select id from account where email=%s',email) user_id = cursor.fetchone() if user_id: user_id = user_id[0] else: user_id = 0 return user_id
def txt_last_id(user_id): cursor = connection.cursor() cursor.execute( 'select id from user_note where user_id=%s and state>=%s order by view_time desc limit 1' % (user_id, USER_NOTE.DEFAULT)) r = cursor.fetchone() if r: return r[0] return 0
def url_new(url): url = str(url.lower()) cursor = connection.cursor() cursor.execute('select id from url where url=%s', url) id = cursor.fetchone() if id: return id[0] else: cursor.execute('insert into url (url) values(%s)', url) return cursor.lastrowid
def url_new(url): url = str(url.lower()) cursor = connection.cursor() cursor.execute('select id from url where url="%s"' % url) id = cursor.fetchone() if id: return id[0] else: cursor.execute('insert into url (url) values("%s")' % url) return cursor.lastrowid
def txt_hide(user_id, url_id): cursor = connection.cursor() cursor.execute( 'select state from user_note where url_id=%s and user_id=%s' % (url_id, user_id)) r = cursor.fetchone() cursor.execute( 'update user_note set state=%s where url_id=%s and user_id=%s' % (USER_NOTE.RM, url_id, user_id)) if r and r[0] > USER_NOTE.RM: _mc_flush(user_id)
def account_new(name, email): name, email = map(string.strip, (name, email)) user_id = user_id_by_email(email) if not user_id: cursor = connection.cursor() if email: cursor.execute( '''insert into `account` (name, email) values (%s,%s)''', (name, email) ) user_id = user_id_by_email(email) return user_id
def txt_log_save(user_id, url_id, txt, txt_old): now = int(time.time()) if txt_old and now - txt_log_last_time(url_id) > 600: cursor = connection.cursor() cursor.execute( 'insert into txt_log (url_id, user_id, time) values (%s,%s,%s)' % (url_id, user_id, int(time.time()))) id = cursor.lastrowid kv.set('TxtLog:%s' % id, txt_old) diff = diff_get(txt_old, txt) kv.set('TxtDiff:%s' % id, diff) kv.set(KV_TXT_SAVE_TIME + str(url_id), now)
def txt_log_save(user_id, url_id, txt, txt_old): now = int(time.time()) if txt_old and now - txt_log_last_time(url_id) > 600: cursor = connection.cursor() cursor.execute( 'insert into txt_log (url_id, user_id, time) values (%s,%s,%s)', (url_id, user_id, int(time.time())) ) id = cursor.lastrowid kv.set('TxtLog:%s' % id, txt_old) diff = diff_get(txt_old, txt) kv.set('TxtDiff:%s' % id, diff) kv.set(KV_TXT_SAVE_TIME+str(url_id), now)
def txt_touch(user_id, url_id): if not user_id:return now = int(time.time()) cursor = connection.cursor() r = txt_view_id_state(user_id, url_id) if r: id, state = r if state < USER_NOTE.DEFAULT or id != txt_last_id(user_id): cursor.execute('update user_note set view_time=%s , state=%s where id=%s',(now,USER_NOTE.DEFAULT, id)) mc_url_id_list_by_user_id.delete(user_id) mc_txt_last_id.set(user_id, url_id) if state < USER_NOTE.DEFAULT: history_count.delete(user_id) else: cursor.execute( 'insert into user_note (user_id, url_id, view_time, state) values ' '(%s,%s,%s,%s) ON DUPLICATE KEY UPDATE view_time=%s, state=%s', (user_id, url_id, now, USER_NOTE.DEFAULT, now, USER_NOTE.DEFAULT) ) history_count.delete(user_id) mc_url_id_list_by_user_id.delete(user_id) mc_txt_last_id.set(user_id, url_id)
def txt_touch(user_id, url_id): if not user_id: return now = int(time.time()) cursor = connection.cursor() r = txt_view_id_state(user_id, url_id) if r: id, state = r if state < USER_NOTE.DEFAULT or id != txt_last_id(user_id): cursor.execute( 'update user_note set view_time="%s" , state=%s where id=%s' % (now, USER_NOTE.DEFAULT, id)) mc_url_id_list_by_user_id.delete(user_id) mc_txt_last_id.set(user_id, url_id) if state < USER_NOTE.DEFAULT: history_count.delete(user_id) else: cursor.execute( 'insert into user_note (user_id, url_id, view_time, state) values ' '(%s,%s,"%s",%s) ON DUPLICATE KEY UPDATE view_time="%s", state=%s' % (user_id, url_id, now, USER_NOTE.DEFAULT, now, USER_NOTE.DEFAULT)) history_count.delete(user_id) mc_url_id_list_by_user_id.delete(user_id) mc_txt_last_id.set(user_id, url_id)
def txt_view_id_state(user_id, url_id): cursor = connection.cursor() cursor.execute('select id,state from user_note where url_id=%s and user_id=%s',(url_id, user_id)) r = cursor.fetchone() return r
def user_mail(user_id): cursor = connection.cursor() cursor.execute('select email from account where id=%s', user_id) r = cursor.fetchone() if r: return r[0]
def user_by_id(user_id): cursor = connection.cursor() cursor.execute('select name, email from account where id=%s', user_id) user = cursor.fetchone() return user
def url_by_id(id): cursor = connection.cursor() cursor.execute('select url from url where id=%s', id) url = cursor.fetchone() return url[0] if url else None
def _history_count(user_id): cursor = connection.cursor() cursor.execute( 'select count(1) from user_note where user_id = %s and state=%s', (user_id, USER_NOTE.DEFAULT) ) return cursor.fetchone()[0]
cursor.execute( 'select url_id from user_note where state>=%s and user_id = %s ' 'order by view_time DESC limit %s offset %s', (USER_NOTE.DEFAULT,user_id, limit, offset) ) return [str(i[0]) for i in cursor] def url_id_time_list_by_user_id(user_id, limit, offset): id_list = url_id_list_by_user_id(user_id, limit, offset) time_dict = kv.get_multi(id_list,key_prefix=KV_TXT_SAVE_TIME) result = [] for i in map(str,id_list): _time = time_dict.get(i,0) if not _time: _time = time() kv.set(KV_TXT_SAVE_TIME+i,_time) result.append([i,_time]) return result if __name__ == "__main__": pass cursor = connection.cursor() cursor.execute( 'select url_id,state from user_note where state>=%s and user_id = %s ' 'order by view_time DESC limit %s offset %s', (USER_NOTE.DEFAULT,1, 13,0) ) print cursor.fetchall()
def _history_count(user_id): cursor = connection.cursor() cursor.execute( 'select count(1) from user_note where user_id = %s and state=%s', (user_id, USER_NOTE.DEFAULT)) return cursor.fetchone()[0]
cursor.execute( 'select url_id from user_note where state>=%s and user_id = %s ' 'order by view_time DESC limit %s offset %s', (USER_NOTE.DEFAULT, user_id, limit, offset)) return [str(i[0]) for i in cursor] def url_id_time_list_by_user_id(user_id, limit, offset): id_list = url_id_list_by_user_id(user_id, limit, offset) time_dict = kv.get_multi(id_list, key_prefix=KV_TXT_SAVE_TIME) result = [] for i in map(str, id_list): _time = time_dict.get(i, 0) if not _time: _time = time() kv.set(KV_TXT_SAVE_TIME + i, _time) result.append([i, _time]) return result if __name__ == "__main__": pass cursor = connection.cursor() cursor.execute( 'select url_id,state from user_note where state>=%s and user_id = %s ' 'order by view_time DESC limit %s offset %s', (USER_NOTE.DEFAULT, 1, 13, 0)) print cursor.fetchall()