Beispiel #1
0
 def setUp(self, m):
     with open(self.tests_path +
               '/fixtures/application_rate_limit_status.json') as f:
         response = f.read()
     m.get('https://api.twitter.com/1.1/application/rate_limit_status.json',
           text=response)
     self.client = TwitterClient('Consumer Key', "Consumer Secret",
                                 "Access Key", "Access Secret")
Beispiel #2
0
 def setUp(self, m):
     load_fixture_config()
     response = get_fixture('application_rate_limit_status.json', True)
     m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response)
     self.client = TwitterClient('Consumer Key', "Consumer Secret", "Access Key", "Access Secret")
Beispiel #3
0
class TestTwitterClient(unittest.TestCase):
    """Tests for TwitterClient class"""
    tests_path = path = os.path.dirname(os.path.abspath(__file__))

    @requests_mock.mock()
    def setUp(self, m):
        load_fixture_config()
        response = get_fixture('application_rate_limit_status.json', True)
        m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response)
        self.client = TwitterClient('Consumer Key', "Consumer Secret", "Access Key", "Access Secret")

    @requests_mock.mock()
    def test_search_tweets(self, m):
        response = get_fixture('search_tweets.json', True)
        m.get('https://api.twitter.com/1.1/search/tweets.json?q=210462857140252672&result_type=mixed&count=50',
              text=response)
        r = self.client.search_tweets("210462857140252672", 50)
        self.assertEqual(len(r), 4)

    @requests_mock.mock()
    def test_search_tweets_with_language(self, m):
        response = get_fixture('search_tweets.json', True)
        m.get('https://api.twitter.com/1.1/search/tweets.json?&lang=en&q=210462857140252672&result_type=mixed&count=50',
              text=response)
        r = self.client.search_tweets("210462857140252672", 50, language="en")
        self.assertEqual(len(r), 4)

    @requests_mock.mock()
    def test_update(self, m):
        response = get_fixture('statuses_update_reply.json', True)
        m.post('https://api.twitter.com/1.1/statuses/update.json', text=response)
        self.client.update('test', 2)

        self.assertTrue(m.called)
        self.assertEqual(m.call_count, 1)

        history = m.request_history[0]
        self.assertEqual(history.method, 'POST')
        self.assertIn('test', history.text)
        self.assertIn('2', history.text)

    @requests_mock.mock()
    def test_retweet(self, m):
        response = get_fixture('statuses_retweet.json', True)
        m.post('https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json', text=response)
        r = self.client.retweet("241259202004267009")
        self.assertEqual(r['retweeted_status']['id'], 241259202004267009)

    @requests_mock.mock()
    def test_retweet_already_retweeted(self, m):
        response = get_fixture('error_already_retweeted.json', True)
        m.post('https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json', text=response)
        with self.assertRaises(TwitterClientRetweetedException):
            self.client.retweet("241259202004267009")

    @requests_mock.mock()
    def test_get_tweet(self, m):
        response = get_fixture('statuses_show.json', True)
        m.get('https://api.twitter.com/1.1/statuses/show/210462857140252672.json', text=response)
        r = self.client.get_tweet("210462857140252672")
        self.assertEqual(r['id'], 210462857140252672)

    @requests_mock.mock()
    def test_get_friends_ids(self, m):
        response = get_fixture('friends_ids.json', True)
        m.get('https://api.twitter.com/1.1/friends/ids.json', text=response)
        r = self.client.get_friends_ids()
        self.assertEqual(len(r), 31)

    @requests_mock.mock()
    def test_follow(self, m):
        response = get_fixture('friendship_create.json', True)
        m.post('https://api.twitter.com/1.1/friendships/create.json', text=response)
        r = self.client.follow(1401881)
        self.assertEqual(r['id'], 1401881)

    @requests_mock.mock()
    def test_unfollow(self, m):
        response = get_fixture('friendship_create.json', True)
        m.post('https://api.twitter.com/1.1/friendships/destroy.json', text=response)
        r = self.client.unfollow(1401881)
        self.assertEqual(r['id'], 1401881)

    @requests_mock.mock()
    def test_favorite(self, m):
        response = get_fixture('favorites_create.json', True)
        m.post('https://api.twitter.com/1.1/favorites/create.json', text=response)
        r = self.client.favorite(243138128959913986)
        self.assertEqual(r['id'], 243138128959913986)

    @requests_mock.mock()
    def test_get_blocks(self, m):
        response = get_fixture('blocks_ids.json', True)
        m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response)
        r = self.client.get_blocks()
        self.assertEqual(len(r), 1)

    @requests_mock.mock()
    def test_get_mentions_timeline(self, m):
        response = get_fixture('statuses_mentions_timeline.json', True)
        m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json', text=response)
        r = self.client.get_mentions_timeline()
        self.assertEqual(len(r), 2)

    @requests_mock.mock()
    def test_get_mentions_timeline_since_id(self, m):
        response = get_fixture('statuses_mentions_timeline_since_id.json', True)
        m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json?since_id=653965849364180992', text=response)
        r = self.client.get_mentions_timeline(since_id=653965849364180992)
        self.assertEqual(len(r), 1)

    @requests_mock.mock()
    def test_get_mentions_timeline_count_1(self, m):
        response = get_fixture('statuses_mentions_timeline_count_1.json', True)
        m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=1', text=response)
        r = self.client.get_mentions_timeline(count=1)
        self.assertEqual(len(r), 1)

    @requests_mock.mock()
    def test_update_ratelimits(self, m):
        # revert original function
        response = get_fixture('application_rate_limit_status.json', True)
        m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response)
        self.client.update_ratelimits(False)
        self.assertEqual(len(self.client.ratelimiter), 80)
        # check if percent is computed
        for x in self.client.ratelimiter.values():
            self.assertIn('percent', x)
            self.assertEqual(x['limit'] / 100 * x['percent'], x['remaining'])

    @requests_mock.mock()
    def test_api_call_error(self, m):
        response = get_fixture('error.json', True)
        m.get(requests_mock.ANY, text=response)
        with self.assertRaises(TwitterClientException):
            self.client._api_call('blocks/ids')

    @requests_mock.mock()
    def test_api_call_no_check_ratelimits(self, m):
        response = get_fixture('blocks_ids.json', True)
        m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response)
        self.client.ratelimiter.check_limit = MagicMock()

        self.client._api_call('blocks/ids', check_ratelimit=False)
        self.assertFalse(self.client.ratelimiter.check_limit.called)

    @requests_mock.mock()
    def test_api_call_decrease_remaining_calls(self, m):
        response = get_fixture('blocks_ids.json', True)
        m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response)
        self.client.ratelimiter.check_limit = MagicMock()

        before_remaining = self.client.ratelimiter['/blocks/ids']['remaining']

        self.client._api_call('blocks/ids')

        self.assertEqual(before_remaining - 1, self.client.ratelimiter['/blocks/ids']['remaining'])
Beispiel #4
0
import argparse
import json
from yatcobot.client import TwitterClient
from yatcobot.config import TwitterConfig, Config

parser = argparse.ArgumentParser(description='Download a tweet to json')
parser.add_argument('tweet_id', metavar='id', type=int)
parser.add_argument('--config',
                    '-c',
                    dest='config',
                    default='../config.yaml',
                    help='Path of the config file')

args = parser.parse_args()

Config.load(args.config)

client = TwitterClient(TwitterConfig.get().consumer_key,
                       TwitterConfig.get().consumer_secret,
                       TwitterConfig.get().access_token_key,
                       TwitterConfig.get().access_token_secret)

tweet = client.get_tweet(args.tweet_id)

with open(f'{args.tweet_id}.json', 'w') as f:
    json.dump(tweet, f, indent=2)
 def setUp(self, m):
     with open(self.tests_path + '/fixtures/application_rate_limit_status.json') as f:
         response = f.read()
     m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response)
     self.client = TwitterClient('Consumer Key', "Consumer Secret", "Access Key", "Access Secret")
class TestTwitterClient(unittest.TestCase):
    """Tests for TwitterClient class"""
    tests_path = path = os.path.dirname(os.path.abspath(__file__))

    @requests_mock.mock()
    def setUp(self, m):
        with open(self.tests_path + '/fixtures/application_rate_limit_status.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response)
        self.client = TwitterClient('Consumer Key', "Consumer Secret", "Access Key", "Access Secret")


    @requests_mock.mock()
    def test_search_tweets(self, m):
        with open(self.tests_path + '/fixtures/search_tweets.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/search/tweets.json?q=210462857140252672&result_type=mixed&count=50',
                                                                                                    text=response)
        r = self.client.search_tweets("210462857140252672", 50)
        self.assertEqual(len(r), 4)

    @requests_mock.mock()
    def test_retweet(self, m):
        with open(self.tests_path + '/fixtures/statuses_retweet.json') as f:
            response = f.read()
        m.post('https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json', text=response)
        r = self.client.retweet("241259202004267009")
        self.assertEqual(r['retweeted_status']['id'], 241259202004267009)

    @requests_mock.mock()
    def test_retweet_already_retweeted(self, m):
        with open(self.tests_path + '/fixtures/error_already_retweeted.json') as f:
            response = f.read()
        m.post('https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json', text=response)
        with self.assertRaises(TwitterClientRetweetedException):
            self.client.retweet("241259202004267009")


    @requests_mock.mock()
    def test_get_tweet(self, m):
        with open(self.tests_path + '/fixtures/statuses_show.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/statuses/show/210462857140252672.json', text=response)
        r = self.client.get_tweet("210462857140252672")
        self.assertEqual(r['id'], 210462857140252672)

    @requests_mock.mock()
    def test_get_friends_ids(self, m):
        with open(self.tests_path + '/fixtures/friends_ids.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/friends/ids.json', text=response)
        r = self.client.get_friends_ids()
        self.assertEqual(len(r), 31)

    @requests_mock.mock()
    def test_follow(self, m):
        with open(self.tests_path + '/fixtures/friendship_create.json') as f:
            response = f.read()
        m.post('https://api.twitter.com/1.1/friendships/create.json', text=response)
        r = self.client.follow(1401881)
        self.assertEqual(r['id'], 1401881)

    @requests_mock.mock()
    def test_unfollow(self, m):
        with open(self.tests_path + '/fixtures/friendship_create.json') as f:
            response = f.read()
        m.post('https://api.twitter.com/1.1/friendships/destroy.json', text=response)
        r = self.client.unfollow(1401881)
        self.assertEqual(r['id'], 1401881)

    @requests_mock.mock()
    def test_favorite(self, m):
        with open(self.tests_path + '/fixtures/favorites_create.json') as f:
            response = f.read()
        m.post('https://api.twitter.com/1.1/favorites/create.json', text=response)
        r = self.client.favorite(243138128959913986)
        self.assertEqual(r['id'], 243138128959913986)

    @requests_mock.mock()
    def test_get_blocks(self, m):
        with open(self.tests_path + '/fixtures/blocks_ids.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response)
        r = self.client.get_blocks()
        self.assertEqual(len(r), 1)

    @requests_mock.mock()
    def test_get_mentions_timeline(self, m):
        with open(self.tests_path + '/fixtures/statuses_mentions_timeline.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json', text=response)
        r = self.client.get_mentions_timeline()
        self.assertEqual(len(r), 2)

    @requests_mock.mock()
    def test_get_mentions_timeline_since_id(self, m):
        with open(self.tests_path + '/fixtures/statuses_mentions_timeline_since_id.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json?since_id=653965849364180992', text=response)
        r = self.client.get_mentions_timeline(since_id=653965849364180992)
        self.assertEqual(len(r), 1)

    @requests_mock.mock()
    def test_get_mentions_timeline_count_1(self, m):
        with open(self.tests_path + '/fixtures/statuses_mentions_timeline_count_1.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=1', text=response)
        r = self.client.get_mentions_timeline(count=1)
        self.assertEqual(len(r), 1)

    @requests_mock.mock()
    def test_update_ratelimits(self, m):
        #revert original function
        with open(self.tests_path + '/fixtures/application_rate_limit_status.json') as f:
            response = f.read()

        m.get('https://api.twitter.com/1.1/application/rate_limit_status.json', text=response)
        self.client.update_ratelimits(False)
        self.assertEqual(len(self.client.ratelimiter), 80)
        #check if percent is computed
        for x in self.client.ratelimiter.values():
            self.assertIn('percent', x)
            self.assertEqual(x['limit'] / 100 * x['percent'], x['remaining'])

    @requests_mock.mock()
    def test_api_call_error(self, m):
        with open(self.tests_path + '/fixtures/error.json') as f:
            response = f.read()
        m.get(requests_mock.ANY, text=response)
        with self.assertRaises(TwitterClientException):
            self.client._api_call('blocks/ids')

    @requests_mock.mock()
    def test_api_call_no_check_ratelimits(self, m):
        with open(self.tests_path + '/fixtures/blocks_ids.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response)
        self.client.ratelimiter.check_limit = MagicMock()

        self.client._api_call('blocks/ids', check_ratelimit=False)
        self.assertFalse(self.client.ratelimiter.check_limit.called)

    @requests_mock.mock()
    def test_api_call_decrease_remaining_calls(self, m):
        with open(self.tests_path + '/fixtures/blocks_ids.json') as f:
            response = f.read()
        m.get('https://api.twitter.com/1.1/blocks/ids.json', text=response)
        self.client.ratelimiter.check_limit = MagicMock()

        before_remaining = self.client.ratelimiter['/blocks/ids']['remaining']

        self.client._api_call('blocks/ids')

        self.assertEqual(before_remaining - 1, self.client.ratelimiter['/blocks/ids']['remaining'])