def oauth_rm_by_oauth_id(oauth_id): cursor = cursor_by_table('oauth_token') cursor_new = cursor_by_table('oauth_token_backup') cursor.execute('select id,app_id,zsite_id,token_key,token_secret from oauth_token where id=%s', oauth_id) item = cursor.fetchone() if item: cursor_new.execute('insert into oauth_token_backup (id,app_id,zsite_id,token_key,token_secret) values (%s,%s,%s,%s,%s)', item) cursor.execute('delete from oauth_token where id =%s', oauth_id)
def oauth_name_by_oauth_id(app_id, oauth_id): table = OAUTH2TABLE[app_id] cursor = cursor_by_table(table) cursor.execute('select name from %s where id=%%s'%table, oauth_id) r = cursor.fetchone() if r: return r[0]
def mq_server(): print 'mq_server on port %s' % MQ_PORT beanstalk = beanstalkc.Connection(host='localhost', port=MQ_PORT, parse_yaml=True) beanstalk.watch(MQ_USE) beanstalk.ignore('default') while True: job = beanstalk.reserve() #print job try: name, args, kwds = loads(job.body) except: job.delete() continue func = NAME2FUNC.get(name) #print name, args, kwds try: func(*args, **kwds) except: import traceback exc = traceback.format_exc() cursor = cursor_by_table('failed_mq') cursor.execute( 'insert into failed_mq (body,exc,func) values (%s,%s,%s)', (job.body, exc, name)) logging.error(exc) job.delete()
def user_session(user_id): s = user_session_by_db(user_id) if not s: u = UserSession.get_or_create(id=user_id) if u.value is None: s = urandom(12) u.value = s u.save() mc_user_session.set(user_id, s) cursor = cursor_by_table('user_login_time') cursor.execute( 'insert delayed into user_login_time (user_id, create_time) values (%s, %s)', (user_id, int(time())) ) cursor.connection.commit() return id_binary_encode(user_id, s)
def txt_new(id, txt): txt = txt.replace('\r\n', '\n').replace('\r', '\n').rstrip() t = Txt.get(id) if t is None: Txt(id=id, txt=txt).save() elif txt != t.txt: c = cursor_by_table('txt_history') c.execute( 'insert delayed into txt_history (rid,txt,create_time) values (%s,%s,%s)', (id, t.txt, int(time()))) c.connection.commit() t.txt = txt t.save() mc_flush(id, txt)
def txt_new(id, txt): txt = txt.replace('\r\n', '\n').replace('\r', '\n').rstrip() t = Txt.get(id) if t is None: Txt(id=id, txt=txt).save() elif txt != t.txt: c = cursor_by_table('txt_history') c.execute( 'insert delayed into txt_history (rid,txt,create_time) values (%s,%s,%s)', (id, t.txt, int(time())) ) c.connection.commit() t.txt = txt t.save() mc_flush(id, txt)
def user_session(user_id): s = user_session_by_db(user_id) if not s: u = UserSession.get_or_create(id=user_id) if u.value is None: s = urandom(12) u.value = s u.save() mc_user_session.set(user_id, s) cursor = cursor_by_table('user_login_time') cursor.execute( 'insert delayed into user_login_time (user_id, create_time) values (%s, %s)', (user_id, int(time()))) cursor.connection.commit() return id_binary_encode(user_id, s)
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import McCache from feed import feed_rt, feed_rt_rm, feed_rt_list, feed_rt_count from po import Po, po_new, po_word_new, po_note_new, po_rm from state import STATE_RM, STATE_ACTIVE from zsite import Zsite #from rank import Rank, rank_po_id_list, rank_new, rank_rm, rank_po_id_count, rank_id_by_po_id_to_id, _rank_mv from _db import cursor_by_table from cid import CID_REC cursor = cursor_by_table('feed') def po_show_new(po): feed_rt(0, po.id) def po_show_rm(po_id): feed_rt_rm(0, po_id) Po.where(user_id=0, rid=po_id, cid=CID_REC, state=STATE_ACTIVE).delete() def po_show_list(limit, offset): ids = feed_rt_list(0, limit, offset) li = Po.mc_get_list(ids) li = Po.mc_get_list(i.rid for i in li) Zsite.mc_bind(li, 'user', 'user_id') return li
#!/usr/bin/env python # -*- coding: utf-8 -*- from state import STATE_RM, STATE_ACTIVE from _db import McModel, McCache, cursor_by_table, McCacheA, McCacheM from mq import mq_client from zkit.feed_merge import MAXINT, merge_iter as _merge_iter from cid import CID_REC PAGE_LIMIT = 50 mc_feed_iter = McCacheM('FeedIter:%s') mc_feed_tuple = McCacheM('F%s') mc_feed_rt_id = McCache('R%s') cursor = cursor_by_table('feed') class Feed(McModel): pass def feed_new(id, zsite_id, cid, rid=0): cursor.execute( 'insert into feed (id, zsite_id, cid) values (%s,%s,%s) on duplicate key update id=id', (id, zsite_id, cid) ) cursor.connection.commit() mc_feed_iter.delete(zsite_id) return id def feed_rm(id):
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import cursor_by_table cursor = cursor_by_table('gid') def gid(): cursor.execute('insert into gid () values()') cursor.connection.commit() return cursor.lastrowid if __name__ == '__main__': print gid()
from model.state import STATE_PO_ZSITE_SHOW_THEN_REVIEW from zkit.feed_merge import MAXINT, merge_iter as _merge_iter from model.zsite import Zsite from model.po import Po from model.ico import ico_url_bind_with_default from model.cid import CID_WORD ID_SQL = 'select id from po where state>=%s and zsite_id=%%s and id<%%s order by id desc limit %s'%( STATE_PO_ZSITE_SHOW_THEN_REVIEW, PAGE_LIMIT ) def site_po_id_lastest(id): return po_id_list_by_zsite_id(id, 0, PAGE_LIMIT, 0) cursor = cursor_by_table('po') def feed_iter(zsite_id, start_id=MAXINT): if start_id == MAXINT: id_list = site_po_id_lastest(zsite_id) if id_list: for i in id_list: #print zsite_id, i yield i start_id = i else: return while True: cursor.execute(ID_SQL, (zsite_id, start_id)) c = cursor.fetchall() if not c:
def __init__(self, table, NULL=''): self.__table__ = table self.cursor = cursor_by_table(table) self.__mc_id__ = '%s.%%s' % table self.__mc_value__ = '-%s' % self.__mc_id__ self.NULL = NULL
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import McModel, McCache, cursor_by_table, McCacheA, McCacheM from zkit.algorithm.merge import imerge from state import STATE_ACTIVE from feed import PAGE_LIMIT mc_feed_user_dict = McCacheM('FeedUserDict.%s') mc_feed_po_iter = McCacheA('FeedPoIter.%s') mc_feed_po_dict = McCacheM('FeedPoDict.%s') cursor = cursor_by_table('po') FEED_PO_ID_LASTEST_SQL = 'select id from po where user_id=%%s and state=%s order by id desc limit %s' % ( STATE_ACTIVE, PAGE_LIMIT) FEED_PO_ID_ITER_SQL = 'select id from po where user_id=%%s and state=%s and id<%%s order by id desc limit %s' % ( STATE_ACTIVE, PAGE_LIMIT) @mc_feed_po_iter('{feed_id}') def feed_po_id_lastest(feed_id): cursor.execute(FEED_PO_ID_LASTEST_SQL, feed_id) return [i for i, in cursor.fetchall()] def feed_po_iter(zsite_id, start_id=None): if start_id is None: id_list = feed_po_id_lastest(zsite_id) if id_list: for i in id_list: yield i
class ReplyMixin(object): reply_cursor = cursor_by_table('reply') @mc_reply_zsite_id_list('{self.cid}_{self.id}') def reply_zsite_id_list(self): id_set = set() id_set.update(i.user_id for i in self.reply_list()) return list(id_set) def reply_new(self, user, txt, state=STATE_ACTIVE, create_time=None): from zsite import user_can_reply user_id = user.id cid = self.cid if cid not in (CID_SITE, CID_COM): if not user_can_reply(user): return if is_spammer(user_id): return txt = txt.rstrip() rid = self.id if not txt or is_same_post(user_id, cid, rid, txt, state): return id = gid() if not create_time: create_time = int(time()) txt_new(id, txt) cursor = self.reply_cursor cursor.execute( 'insert into reply (id,cid,create_time,state,rid,user_id) values (%s,%s,%s,%%s,%%s,%%s)' % ( id, cid, create_time, ), (state, rid, user_id)) cursor.connection.commit() mc_flush_reply_id_list(cid, rid) # key = '%s_%s' % (rid, user_id) # if mc_reply_in_1h.get(key) is None: # mq_buzz_po_reply_new(user_id, rid) # mc_reply_in_1h.set(key, True, 3600) return id @property @mc_reply_count('{self.cid}_{self.id}') def reply_count(self): cid = self.cid rid = self.id cursor = self.reply_cursor cursor.execute( 'select count(1) from reply where rid=%s and cid=%s and state>=%s', (rid, cid, STATE_SECRET)) r = cursor.fetchone() return r[0] def _reply_id_list(self, limit, offset, order): cursor = self.reply_cursor cid = self.cid rid = self.id sql = [ 'select id from reply where rid=%s and cid=%s', 'and state>=%s' % STATE_SECRET ] para = [rid, cid] sql.append(order) if limit: sql.append('limit %s') para.append(limit) if offset: sql.append('offset %s') para.append(offset) cursor.execute(' '.join(sql), para) return [i for i, in cursor] @property def reply_id_last(self): li = self.reply_id_list_reversed(1, 0) if li: return li[0] return 0 def reply_last(self): id = self.reply_id_last if id: return Reply.mc_get(id) @mc_reply_id_list_reversed('{self.cid}_{self.id}') def reply_id_list_reversed(self, limit=None, offset=None): return self._reply_id_list(limit, offset, 'order by id desc') @mc_reply_id_list('{self.cid}_{self.id}') def reply_id_list(self, limit=None, offset=None): return self._reply_id_list(limit, offset, 'order by id') def _reply_list(self, limit, offset, reply_id_list): from model.zsite import Zsite r = Reply.mc_get_list(reply_id_list(limit, offset)) txt_bind(r) Zsite.mc_bind(r, 'user', 'user_id') return r def reply_list_reversed(self, limit=None, offset=None): return self._reply_list(limit, offset, self.reply_id_list_reversed) def reply_list(self, limit=None, offset=None): return self._reply_list(limit, offset, self.reply_id_list)
from model.cid import CID_USER from zkit.mc_func import mc_func_get_dict_with_key_pattern follow_count_by_to_id = McNum(lambda to_id: Follow.where(to_id=to_id).count(), 'FollowCountByToId.%s') follow_count_by_from_id = McNum(lambda from_id: Follow.where(from_id=from_id).count(), 'FollowCountByFromId.%s') mc_follow_id_list_by_to_id = McLimitA('FollowIdListByToId.%s', 128) mc_follow_id_list_by_from_id_cid = McCacheA('FollowIdListByFromIdCid.%s') mc_follow_id_list_by_from_id = McCacheA('FollowIdListByFromId.%s') mc_follow_get = McCache('FollowGet<%s') mc_following_id_rank_tuple = McCacheM('FollowingIdRankTuple.%s') class Follow(Model): pass follow_cursor = cursor_by_table('follow') @mc_follow_id_list_by_to_id('{to_id}') def follow_id_list_by_to_id(to_id, limit, offset): follow_cursor.execute('select from_id from follow where to_id=%s order by id desc limit %s offset %s', (to_id, limit, offset)) return [i for i, in follow_cursor] @mc_follow_id_list_by_from_id('{from_id}') def follow_id_list_by_from_id(from_id): follow_cursor.execute('select to_id from follow where from_id=%s order by id desc', (from_id)) return [i for i, in follow_cursor] @mc_following_id_rank_tuple('{from_id}', ONE_DAY) def following_id_rank_tuple(from_id): from model.zsite_rank import zsite_rank id_list = follow_id_list_by_from_id(from_id)