Python API

Twitter

class RPA.Twitter.Twitter

Twitter is a library for accessing Twitter using developer API. The library extends tweepy library.

Authorization credentials can be given as parameters for authorize keyword or keyword can read them in as environment variables:

  • TWITTER_CONSUMER_KEY

  • TWITTER_CONSUMER_SECRET

  • TWITTER_ACCESS_TOKEN

  • TWITTER_ACCESS_TOKEN_SECRET

Library usage requires Twitter developer credentials. Those can be requested from Twitter developer site

Examples

*** Settings ***
Library   RPA.Twitter

*** Tasks ***
Get user tweets and like them
    [Setup]   Authorize
    @{tweets}=   Get User Tweets   username=niinisto   count=5
    FOR   ${tweet}  IN   @{tweets}
        Like   ${tweet}
    END
from RPA.Twitter import Twitter

library = Twitter()
library.authorize()
tweets = library.get_user_tweets(username="niinisto", count=5)
for tw in tweets:
    library.like(tw)
tweets = library.text_search_tweets(query="corona trump")
for tw in tweets:
    print(tw.text)
user = library.get_user_profile("niinisto")
library.follow(user)
library.tweet("first tweet")
me = library.get_me()
print(me)
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
authorize(consumer_key: Optional[str] = None, consumer_secret: Optional[str] = None, access_token: Optional[str] = None, access_token_secret: Optional[str] = None) None

Authorize to Twitter API

Parameters
  • consumer_key – app consumer key

  • consumer_secret – app consumer secret

  • access_token – user access token

  • access_token_secret – user access token secret

follow(user: Optional[str] = None) bool

Follow Twitter user

Parameters

user – screen name of the user

Returns

True if user was followed, False if not

get_me() dict

Get Twitter profile of authenticated user

Returns

user profile as dictionary or None

get_user_profile(username: Optional[str] = None) dict

Get user’s Twitter profile

Parameters

username – whose profile to get

Returns

profile as dictionary

get_user_tweets(username: Optional[str] = None, count: int = 100) list

Get user tweets

Parameters
  • username – whose tweets to get

  • count – maximum number of tweets, defaults to 100

Returns

list of user tweets

like(tweet: Optional[Tweet] = None) bool

Like a tweet

Parameters

tweet – as a class Tweet

Returns

True if Tweet was liked, False if not

text_search_tweets(query: Optional[str] = None, count: int = 100, geocode: Optional[str] = None, lang: Optional[str] = None, locale: Optional[str] = None, result_type: str = 'mixed', until: Optional[str] = None, since_id: Optional[str] = None, max_id: Optional[str] = None) list

Search tweets defined by search query

Results types:

  • mixed : include both popular and real time results in the response

  • recent : return only the most recent results in the response

  • popular : return only the most popular results in the response

Parameters
  • query – search query string of 500 characters maximum, including operators

  • count – maximum number of tweets, defaults to 100

  • geocode – tweets by users located within a given radius of the given latitude/longitude

  • lang – language code of tweets

  • locale – language of the query you are sending

  • result_type – type of search results you would prefer to receive, default “mixed”

  • until – tweets created before the given date

  • since_id – Returns only statuses with an ID greater than

  • max_id – only statuses with an ID less than

Returns

list of matching tweets

tweet(content: Optional[str] = None) None

Make a tweet with content

Parameters

content – text for the status update

unfollow(user: Optional[str] = None) bool

Unfollow Twitter user

Parameters

user – screen name of the user

Returns

True if user was followed, False if not

unlike(tweet: Optional[Tweet] = None) bool

Unlike a tweet

Parameters

tweet – as a class Tweet

Returns

True if Tweet was unliked, False if not