def test_match_user_mentions(self): match = Tweet({ 'text': 'text', 'entities': { 'user_mentions': [{ 'id': 123 }] }, 'user': { 'id': 1 } }) unmatched = Tweet({ 'text': 'text', 'entities': { 'user_mentions': [{ 'id': 1 }] }, 'user': { 'id': 1 } }) self.assertTrue(self.filter.match_tweet(match, user_stream=False)) self.assertFalse(self.filter.match_tweet(unmatched, user_stream=False))
def test_save_incoming_tweets(self): tweet = Tweet({ 'text': '@bot mornin', 'user': { 'screen_name': 'some_bloke' } }) client = ResponseBotClient(client=MagicMock(me=MagicMock( return_value=MagicMock(_json={'id': 123}))), config=MagicMock()) mock_cursor = MagicMock() with patch('sqlite3.connect', return_value=MagicMock(cursor=MagicMock( return_value=mock_cursor))): handler = SaveTweetHandler(client) handler.on_tweet(tweet) mock_cursor.execute.assert_has_calls([ call( "CREATE TABLE IF NOT EXISTS tweets (tweet text, username text)" ), call( "INSERT INTO tweets VALUES ('@bot mornin', 'some_bloke')"), ])
def test_does_not_reply_non_existent_country(self): client = MagicMock(tweet=MagicMock()) handler = CountryHandler(client) tweet = Tweet({'text': '@bot Azeroth'}) handler.on_tweet(tweet) client.tweet.assert_not_called()
def tweet(self, text, in_reply_to=None, filename=None, file=None): """ Post a new tweet. :param text: the text to post :param in_reply_to: The ID of the tweet to reply to :param filename: If `file` param is not provided, read file from this path :param file: A file object, which will be used instead of opening `filename`. `filename` is still required, for MIME type detection and to use as a form field in the POST data :return: Tweet object """ if filename is None: return Tweet(self._client.update_status(status=text, in_reply_to_status_id=in_reply_to)._json) else: return Tweet(self._client.update_with_media(filename=filename, file=file, status=text, in_reply_to_status_id=in_reply_to)._json)
def test_match_user_stream_with_track(self): match = Tweet({ 'text': 'keyword text', 'entities': {}, 'user': { 'id': 1 } }) unmatched = Tweet({ 'text': 'text otherkey', 'entities': {}, 'user': { 'id': 1 } }) self.assertTrue(self.filter.match_tweet(match, user_stream=True)) self.assertFalse(self.filter.match_tweet(unmatched, user_stream=True))
def test_reply_country_info(self): client = MagicMock(tweet=MagicMock()) handler = CountryHandler(client) tweet = Tweet({'text': '@bot Andorra'}) handler.on_tweet(tweet) client.tweet.assert_called_once_with('Country: Andorra\n' 'Population: 84000\n' 'Languages: ca\n' 'Continent: Europe')
def list_timeline(self, list_id, since_id=None, max_id=None, count=20): """ List the tweets of specified list. :param list_id: list ID number :param since_id: results will have ID greater than specified ID (more recent than) :param max_id: results will have ID less than specified ID (older than) :param count: number of results per page :return: list of :class:`~responsebot.models.Tweet` objects """ statuses = self._client.list_timeline(list_id=list_id, since_id=since_id, max_id=max_id, count=count) return [Tweet(tweet._json) for tweet in statuses]
def get_tweet(self, id): """ Get an existing tweet. :param id: ID of the tweet in question :return: Tweet object. None if not found """ try: return Tweet(self._client.get_status(id=id)._json) except TweepError as e: if e.api_code == TWITTER_TWEET_NOT_FOUND_ERROR: return None raise
def test_create_from_raw_data(self): created_at = 'Mon Apr 25 08:25:58 +0000 2016' raw = { 'some_key': 'some value', 'created_at': created_at, 'user': { 'name': 'Bird' }, 'retweeted_status': { 'created_at': created_at, }, 'quoted_status': { 'created_at': created_at, }, 'quoted_status_id_str': '123', 'in_reply_to_status_id': 123, 'in_reply_to_status_id_str': '123' } tweet = Tweet(raw) expected_created_at = parse(created_at) self.assertEqual(tweet.some_key, 'some value') self.assertEqual(tweet.created_at, expected_created_at) self.assertTrue(isinstance(tweet.user, User)) self.assertEqual(tweet.user.name, 'Bird') self.assertTrue(isinstance(tweet.retweeted_tweet, Tweet)) self.assertEqual(tweet.retweeted_tweet.created_at, expected_created_at) self.assertTrue(isinstance(tweet.quoted_tweet, Tweet)) self.assertEqual(tweet.quoted_tweet.created_at, expected_created_at) self.assertEqual(tweet.quoted_tweet_id_str, '123') self.assertEqual(tweet.in_reply_to_tweet_id, 123) self.assertEqual(tweet.in_reply_to_tweet_id_str, '123')
def on_status(self, status): self.listener.on_tweet(Tweet(status._json))