Esempio n. 1
0
    def post(self):
        json_data = request.get_json()

        if not json_data:
            response = jsonify({'error': 'Invalid input'})

            return response, 400

        data, errors = add_fact_schema.load(json_data)

        if errors:
            response = {'error': errors}

            return jsonify(response), 422

        fact = Fact()
        fact.user_id = current_user.id
        fact.message = data['message']
        fact.save()

        pusher.trigger('private-facts', 'new-fact', {
            'message': fact.message,
            'username': current_user.username
        })

        response = {
            'data': {
                'created_on': fact.created_on,
                'id': fact.id,
                'message': fact.message
            },
            'editable': True
        }

        return jsonify(response), 200
Esempio n. 2
0
def cli():
    """
    Simulate users posting fake facts.

    :return: None
    """
    while True:
        # A Postgres specific way to grab a random row.
        user = User.query.order_by('RANDOM()').first()

        fact = Fact()
        fact.user_id = user.id
        fact.message = fake.text(max_nb_chars=random.randint(5, 200))
        fact.save()

        pusher.trigger('private-facts', 'new-fact', {
            'message': fact.message,
            'username': user.username
        })

        print('"{0}" posted the fact "{1}"'.format(user.username,
                                                   fact.message))

        time.sleep(2)

    return None
Esempio n. 3
0
def cli():
    """
    Run a primitive chat bot to push fake facts.

    :return: None
    """
    messages = [
        "Gargling water outdoors is illegal in Mongolia",
        "An ounce of giraffe saliva contains more vitamin C than an orange",
        "Snow is caused when Greek god Dandrifficles listens to death metal",
        "Matsubayashi Henyasai invented the shruiken after witnessing monkeys flinging their own poop",
        "Nostradamus' real name was Nostrildamus but he changed it at age 42 after no one was taking him seriously",
        "Docker Swarm was named after the Starcraft II video game expansion 'Heart of the Swarm'",
        "You sound better singing in the shower because the warm misty air loosens your vocal chords",
        "Potato bugs live and breed on potato farms. 27% of every potato you eat is made up of potato bugs",
        "More Canadians die per year from moose attacks than gun related injuries",
        "A tyrannosaurus rex's arms are the same size as an average 6 foot adult's arms",
        "In Michael Jackson's song 'Black or White', he was actually singing about zebras",
        "Mammals have hair and produce milk, therefore coconuts are technically mammals",
        "Google named themselves Google because even a baby can pronounce that word",
        "On average, you will swallow 8 spiders per year while sleeping"
    ]

    previously_sent_message = ''

    while True:
        message = random.choice(messages)

        # Anti-duplication algorithm (patent pending).
        while previously_sent_message == message:
            message = random.choice(messages)

        time.sleep(5)

        pusher.trigger('public-facts', 'new-fact', {'message': message})
        print('Pushed message: {0}'.format(message))
        previously_sent_message = message

    return None