Exemplo n.º 1
0
    def twitter_follow(**options):
        json_data = request.get_json()
        if not json_data:
            return {"error": "No input data provided"}, 400

        try:
            data: UserRequest = UserRequestSchema().load(json_data)
        except ValidationError as err:
            return {
                "error": f"Did not match schema: {json.dumps(err.messages)}"
            }, 400

        username = data.username.strip()

        if not username:
            return {
                "message": "username must contain at least 1 character"
            }, 400

        with DBManager.create_session_scope() as db_session:
            twitter_user = db_session.query(TwitterUser).filter_by(
                username=username).one_or_none()
            if twitter_user is not None:
                return {"message": f"We are already following {username}"}, 409

            twitter_user = TwitterUser(username)

            db_session.add(twitter_user)
            db_session.flush()
            db_session.commit()

            SocketClientManager.send("twitter.follow", {"username": username})

            return {"message": f"Successfully followed {username}"}, 200
Exemplo n.º 2
0
 def follow_user(self, username):
     """Add `username` to our relevant_users list."""
     if self.listener:
         if username not in self.listener.relevant_users:
             with DBManager.create_session_scope() as db_session:
                 db_session.add(TwitterUser(username))
             self.listener.relevant_users.append(username)
             log.info('Now following {0}'.format(username))
             return True
     return False
Exemplo n.º 3
0
    def follow_user(self, username):
        """Add `username` to our relevant_users list."""
        if not self.listener:
            log.error("No twitter listener set up")
            return False

        if username in self.listener.relevant_users:
            log.warning(f"Already following {username}")
            return False

        with DBManager.create_session_scope() as db_session:
            db_session.add(TwitterUser(username))
            self.listener.relevant_users.append(username)
            log.info(f"Now following {username}")

        return True
Exemplo n.º 4
0
    def post(self, **options):
        args = self.post_parser.parse_args()
        with DBManager.create_session_scope() as db_session:
            twitter_user = db_session.query(TwitterUser).filter_by(username=args["username"]).one_or_none()
            if twitter_user is not None:
                return {"message": "We are already following {}".format(args["username"])}, 409

            twitter_user = TwitterUser(args["username"].lower())

            db_session.add(twitter_user)
            db_session.flush()
            db_session.commit()

            SocketClientManager.send("twitter.follow", {"username": args["username"]})

            return {"message": "Successfully followed {}".format(args["username"])}, 200