def get_by_token(token: str) -> NeotterUser: current = datetime.datetime.utcnow().strftime(DATETIME_FORMAT) session = db.get_session() user = session.query(NeotterUser).filter( NeotterUser.token == token, NeotterUser.expired >= current).first() session.close() return user
def extend_expiration(user_id: str): session = db.get_session() user = session.query(NeotterUser).filter(NeotterUser.id == user_id).first() new_expired = datetime_utils.day_after(configs.auth_expiration_date) user.expired = datetime_utils.datetime_to_neotter_inner(new_expired) session.commit() session.close()
def get_valid_users() -> List[NeotterUser]: current = datetime.datetime.utcnow().strftime(DATETIME_FORMAT) session = db.get_session() users = list( map(lambda user: user.to_dict(), session.query(NeotterUser).filter(NeotterUser.expired >= current))) session.close() return users
def add_all(status_list: List[Status], neotter_user_id: str): tweet_list = from_status_list(status_list, neotter_user_id) session = db.get_session() try: for tweet in tweet_list: session.merge(tweet) except (OperationalError, InternalError): logger.error(traceback.format_exc()) session.rollback() else: session.commit() finally: session.close()
def delete_expired_users(): current = datetime.datetime.utcnow().strftime(DATETIME_FORMAT) session = db.get_session() try: session.query(NeotterUser).filter( NeotterUser.expired <= current).delete() except (OperationalError, InternalError): logger.error(traceback.format_exc()) session.rollback() else: session.commit() finally: session.close()
def register(user_dict: dict): user = NeotterUser(**user_dict) session = db.get_session() try: session.merge(user) except (OperationalError, InternalError): logger.error(traceback.format_exc()) session.rollback() raise ValueError('Failed to register user.') else: session.commit() finally: session.close()
def add_all(users: List[User]): twitter_users = from_user_list(users) session = db.get_session() try: for user in twitter_users: session.merge(user) except (OperationalError, InternalError): logger.error(traceback.format_exc()) session.rollback() else: session.commit() finally: session.close()
def delete_old_tweets(hour_before: int): delete_from = (datetime.datetime.now() - datetime.timedelta(hours=hour_before)) \ .strftime(INNER_DATETIME_FORMAT) session = db.get_session() try: session.query(Tweet).filter( Tweet.created_datetime <= delete_from).delete() except (OperationalError, InternalError): logger.error(traceback.format_exc()) session.rollback() else: session.commit() finally: session.close()
def get_recent_messages(count: int, offset: int, start_time: str, user_id: str): logger.info('getting messages') session = db.get_session() try: messages = session.query(Tweet, TwitterUser) \ .join(TwitterUser, and_( Tweet.user_id == TwitterUser.user_id, Tweet.client == TwitterUser.client)) \ .filter( Tweet.neotter_user_id == user_id, Tweet.created_datetime >= start_time) \ .order_by(Tweet.created_datetime) \ .limit(count).offset(offset) except (OperationalError, InternalError): logger.error(traceback.format_exc()) finally: session.close() return _merge_result(messages)
def get_timeline_messages(user_id: str, from_id: str = 0, count: int = 10): session = db.get_session() try: messages = session.query(Tweet, TwitterUser) \ .join(TwitterUser, and_( Tweet.user_id == TwitterUser.user_id, Tweet.client == TwitterUser.client)) if from_id: from_id_db = f'{user_id}-{from_id}' messages = messages.filter( Tweet.neotter_user_id == user_id, Tweet.message_id > from_id_db) \ .order_by(Tweet.created_datetime).limit(count).all() else: messages = reversed( messages.filter(Tweet.neotter_user_id == user_id).order_by( desc(Tweet.created_datetime)).limit(count).all()) except (OperationalError, InternalError): logger.error(traceback.format_exc()) finally: session.close() return _merge_result(messages)
def get_by_id(user_id: str) -> TwitterUser: session = db.get_session() user = session.query(TwitterUser).filter( TwitterUser.user_id == user_id).first() return user
def get_by_session(session_id: str) -> NeotterUser: session = db.get_session() user = session.query(NeotterUser).filter( NeotterUser.session_id == session_id).first() session.close() return user