TwiliteTimeline

TwiliteTimeline is a twitter API client, which is capable of querying the Twitter statuses/user_timeline and statuses/update endpoint. The current version of the package only valid for Twitter API version 1.1

Use Case & Example:

The current version of the App has limited feature which will be improved in future updates. Each new feature will be introduced with elaborate examples. The current version has introduced new feature, now you can post tweet using TwiliteTimeline. But the design also changed along the way and old usecase examples are deprecated. Please follow new use cases to know more.

Use Case: Get latest 5 tweets from a Profile

To be able to use the package first user need their OAuth tokens and secrets which can be created after log in to twitter developer account by creating a new app. Then using the Authentictor and ParamsGetTweets type provided by the package they need to define values of that type and finally use the get_tweets methods to query the API.

  • Authentictor is a julia concrete type to store the Consumer Key, Consumer Secret, OAuth Token and OAuth Token from twitter API. It is recommended that user save them into environment and load them by using ENV keyword.
  • The ResourceParams concrete type is to store the Parameters needed to query the API and based on the API document version 1.1. The default values in the package are sometime different than the API document. By default the trim_user is set to false, when providing any valid count parameter the include_rts will be set to true silently.
creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"]);
params = ParamsGetTweets("rustlang", count=5);
tweets = get_tweets(creds, params);

Use Case: Post a New Tweet

To be able to use the package first user need their OAuth tokens and secrets which can be created after log in to twitter developer account by creating a new app. Then using the Authentictor and ParamsPostTweet type provided by the package they need to define values of that type and finally use the post_tweet methods to send the request.

creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"]);
params = ParamsPostTweet("... asdf asdf test tweet .... @JuliaLanguage using TwiliteTimeline.jl");
tweet = post_tweet(creds, params);

As twitter also return the whole tweet object after success post it is possible to persist every twitter after every post request or atleast test the return value that everything went well. In ParamsPostTweet type the status field is required and all other field either optional or has default value. **The other field need proper understanding before you use so please follow the status/update.json api doc before adding the other field of ParamsPostTweet.

Now it is possible to write and read json files having single tweets or array of tweets for better persistency.

Use Case: Write to Json and Read from Json

First run the any of the first or second usecase to load the tweets in to a object called tweets. Assuming we have a temp directory to store the tweets. Then follow the example:

# Write the tweets
write_to_json("./temp/all_tweets.json", tweets)

# Later read the tweets
read_from_json("./temp/all_tweets.json")

Deprecated and will be removed in the next minor release To be able to use the package first user need their OAuth tokens and secrets which can be created after log in to twitter developer account by creating a new app. Then using the Authentictor and ResourceParams type provided by the package they need to define values of that type and finally use the collect_tweets methods to query the API.

  • Authentictor is a julia concrete type to store the Consumer Key, Consumer Secret, OAuth Token and OAuth Token from twitter API. It is recommended that user save them into environment and load them by using ENV keyword.
  • The ResourceParams concrete type is to store the Parameters needed to query the API and based on the API document version 1.1. The default values in the package are sometime different than the API document. By default the trim_user is set to false, when providing any valid count parameter the include_rts will be set to true silently.

Use Case: Get latest 100 tweets from a Profile

In the following example the all credentials is saved as environment variable and later loaded from ENV dictionary.

# Create value of type Authentictor
creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"])
# Create Value of Type ResourceParams
rps = ResourceParams("Viral_B_Shah", count=100)

# Call The Twitter API
tweets = collect_tweets(creds, rps)

Use Case: Get latest 5 tweets with Excluding Replies and Trim User Data

The field trim_user and exclude_replies decides that if we want to exclude user data and exclude replies when fetching the data or not.

# Create value of type Authentictor
creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"])

# Create Value of Type ResourceParams
rps = ResourceParams("tomkwong", count=5, trim_user=true, exclude_replies=true)

# Call The Twitter API
tweets = collect_tweets(creds, rps)

The data return as list/array of dictionary, were each dictionary object is a Tweet.

TwiliteTimeline.AuthentictorType
Authentictor (Datatype:TwiliteTimeline)

Authenticator is defined datatype to create an object which will be used to authenticate the user when interacting with the twitter API. Someone must generate the Consumer Key, Consumer Secret, OAuth Token and OAuth Secret using their own twitter developer account then using those credentionas create a value of the type Authenticator.

Example

# Here Twitter OAuth token and secret is stored as environment varialbe whith name
# CKEY, CSEC, OTOK, OSEC
julia> creds =  Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"])
source
TwiliteTimeline.ParamsType
Params(AbstractType: TwiliteTimeline)

ApiParams is a abstruct type for any parameter subtype. So Any subtype of ApiParams should sataify all the requirements of the ApiParams.

source
TwiliteTimeline.ParamsGetTweetsType
ParamsGetTweets(ConcreteType: TwiliteTimeline)

ParamGetTweets is a defined datatype to store the Resource Parameter mentioned in the twitter API document which we have to send when qurying the API in the request body.

To be able to use twitter API the fild value shoud satisfy the following criteria:

  • When Providing valid count value the include_rts arguments must have to be 1; by default providing any valid count value of the include_rts will be set to 1 silintly
  • When providing constructor argument value for the boolean field such as trim_user, exclude_replies and include_rts the value mush have to be 1 or 0. By the default the values are set to the default value according to Twitter API doc.

Example

julia> resource_params = ParamsGetTweet("realDonaldTrump", count=100)
ParamsGetTweet("realDonaldTrump", nothing, nothing, 100, nothing, false, true)


julia> resource_params = ParamsGetTweet("realDonaldTrump", count=100, exclude_replies=true)
ParamsGetTweet("realDonaldTrump", nothing, nothing, 100, nothing, false, true)
source
TwiliteTimeline.ParamsPostTweetType
ParamsPostTweet(ConcreteType: TwiliteTimeline)

ParamsPostTweet is a defined datatype to store the Resource Parameter mentioned in the twitter API document which we have to send when qurying the API in the request body.

The status field is a mandatory field and rest of the field is set according to twitter api doc with default values. There is a lot of condition implied when using optional fields so it is suggested that use should first see the twitter version 1 api doc statuses/update.json.

Example

julia> test = ParamsPostTweet("asdf asdf asdf a test tweet.")
ParamsPostTweet("asdf asdf asdf a test tweet.", nothing, false, nothing, nothing, nothing, false, nothing, nothing, nothing, nothing, false, false, true, nothing)

source
TwiliteTimeline.ResourceParamsType
ResourceParams(DataType:TwiliteTimeline)

ResourceParams is a defined datatype to store the Resource Parameter mentioned in the twitter API document which we have to send when qurying the API in the request body.

To be able to use twitter API the fild value shoud satisfy the following criteria:

  • When Providing valid count value the include_rts arguments must have to be 1; by default providing any valid count value of the include_rts will be set to 1 silintly
  • When providing constructor argument value for the boolean field such as trim_user, exclude_replies and include_rts the value mush have to be 1 or 0. By the default the values are set to the default value according to Twitter API doc.

Example

julia> resource_params = ResourceParams("realDonaldTrump", count=100)
ResourceParams("realDonaldTrump", nothing, nothing, 100, nothing, false, true)


julia> resource_params = ResourceParams("realDonaldTrump", count=100, exclude_replies=true)
ResourceParams("realDonaldTrump", nothing, nothing, 100, nothing, false, true)
source
TwiliteTimeline.collect_tweetsMethod
collect_tweets(a::Authentictor, rp::ResourceParams)

Try to collect tweets based on given object of type Autenticator and ResourceParams. The collect_tweets is a wrapper around the oauth_request_resource method from OAuth package which is responsible for sending the get request to the statuses/user_timeline.

Example

# Here Twitter OAuth token and secret is stored as environment varialbe whith name
# CKEY, CSEC, OTOK, OSEC
julia> creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"])
julia> resource_params = ResourceParams("realDonaldTrump", count=100)
julia> tweets = collect_tweets(creds, resource_params);

That should return 100 latest tweets from the profile @realDonaldTrump.

source
TwiliteTimeline.get_tweetsMethod
get_tweets(a::Authentictor, p::ParamsGetTweet)

Try to collect tweets based on given object of type Autenticator and ParamsGetTweet. The get_tweets is a wrapper around the oauth_request_resource method from OAuth package which is responsible for sending the get request to the statuses/user_timeline.json.

Example

# Here Twitter OAuth token and secret is stored as environment varialbe whith name
# CKEY, CSEC, OTOK, OSEC
julia> creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"])
julia> params = ParamsGetTweet("realDonaldTrump", count=100)
julia> tweets = get_tweets(creds, params);

That should return 100 latest tweets from the profile @realDonaldTrump.

source
TwiliteTimeline.params_to_dictMethod
params_to_dict(rp::ResourceParams)

Is a internal function to convert the user provided Twitter API parameter into dictionary which oauth_request_resource from OAuth package expect. The method silently add include_rts paramater value to 1 when providing an valid count paramter.

source
TwiliteTimeline.params_to_dictMethod
params_to_dict(p::T) where {T <: Params}

Is a internal function to convert the user provided Twitter API parameter into dictionary which oauth_request_resource from OAuth package expect. The method silently add include_rts paramater value to 1 when providing an valid count paramter.

source
TwiliteTimeline.post_tweetMethod
post_tweet(a::Authentictor, p::ParamsPostTweet)

Try to collect tweets based on given object of type Autenticator and ParamsPostTweet. The post_tweet is a wrapper around the oauth_request_resource method from OAuth package which is responsible for sending the get request to the statuses/update.json.

Example

# Here Twitter OAuth token and secret is stored as environment varialbe whith name
# CKEY, CSEC, OTOK, OSEC
julia> creds = Authentictor(ENV["CKEY"], ENV["CSEC"], ENV["OTOK"], ENV["OSEC"])
julia> resource_params = ParamsPostTweet("asdf asdf asdf Test tweet")
julia> tweets = post_tweet(creds, resource_params);

That should return 100 latest tweets from the profile @realDonaldTrump.

source
TwiliteTimeline.read_from_jsonMethod
read_from_json(path::String)

Given a path of json file the method should be able to read files as Dict type or Array of Dict. The method expect the following parameters:

  • path: path of the json file to load

Example

julia> read_from_json("./temp/all_tweets.json")
File loading successful!
Dict{String,Any} with 1 entry:
  "asdf" => "asdf
source
TwiliteTimeline.write_to_jsonMethod
write_to_json(path::String, tw::ArrayOrDict)

General functon to write Dict or Array of Dict as tweet as json for better persistance. The method expect the following parameters:

  • path: path to save the file with file name (see the example)
  • tw: Array of Dict or Dict object to write as json

Example

julia> tweet = Dict("asdf" => "asdf")
Dict{String,String} with 1 entry:
  "asdf" => "asdf"

julia> tweets = [tweet, tweet]
2-element Array{Dict{String,String},1}:
 Dict("asdf" => "asdf")
 Dict("asdf" => "asdf")

julia> write_to_json("./temp/all_tweets.json",tweet)
File writting was successful!


julia> write_to_json("./temp/single_tweet.json",tweet)
File writting was successful!
source