예제 #1
0
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
예제 #2
0
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()
예제 #3
0
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
예제 #4
0
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()
예제 #5
0
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()
예제 #6
0
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()
예제 #7
0
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()
예제 #8
0
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()
예제 #9
0
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)
예제 #10
0
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)
예제 #11
0
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
예제 #12
0
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