コード例 #1
0
ファイル: test_view.py プロジェクト: jrgrafton/tweet-debate
    def test_view_tasks_twitter_stream(self):
        if os.getenv('TWITTER_TESTS') == "FALSE":
            return # Modify in runtests.sh

        load_fixture('tests/states.json', kind={'State': State})
        load_fixture('tests/questions.json', 
                        kind={'Question': Question,'State': State})

        # Start new thread with Twitter Stream listener
        stream_thread = Thread(target=self._start_twitter_stream)
        stream_thread.daemon = True
        stream_thread.start()

        # Delete all previous tweets before proceeding
        twitter_api = TwitterAPI()
        twitter_api.delete_all_tweets()

        reponse = \
            self.app.get('/tasks/twitter_post_status' \
                '?question_cadence_minutes=1&post_to_twitter=True')
        current_question = Question.get_current_question()

        # Post reply
        twitter_status_id = twitter_api.get_last_tweet().id
        twitter_api.update_status("#yes for #WA #sway10", twitter_status_id)

        # Wait for stream to be updated
        time.sleep(2)

        # Test to see reply has been registered
        ndb.get_context().clear_cache()
        users = User.get_all()
        assert len(users) == 1
        users[0].sway_points = User.get_starting_sway_points() - 10
        users[0].votes[0].vote_text == "#yes for #WA"

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "WA"
        assert current_question.state_scores[0].party_score_votes[0] == 1
        assert current_question.state_scores[0].party_score_votes[1] == 0
        assert current_question.state_scores[0].party_score_sway[0] == 10
        assert current_question.state_scores[0].party_score_sway[1] == 0
コード例 #2
0
ファイル: test_view.py プロジェクト: jrgrafton/tweet-debate
    def test_view_tasks_twitter_stream(self):
        if os.getenv("TWITTER_TESTS") == "FALSE":
            return  # Modify in runtests.sh

        load_fixture("tests/states.json", kind={"State": State})
        load_fixture("tests/questions.json", kind={"Question": Question, "State": State})

        # Start new thread with Twitter Stream listener
        stream_thread = Thread(target=self._start_twitter_stream)
        stream_thread.daemon = True
        stream_thread.start()

        # Delete all previous tweets before proceeding
        twitter_api = TwitterAPI()
        twitter_api.delete_all_tweets()

        reponse = self.app.get("/tasks/twitter_post_status" "?question_cadence_minutes=1&post_to_twitter=True")
        current_question = Question.get_current_question()

        # Post reply
        twitter_status_id = twitter_api.get_last_tweet().id
        twitter_api.update_status("#yes for #WA #sway10", twitter_status_id)

        # Wait for stream to be updated
        time.sleep(2)

        # Test to see reply has been registered
        ndb.get_context().clear_cache()
        users = User.get_all()
        assert len(users) == 1
        users[0].sway_points = User.get_starting_sway_points() - 10
        users[0].votes[0].vote_text == "#yes for #WA"

        current_question = Question.get_current_question()
        assert len(current_question.state_scores) == 1
        assert current_question.state_scores[0].state_abbreviation == "WA"
        assert current_question.state_scores[0].party_score_votes[0] == 1
        assert current_question.state_scores[0].party_score_votes[1] == 0
        assert current_question.state_scores[0].party_score_sway[0] == 10
        assert current_question.state_scores[0].party_score_sway[1] == 0
コード例 #3
0
ファイル: tasks.py プロジェクト: jrgrafton/tweet-debate
def twitter_post_status():
    """ Potentially post a new status to Twitter
    """
    # Can be overriden in GET request
    question_cadence_minutes = \
        int(request.args.get('question_cadence_minutes', 60 * 6))
    post_to_twitter = request.args.get('post_to_twitter', False)

    next_question = Question.get_next_question()
    if next_question is not None \
            and __is_time_for_new_question(question_cadence_minutes):
        current_question = Question.get_current_question()

        if post_to_twitter != False:
            try:
                twitter_api = TwitterAPI()
                status = twitter_api.update_status(next_question.question_text)
                next_question.twitterid = str(status.id)
                next_question.put()
            except TweepError as e:
                pass  #TODO: do something - message to monitoring?

        if current_question is not None:
            # Update overall state scores
            State.update_state_scores_for_completed_question(current_question)

            # Update overall question scores
            Question.tally_college_and_vote_scores(current_question)

            # Update end of question user sway scores
            users = User.get_all()
            for user in users:
                __attribute_sway_points_for_user(current_question, user)
            current_question.end_time = datetime.datetime.now()
            current_question.put()

        next_question.start_time = datetime.datetime.now()
        next_question.put()

        return 'Posted new status [%s]' % next_question.question_text, 200

    return 'New question not ready', 200
コード例 #4
0
ファイル: tasks.py プロジェクト: jrgrafton/tweet-debate
def twitter_post_status():
    """ Potentially post a new status to Twitter
    """
    # Can be overriden in GET request
    question_cadence_minutes = \
        int(request.args.get('question_cadence_minutes', 60 * 6))
    post_to_twitter = request.args.get('post_to_twitter', False)
    
    next_question = Question.get_next_question()
    if next_question is not None \
            and __is_time_for_new_question(question_cadence_minutes):
        current_question = Question.get_current_question()
        
        if post_to_twitter != False:
            try:
                twitter_api = TwitterAPI()
                status = twitter_api.update_status(next_question.question_text)
                next_question.twitterid = str(status.id)
                next_question.put()
            except TweepError as e:
                pass  #TODO: do something - message to monitoring?

        if current_question is not None:
            # Update overall state scores
            State.update_state_scores_for_completed_question(current_question)
            
            # Update overall question scores
            Question.tally_college_and_vote_scores(current_question)

            # Update end of question user sway scores
            users = User.get_all()
            for user in users:
                __attribute_sway_points_for_user(current_question, user)
            current_question.end_time = datetime.datetime.now()
            current_question.put()
        
        next_question.start_time = datetime.datetime.now()
        next_question.put()

        return 'Posted new status [%s]' % next_question.question_text, 200

    return 'New question not ready', 200