Exemplo n.º 1
0
def test_transform_tweepy_error():
    e = TweepError("Something bad happened", mock.Mock(status_code=400))

    with pytest.raises(TwitterError) as excinfo:
        Twitter._throw_from_tweepy_error(e)

    assert excinfo.value.msg == "Something bad happened" and excinfo.value.code == 500
Exemplo n.º 2
0
 def test_rate_limit(self, mock_items):
     mock_items.side_effect = TweepError("Rate limit error", Response(429))
     self.widget.word_list = ["orange"]
     self.widget.search_button.click()
     self.wait_until_finished()
     self.assertTrue(self.widget.Error.rate_limit.is_shown())
     self.assertEqual("Search", self.widget.search_button.text())
Exemplo n.º 3
0
 def test_error(self, mock_items):
     mock_items.side_effect = TweepError("Other errors", Response(400))
     self.widget.word_list = ["orange"]
     self.widget.search_button.click()
     self.wait_until_finished()
     self.assertTrue(self.widget.Error.api_error.is_shown())
     self.assertEqual("Search", self.widget.search_button.text())
Exemplo n.º 4
0
def test_no_response(mocker):
    """Raise TweepError if Twitter is down."""
    from tweepy import TweepError
    mocked_api = mocker.patch('tweepy.API')
    mocked_method = mocked_api().trends_place
    mocked_method.side_effect = TweepError("Failed to send request: %s' % e")
    with pytest.raises(TweepError):
        call_twitter_api()
Exemplo n.º 5
0
 def get_user(self, user_id):
     if user_id not in self.id_to_screen_name:
         if user_id == 1234:
             raise TweepError("Invalid id")
         else:
             return MockUser("ScreenName3")
     else:
         return self.id_to_screen_name[user_id]
Exemplo n.º 6
0
    def test_get_tweet(self):
        wrapper = TweepyWrapper(client=Mock(get_status=Mock(return_value=Mock(
            _json={'abc': 123}))))
        expect(wrapper.get_tweet(1).raw_data).to.eq({'abc': 123})

        get_status_mock = Mock()
        get_status_mock.side_effect = TweepError(
            None, api_code=TWITTER_TWEET_NOT_FOUND_ERROR)
        client_mock = Mock(
            get_status=get_status_mock,
            get_user=Mock(return_value=Mock(_json={'abc': 456})))
        wrapper = TweepyWrapper(client=client_mock)
        expect(wrapper.get_tweet(1)).to.be.none()

        get_status_mock = Mock()
        get_status_mock.side_effect = TweepError(None, api_code=1111)
        wrapper = TweepyWrapper(client=Mock(get_status=get_status_mock))
        expect(lambda: wrapper.get_tweet(1)).to.throw(TweepError)
Exemplo n.º 7
0
    def test_update_error(self, API):
        handler = TweetsHandler(access_token=self.access_token, access_token_secret=self.access_token_secret,
                                screen_name=self.screen_name)
        api = API.return_value
        api.user_timeline.side_effect = TweepError('reason')

        self.assertRaises(FeedErrorException, handler.update)

        api.user_timeline.assert_called_once_with(screen_name=self.screen_name)
Exemplo n.º 8
0
def test_twitter_search_throws_error():
    twitter = Twitter()
    api = mock.create_autospec(API)
    api.search.side_effect = TweepError('Something Bad Happened',
                                        mock.Mock(status_code=400))
    twitter._api = api

    with pytest.raises(TwitterError):
        twitter.search('Fred')
Exemplo n.º 9
0
def test_main_bad_update(update_status, youtube_api, twitter_api,
                         get_redis_data, set_redis_data):
    """Test if main does stuff if tweepy goes horribly wrong.

    Make sure it can keep going."""
    from tweepy import TweepError
    get_redis_data.side_effect = redis_side_effect
    youtube_api.return_value = (u'url', True)
    update_status.side_effect = TweepError("Couldn't Post")
    assert twitter_bot.main() == u'Something went horribly wrong.'
Exemplo n.º 10
0
    def test_follow(self):
        wrapper = TweepyWrapper(client=Mock(create_friendship=Mock(
            return_value=Mock(_json={'abc': 123}))))
        expect(wrapper.follow(1).raw_data).to.eq({'abc': 123})

        create_friendship_mock = Mock()
        create_friendship_mock.side_effect = TweepError(
            None, api_code=TWITTER_ACCOUNT_SUSPENDED_ERROR)
        client_mock = Mock(
            create_friendship=create_friendship_mock,
            get_user=Mock(return_value=Mock(_json={'abc': 456})))
        wrapper = TweepyWrapper(client=client_mock)
        expect(wrapper.follow(1).raw_data).to.eq({'abc': 456})

        create_friendship_mock = Mock()
        create_friendship_mock.side_effect = TweepError(None, api_code=1111)
        wrapper = TweepyWrapper(client=Mock(
            create_friendship=create_friendship_mock))
        expect(lambda: wrapper.follow(1)).to.throw(TweepError)
Exemplo n.º 11
0
    def test_get_user(self):
        wrapper = TweepyWrapper(client=Mock(get_user=Mock(return_value=Mock(
            _json={'abc': 123}))))
        expect(wrapper.get_user(1).raw_data).to.eq({'abc': 123})
        expect(wrapper._user_dict[1].raw_data).to.eq({'abc': 123})

        wrapper = TweepyWrapper(client=None)
        wrapper._user_dict = {1: 'abc'}
        expect(wrapper.get_user(1)).to.eq('abc')

        get_user_mock = Mock()
        get_user_mock.side_effect = TweepError(
            None, api_code=TWITTER_USER_NOT_FOUND_ERROR)
        wrapper = TweepyWrapper(client=Mock(get_user=get_user_mock))
        expect(wrapper.get_user(1)).to.be.none()

        get_user_mock.side_effect = TweepError(None, api_code=11111)
        wrapper = TweepyWrapper(client=Mock(get_user=get_user_mock))
        expect(lambda: wrapper.get_user(1)).to.throw(TweepError)
Exemplo n.º 12
0
def test_twitter_error_handling(client):
    api = mock.Mock()
    api.search.side_effect = TweepError('Whoops...')

    with mock.patch('twiki.exts.twitter._api', api):
        resp = client.get('/api/tweets/whoops')

    jsoned = make_json(resp.data)

    assert "Sorry, we're having trouble with Twitter right now." == jsoned[
        'msg']
    assert resp.status_code == 500
    def test_compare_sentiments__when_POST_method_and_error_occurs(
            self, mock_request, mock_tweets_sentiment, mock_render_template):
        mock_request_method = PropertyMock(return_value='POST')
        type(mock_request).method = mock_request_method
        mock_request.form.__getitem__.return_value = 'keyword1 ,keyword2, keyword3 '
        mock_tweets_sentiment.side_effect = TweepError('test reason')

        sentiment.compare_sentiments()

        mock_request_method.assert_called_once()
        mock_request.form.__getitem__.assert_called_once_with('keywords')
        mock_tweets_sentiment.assert_called_once()
        mock_render_template.assert_called_once()
Exemplo n.º 14
0
def get_tweets(handle):
    """ :param handle: a non-empty string starting with @
        :return: a list of words in tweets from the specified user
    """

    if handle == "@":
        raise ValueError
    else:
        # connect to API
        auth = tweepy.OAuthHandler(Secrets.api_key, Secrets.api_secret)
        auth.set_access_token(Secrets.access_token,
                              Secrets.access_token_secret)

        api = tweepy.API(auth)

        # check if they're protected
        is_protected = api.get_user(handle).protected

        if is_protected:
            raise TweepError("User is protected")

        # pre-compile regex pattern
        regex_pattern = re.compile(
            r"""
                                    @[\w\']+             # handles
                                    |http[\w:/\.]+       # links
                                    |\*                  # asterisks
                                    |"|''                # quotes
                                    |\(|\)               # parentheses
                                    |\[|\]               # brackets
                                    |:\)|:\(             # smiley faces
                                    |,                   # commas
                                    |…                   # ...
                                    |&[A-Za-z;]+         # html special entities
                                    """, re.VERBOSE)

        # loop through tweets
        tweet_list = []
        for status in tweepy.Cursor(api.user_timeline,
                                    screen_name=handle,
                                    include_rts=False).items(500):
            # grab tweet from response
            tweet = status.text

            # clean up tweet and make it a list
            tweet_list += get_cleaned_list(tweet, regex_pattern)

        # return list containing all tweets
        return tweet_list
Exemplo n.º 15
0
 def getNewStats(self, statusID, api):
     try:
         x = json.dumps(api.get_status(statusID)._json)
         x = json.loads(x)
         ret_count = x.get('retweet_count')
         fav_count = x.get('favorite_count')
 #        print("Updated tweet with ID " + str(statusID))
         return ret_count, fav_count
     #Error handling, special case for rate limit
     except TweepError as t:
         print(' * Tweet with ID ' + str(statusID) + ' error:')
         print(t.reason)
         if t.reason[10:12] == '88' or t.reason[44:46] == '88':
             raise TweepError('Can\'t continue with rate limit exceeded. Wait a bit before trying again')
         return 0,0
Exemplo n.º 16
0
    def _poll(self):
        try:
            kwargs = {
                "id": "3170905003",
                "include_rts":
                False,  # please for the love of god retweet less
                "tweet_mode": "extended"
            }
            tweets = tweepy.Cursor(self.api.user_timeline, **kwargs).items(100)
            for tweet in sorted(tweets, key=lambda t: t.created_at):
                # print(f"looking at https://twitter.com/statuses/{tweet.id}")
                if self.is_recent(tweet):
                    if not self._have_seen(tweet):
                        self._mark_seen(tweet)
                        self.relay(tweet)

        except TweepError as err:
            self.log_error(self._extract_tweepy_error(err))
            raise TweepError(err)
Exemplo n.º 17
0
	def filter(self, track_list):
		while True:
			try:
				self.stream.filter(track=track_list)
			except error.TweepError as e:
				raise TweepError(e)
Exemplo n.º 18
0
 def friends_ids(self, user_id):
     if user_id not in self.id_to_friends:
         raise TweepError("Invalid id")
     else:
         return self.id_to_friends[user_id]
Exemplo n.º 19
0
 def inner(*args, **kwargs):
     if not called['called']:
         called['called'] = True
         raise TweepError([{'message': 'Rate Limit Exceeded', 'code': 88}])
     else:
         return 0
Exemplo n.º 20
0
 def followers_ids(self, user_id):
     if user_id not in self.id_to_followers:
         raise TweepError("Invalid id")
     else:
         return self.id_to_followers[user_id]
Exemplo n.º 21
0
        return True  # Don't kill the stream


class UserStream(Stream):
    def user_stream(self,
                    follow=None,
                    track=None,
                    async=False,
                    locations=None):
        self.parameters = {
            "delimited": "length",
        }
        self.headers['Content-type'] = "application/x-www-form-urlencoded"

        if self.running:
            raise TweepError('Stream object already connected!')

        self.scheme = "https"
        self.host = 'userstream.twitter.com'
        self.url = '/2/user.json'

        if follow:
            self.parameters['follow'] = ','.join(map(str, follow))
        if track:
            self.parameters['track'] = ','.join(map(str, track))
        if locations and len(locations) > 0:
            assert len(locations) % 4 == 0
            self.parameters['locations'] = ','.join(
                ['%.2f' % l for l in locations])

        self.body = urllib.urlencode(self.parameters)
Exemplo n.º 22
0
 def __init__(self, reason, response, ratelimit):
     self.response = response
     self.ratelimit = ratelimit or {}
     TweepError.__init__(self, reason, response)
Exemplo n.º 23
0
 def check_status(self):
     try:
         self.api.verify_credentials()
         print("Authentication OK")
     except TweepError as e:
         raise TweepError(f"Error during authentication. Details: {e}")
            }
            tweet_list.append(entry)
        print("...tweets fetched")
        utils.write_to_json(json_file_name, tweet_list)
    except tweepy.TweepError as e:
        raise HistoricTweetException(str(e))


if __name__ == "__main__":
    arg_keyword = "".join(sys.argv[1])
    arg_num_of_tweets = int(sys.argv[2])
    arg_json_file_name = sys.argv[3]
    # Get credentials
    try:
        consumer_key = credentials.get_consumer_key()
        consumer_secret = credentials.get_consumer_secret()
        access_token = credentials.get_access_token()
        access_token_secret = credentials.get_access_secret()
    except credentials.VaultException as error:
        raise HistoricTweetException("Vault Exception: " + str(error))
    # Set Up Auth
    try:
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        api = API(auth)
    except TweepError as err:
        raise TweepError("Authentication Failed: " + str(err))

    _get_historic_tweets(api, arg_keyword, arg_json_file_name,
                         arg_num_of_tweets)