def move_status(): STATUS_REDIS_KEY = "/status/text/%s" RAW_STATUS_REDIS_KEY = "/status/raw/%s" start = 3720000 limit = 100000 #r =db_conn.execute("select count(1) from status") #total = r.fetchone()[0] total = 4423725 print '----total status:', total sys.stdout.flush() ef = open("error.log", "a") #cf = open("cmd.txt", "w") while (start <= int(total)): f = open("./midfile.txt", "w") print '-------start ', start sys.stdout.flush() cursor = db_conn.execute( "select id from status order by id limit %s,%s", (start, limit)) rows = cursor.fetchall() for row in rows: text = mongo_conn.get(STATUS_REDIS_KEY % row[0]) raw = mongo_conn.get(RAW_STATUS_REDIS_KEY % row[0]) if text and raw: text = json_encode(text) if not isinstance( text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw db_conn.execute( '''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (row[0], text, raw)) db_conn.commit() start += limit
def move_status(): STATUS_REDIS_KEY = "/status/text/%s" RAW_STATUS_REDIS_KEY = "/status/raw/%s" start = 3720000 limit = 100000 #r =db_conn.execute("select count(1) from status") #total = r.fetchone()[0] total = 4423725 print '----total status:', total sys.stdout.flush() ef = open("error.log", "a") #cf = open("cmd.txt", "w") while (start <= int(total)): f = open("./midfile.txt", "w") print '-------start ', start sys.stdout.flush() cursor = db_conn.execute("select id from status order by id limit %s,%s", (start, limit)) rows = cursor.fetchall() for row in rows: text = mongo_conn.get(STATUS_REDIS_KEY % row[0]) raw = mongo_conn.get(RAW_STATUS_REDIS_KEY% row[0]) if text and raw: text = json_encode(text) if not isinstance(text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw db_conn.execute('''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (row[0], text, raw)) db_conn.commit() start += limit
def remove(self): cursor = db_conn.execute( """delete from sync_task where id=%s""", self.id) db_conn.commit() cursor and cursor.close() return None
def update_session(self, session_id): cursor = db_conn.execute( """update user set session_id=%s where id=%s""", (session_id, self.id)) cursor and cursor.close() db_conn.commit() User._clear_cache(self.id)
def remove(self): cursor = db_conn.cursor() cursor.execute("""delete from sync_task where id=%s""", self.id) db_conn.commit() cursor.close() return None
def add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute( """insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title), ) db_conn.commit() status_id = cursor.lastrowid if text is not None: mongo_conn.set(cls.STATUS_REDIS_KEY % status_id, json_encode(text)) if raw is not None: mongo_conn.set(cls.RAW_STATUS_REDIS_KEY % status_id, raw) cls._clear_cache(user_id, None, cate=category) status = cls.get(status_id) except IntegrityError: # log.warning("add status duplicated, ignore...") db_conn.rollback() finally: cursor and cursor.close() return status
def update_uid(self, uid): assert isinstance(uid, basestring) if self.id != self.uid: return False, "already_set" if uid == self.uid: return True, "same_with_old" if len(uid) > User.UID_MAX_LEN: return False, u"太长了,不能超过%s" %User.UID_MAX_LEN if len(uid) < User.UID_MIN_LEN: return False, u"太短了,不能小于%s" %User.UID_MIN_LEN uid_re = re.compile(User.UID_RE) if not uid_re.match(uid): return False, u"只能包括字母数字和.-_" uid = uid.lower() if uid in ["user", "pdf", "explore", "home", "visual", "settings", "admin", "past", "connect", "bind", "i", "notes", "note", "status", "share", "timeline", "post", "login", "logout", "sync", "about", "connect", "dev", "api", "thepast", "thepast.me", ]: return False, u"被系统占用了:)" try: cursor = db_conn.execute("""update user set uid=%s where id=%s""", (uid, self.id)) db_conn.commit() User._clear_cache(self.id) return True, u"设置成功" except IntegrityError: db_conn.rollback() return False, u"被别人占用了:)" finally: cursor and cursor.close() return False, "fail"
def add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute( """insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title)) status_id = cursor.lastrowid if status_id > 0: text = json_encode(text) if text is not None else "" raw = json_encode(raw) if raw is not None else "" RawStatus.set(status_id, text, raw) db_conn.commit() status = cls.get(status_id) except IntegrityError: log.warning( "add status duplicated, uniq key is %s:%s:%s, ignore..." % (origin_id, site, category)) db_conn.rollback() finally: cls._clear_cache(user_id, None, cate=category) cursor and cursor.close() return status
def delete_by_random_id(cls, random_id): db_conn.execute( """delete from confirmation where random_id=%s""", random_id, ) db_conn.commit()
def update_session(self, session_id): cursor = db_conn.cursor() cursor.execute("""update user set session_id=%s where id=%s""", (session_id, self.id)) cursor.close() db_conn.commit() User._clear_cache(self.id)
def update_uid(self, uid): assert isinstance(uid, basestring) if self.id != self.uid: return False, "already_set" if uid == self.uid: return True, "same_with_old" if len(uid) > User.UID_MAX_LEN: return False, u"太长了,不能超过%s" % User.UID_MAX_LEN if len(uid) < User.UID_MIN_LEN: return False, u"太短了,不能小于%s" % User.UID_MIN_LEN uid_re = re.compile(User.UID_RE) if not uid_re.match(uid): return False, u"只能包括字母数字和.-_" uid = uid.lower() if uid in [ "user", "pdf", "explore", "home", "visual", "settings", "admin", "past", "connect", "bind", "i", "notes", "note", "status", "share", "timeline", "post", "login", "logout", "sync", "about", "connect", "dev", "api", "thepast", "thepast.me", ]: return False, u"被系统占用了:)" try: cursor = db_conn.execute("""update user set uid=%s where id=%s""", (uid, self.id)) db_conn.commit() User._clear_cache(self.id) return True, u"设置成功" except IntegrityError: db_conn.rollback() return False, u"被别人占用了:)" finally: cursor and cursor.close() return False, "fail"
def add(cls, name=None, uid=None, session_id=None): cursor = None user = None name = "" if name is None else name uid = "" if uid is None else uid session_id = session_id if session_id else randbytes(8) try: cursor = db_conn.execute("""insert into user (uid, name, session_id) values (%s, %s, %s)""", (uid, name, session_id)) user_id = cursor.lastrowid if uid == "": cursor = db_conn.execute("""update user set uid=%s where id=%s""", (user_id, user_id), cursor=cursor) db_conn.commit() cls._clear_cache(None) user = cls.get(user_id) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return user
def add(cls, name=None, uid=None, session_id=None): cursor = None user = None name = "" if name is None else name uid = "" if uid is None else uid session_id = session_id if session_id else randbytes(8) try: cursor = db_conn.execute( """insert into user (uid, name, session_id) values (%s, %s, %s)""", (uid, name, session_id)) user_id = cursor.lastrowid if uid == "": cursor = db_conn.execute( """update user set uid=%s where id=%s""", (user_id, user_id), cursor=cursor) db_conn.commit() cls._clear_cache(None) user = cls.get(user_id) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return user
def remove(self): cursor = db_conn.execute( """delete from task_queue where id=%s""", self.id, ) db_conn.commit() cursor and cursor.close()
def remove(cls, status_id): cursor = None try: cursor = db_conn.execute('''delete from raw_status where status_id = %s''', status_id ) db_conn.commit() cls.clear_cache(status_id) except IntegrityError: db_conn.rollback() cursor and cursor.close()
def remove(cls, user_id): cursor = None try: cursor = db_conn.execute('''delete from user_profile where user_id= %s''', user_id) db_conn.commit() cls.clear_cache(user_id) except IntegrityError: db_conn.rollback() cursor and cursor.close()
def remove(cls, key_): cursor = None try: cursor = db_conn.execute('''delete from kv where `key` = %s''', key_) db_conn.commit() cls.clear_cache(key_) except IntegrityError: db_conn.rollback() cursor and cursor.close()
def add(cls, random_id, text): cursor = None try: cursor = db_conn.execute("""insert into confirmation (random_id, text) values(%s, %s)""", (random_id, text)) db_conn.commit() return True except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def add_user_id(cls, user_id): cursor = None try: cursor = db_conn.execute('''insert into pdf_settings (user_id) values (%s)''', user_id) db_conn.commit() cls._clear_cache(user_id) return True except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def set_email(self, email): cursor = None try: cursor = db_conn.execute("""replace into passwd (user_id, email) values (%s,%s)""", (self.id, email)) db_conn.commit() return True except IntegrityError: db_conn.rollback() return False finally: mc.delete("user_email:%s" % self.id) cursor and cursor.close()
def add(cls, user_id, weixin_name): cursor = None try: cursor = db_conn.execute('''insert into user_weixin (user_id, weixin_name) values (%s, %s) on duplicate key update user_id=%s''', (user_id, weixin_name, user_id)) db_conn.commit() cls.clear_cache(user_id, weixin_name) return cls.get_by_weixin(weixin_name) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def add(cls, random_id, text): cursor = None try: cursor = db_conn.execute( '''insert into confirmation (random_id, text) values(%s, %s)''', (random_id, text)) db_conn.commit() return True except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def add_user_id(cls, user_id): cursor = None try: cursor = db_conn.execute( '''insert into pdf_settings (user_id) values (%s)''', user_id) db_conn.commit() cls._clear_cache(user_id) return True except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def merge_a2b(del_uid, merged_uid): print "-------update status:%s 2 %s" % (del_uid, merged_uid) db_conn.execute("update status set user_id=%s where user_id=%s", (merged_uid, del_uid)) print "-------update alias:%s 2 %s" % (del_uid, merged_uid) db_conn.execute("update user_alias set user_id=%s where user_id=%s", (merged_uid, del_uid)) print "-------update synctask:%s 2 %s" % (del_uid, merged_uid) db_conn.execute("update sync_task set user_id=%s where user_id=%s", (merged_uid, del_uid)) db_conn.commit()
def set(cls, user_id, val): cursor = None val = json_encode(val) if not isinstance(val, basestring) else val try: cursor = db_conn.execute('''replace into user_profile (user_id, profile) values(%s,%s)''', (user_id, val)) db_conn.commit() cls.clear_cache(user_id) except IntegrityError: db_conn.rollback() cursor and cursor.close()
def update(self, title, content, fmt): if title and title != self.title or fmt and fmt != self.fmt or content and content != self.content: _fmt = fmt or self.fmt _title = title or self.title _content = content or self.content db_conn.execute('''update note set title = %s, content = %s, fmt = %s where id = %s''', (_title, _content, _fmt, self.id)) db_conn.commit() self.flush_note() if title != self.title: from past.model.status import Status Status._clear_cache(None, self.get_status_id(), None)
def add(cls, user_id, token, device=""): cursor = None try: cursor = db_conn.execute( '''insert into user_tokens (user_id, token, device) values (%s, %s, %s)''', (user_id, token, device)) id_ = cursor.lastrowid db_conn.commit() return cls.get(id_) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def remove_status(uid): cursor = db_conn.execute("select id from status where user_id=%s", uid) if cursor: rows = cursor.fetchall() for row in rows: sid = row[0] print "---- delete mongo text, sid=", sid RawStatus.remove(sid) print "---- delete from status, uid=", uid db_conn.execute("delete from status where user_id=%s", uid) db_conn.commit() Status._clear_cache(uid, None)
def update(self, title, content, fmt, privacy): if title and title != self.title or fmt and fmt != self.fmt or content and content != self.content or privacy and privacy != self.privacy: _fmt = fmt or self.fmt _title = title or self.title _content = content or self.content _privacy = privacy or self.privacy db_conn.execute('''update note set title = %s, content = %s, fmt = %s, privacy = %s where id = %s''', (_title, _content, _fmt, _privacy, self.id)) db_conn.commit() self.flush_note() if title != self.title: from past.model.status import Status Status._clear_cache(None, self.get_status_id(), None)
def set_email(self, email): cursor = None try: cursor = db_conn.execute( '''replace into passwd (user_id, email) values (%s,%s)''', (self.id, email)) db_conn.commit() return True except IntegrityError: db_conn.rollback() return False finally: mc.delete("user_email:%s" % self.id) cursor and cursor.close()
def set(cls, status_id, text, raw): cursor = None text = json_encode(text) if not isinstance(text, basestring) else text raw = json_encode(raw) if not isinstance(raw, basestring) else raw try: cursor = db_conn.execute('''replace into raw_status (status_id, text, raw) values(%s,%s,%s)''', (status_id, text, raw)) db_conn.commit() cls.clear_cache(status_id) except IntegrityError: db_conn.rollback() cursor and cursor.close()
def add(cls, task_id, task_kind): task = None cursor = None try: cursor = db_conn.execute("""insert into task_queue (task_id, task_kind) values (%s,%s)""", (task_id, task_kind)) db_conn.commit() task = cls.get(cursor.lastrowid) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return task
def add(cls, user_id, token, device=""): cursor = None try: cursor = db_conn.execute( """insert into user_tokens (user_id, token, device) values (%s, %s, %s)""", (user_id, token, device), ) id_ = cursor.lastrowid db_conn.commit() return cls.get(id_) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def add(cls, alias_id, access_token, refresh_token): ot = None cursor = None try: cursor = db_conn.execute("""replace into oauth2_token (alias_id, access_token, refresh_token) values (%s, %s, %s)""", (alias_id, access_token, refresh_token)) db_conn.commit() ot = cls.get(alias_id) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return ot
def add(cls, category, user_id): task = None cursor = None try: cursor = db_conn.execute("""insert into sync_task (category, user_id) values (%s,%s)""", (category, user_id)) db_conn.commit() task_id = cursor.lastrowid task = cls.get(task_id) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return task
def add(cls, alias_id, access_token, refresh_token): ot = None cursor = None try: cursor = db_conn.execute( """replace into oauth2_token (alias_id, access_token, refresh_token) values (%s, %s, %s)""", (alias_id, access_token, refresh_token)) db_conn.commit() ot = cls.get(alias_id) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return ot
def bind_to_exists_user(cls, user, type_, alias): ua = cls.get(type_, alias) if ua: return None ua = None cursor = None try: cursor = db_conn.execute("""insert into user_alias (`type`,alias,user_id) values (%s, %s, %s)""", (type_, alias, user.id)) db_conn.commit() ua = cls.get(type_, alias) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return ua
def bind_to_exists_user(cls, user, type_, alias): ua = cls.get(type_, alias) if ua: return None ua = None cursor = None try: cursor = db_conn.execute( """insert into user_alias (`type`,alias,user_id) values (%s, %s, %s)""", (type_, alias, user.id)) db_conn.commit() ua = cls.get(type_, alias) except IntegrityError: db_conn.rollback() finally: cursor and cursor.close() return ua
def add(cls, user_id, title, content, fmt=consts.NOTE_FMT_PLAIN, privacy=consts.STATUS_PRIVACY_PUBLIC): cursor = None try: cursor = db_conn.execute('''insert into note (user_id, title, content, create_time, fmt, privacy) values (%s, %s, %s, %s, %s, %s)''', (user_id, title, content, datetime.datetime.now(), fmt, privacy)) db_conn.commit() note_id = cursor.lastrowid note = cls.get(note_id) from past.model.status import Status Status.add(user_id, note_id, note.create_time, config.OPENID_TYPE_DICT[config.OPENID_THEPAST], config.CATE_THEPAST_NOTE, "") cls._clear_cache(user_id, None) return note except IntegrityError: db_conn.rollback() finally: cursor and cursor.close()
def remove_user(uid): user = User.get(uid) if not user: print '---no user:%s' % uid print "---- delete from user, uid=", uid db_conn.execute("delete from user where id=%s", uid) db_conn.commit() User._clear_cache(uid) cursor = db_conn.execute("select id from status where user_id=%s", uid) if cursor: rows = cursor.fetchall() for row in rows: sid = row[0] print "---- delete mongo text, sid=", sid RawStatus.remove(sid) print "---- delete from status, uid=", uid db_conn.execute("delete from status where user_id=%s", uid) db_conn.commit() Status._clear_cache(uid, None) print "---- delete from passwd, uid=", uid db_conn.execute("delete from passwd where user_id=%s", uid) print "---- delete from sync_task, uid=", uid db_conn.execute("delete from sync_task where user_id=%s", uid) print "---- delete from user_alias, uid=", uid db_conn.execute("delete from user_alias where user_id=%s", uid) db_conn.commit()
def remove_user(uid, clear_status=True): user = User.get(uid) if not user: print "---no user:%s" % uid suicide_log.info("---- delete from user, uid=%s" % uid) db_conn.execute("delete from user where id=%s", uid) db_conn.commit() User._clear_cache(uid) if clear_status: cursor = db_conn.execute("select id from status where user_id=%s", uid) if cursor: rows = cursor.fetchall() for row in rows: sid = row[0] suicide_log.info("---- delete status text, sid=%s" % sid) RawStatus.remove(sid) suicide_log.info("---- delete from status, uid=" % uid) db_conn.execute("delete from status where user_id=%s", uid) db_conn.commit() Status._clear_cache(uid, None) suicide_log.info("---- delete from passwd, uid=%s" % uid) db_conn.execute("delete from passwd where user_id=%s", uid) suicide_log.info("---- delete from sync_task, uid=%s" % uid) db_conn.execute("delete from sync_task where user_id=%s", uid) suicide_log.info("---- delete from user_alias, uid=%s" % uid) db_conn.execute("delete from user_alias where user_id=%s", uid) db_conn.commit()
def remove_user(uid, clear_status=True): user = User.get(uid) if not user: print '---no user:%s' % uid suicide_log.info("---- delete from user, uid=%s" % uid) db_conn.execute("delete from user where id=%s", uid) db_conn.commit() User._clear_cache(uid) if clear_status: cursor = db_conn.execute("select id from status where user_id=%s", uid) if cursor: rows = cursor.fetchall() for row in rows: sid = row[0] suicide_log.info("---- delete status text, sid=%s" % sid) RawStatus.remove(sid) suicide_log.info("---- delete from status, uid=" % uid) db_conn.execute("delete from status where user_id=%s", uid) db_conn.commit() Status._clear_cache(uid, None) suicide_log.info("---- delete from passwd, uid=%s" % uid) db_conn.execute("delete from passwd where user_id=%s", uid) suicide_log.info("---- delete from sync_task, uid=%s" % uid) db_conn.execute("delete from sync_task where user_id=%s", uid) suicide_log.info("---- delete from user_alias, uid=%s" % uid) db_conn.execute("delete from user_alias where user_id=%s", uid) db_conn.commit()
def add(cls, user_id, origin_id, create_time, site, category, title, text=None, raw=None): status = None cursor = None try: cursor = db_conn.execute("""insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title)) status_id = cursor.lastrowid if status_id > 0: text = json_encode(text) if text is not None else "" raw = json_encode(raw) if raw is not None else "" RawStatus.set(status_id, text, raw) db_conn.commit() status = cls.get(status_id) except IntegrityError: #log.warning("add status duplicated, ignore...") db_conn.rollback() finally: cls._clear_cache(user_id, None, cate=category) cursor and cursor.close() return status
def delete_by_random_id(cls, random_id): db_conn.execute( '''delete from confirmation where random_id=%s''', random_id) db_conn.commit()
def remove_user_id(cls, user_id): cursor = db_conn.execute( '''delete from pdf_settings where user_id=%s''', user_id) db_conn.commit() cls._clear_cache(user_id)
def remove(self): cursor = db_conn.execute("""delete from task_queue where id=%s""", self.id) db_conn.commit() cursor and cursor.close()
sys.path.append('../') import datetime import past from past.store import db_conn from past.utils.escape import json_decode from past.model.kv import RawStatus cursor = db_conn.execute("select id from status where category=200") rows = cursor.fetchall() cursor and cursor.close() ids = [x[0] for x in rows] for x in ids: try: r = RawStatus.get(x) raw = r.raw if r else "" if raw: print x data = json_decode(raw) t = data.get("created_at") created_at = datetime.datetime.strptime( t, "%a %b %d %H:%M:%S +0800 %Y") db_conn.execute("update status set create_time = %s where id=%s", (created_at, x)) db_conn.commit() except: import traceback print traceback.format_exc() sys.stdout.flush()
try: cursor = db_conn.execute("""insert into status (user_id, origin_id, create_time, site, category, title) values (%s,%s,%s,%s,%s,%s)""", (user_id, origin_id, create_time, site, category, title)) status_id = cursor.lastrowid try: if text is not None: mongo_conn.set(cls.STATUS_REDIS_KEY %status_id, json_encode(text)) if raw is not None: mongo_conn.set(cls.RAW_STATUS_REDIS_KEY %status_id, raw) except Exception, e: log.warning('ERROR_MONGODB:%s' % e) db_conn.rollback() else: db_conn.commit() status = cls.get(status_id) except IntegrityError: #log.warning("add status duplicated, ignore...") db_conn.rollback() finally: cls._clear_cache(user_id, None, cate=category) cursor and cursor.close() return status @classmethod def add_from_obj(cls, user_id, d, raw=None): origin_id = d.get_origin_id() create_time = d.get_create_time() title = d.get_title()