Reading user timeline

by javier (modified: 2017 Jul 03)

How to read a User Timeline (Part 4)

In the previous entries, we have learned how to authenticate with Twitter then how to parse the response and in the last entry we learned how to read a tweet

Here we will show you how to read a user timeline calling the statuses/user_timeline endpoint, which returns a collection of the most recent Tweets posted by the user indicated by the screen_name or user_id parameters. The timeline returned is the equivalent of the one seen as a user’s profile on twitter.com.

Resource URL

https://dev.twitter.com/rest/reference/get/statuses/user_timeline

Returns a collection of the most recent Tweets posted by the user indicated by the screen_name or user_id parameters.

User timelines belonging to protected users may only be requested when the authenticated user either “owns” the timeline or is an approved follower of the owner.

The timeline returned is the equivalent of the one seen as a user’s profile on twitter.com.

Check the documentation for details here: User Timeline

Example

In this example we will also parse JSON response and we will create a new object l_tweets instance of LIST [TWITTER_TWEETS] class. First, we will check that the Status code is 200, so we know everything is Ok and we have a response body, in other case we show the response status code.

The following snippet code, shows how we do that after we got the Twitter's response, the l_body have the JSON response from twitter. Finally, we print out the user timeline.

-- Now we will parse the response body. -- and display the user timeline details. if l_response.status = 200 and then attached l_response.body as l_body then if attached {LIST [TWITTER_TWEETS]} (create {TWITTER_JSON}).user_timeline (l_body) as l_tweets then across l_tweets as t loop print (t.item.full_out) io.put_new_line end end else print ("%NResponse: STATUS:" + l_response.status.out) end

Let's see how this work in details. The class TWITTER_JSON is responsible for parsing the JSON input and generate the corresponding Twitter object, in this case, l_tweets, calling the feature user_timeline .

user_timeline (a_string: STRING): detachable LIST [TWITTER_TWEETS] require a_string_attached: a_string /= Void local i: INTEGER do if attached parsed_json (a_string) as j then if attached {JSON_ARRAY} j as l_array then from create {ARRAYED_LIST [TWITTER_TWEETS]} Result.make (l_array.count) i := 1 until i > l_array.count loop Result.force (twitter_tweets (Void, l_array.i_th (i))) i := i + 1 end end end end

If the feature {TWITTER_JSON}).user_timeline (l_body) can't parse the body, it will return Void, here we are not handling errors for simplicity. In other case we will iterate over the array of twitter tweets and we build the LIST [TWITTER_TWEETS] using the feature twitter_tweets.

twitter_tweets (a_tweet: detachable like twitter_tweets; a_json: JSON_VALUE): TWITTER_TWEETS -- Fill 'a_tweet' from 'a_json'. require a_json_attached: a_json /= Void do if a_tweet /= Void then Result := a_tweet else create Result end Result.set_created_at (string_value_from_json (a_json, "created_at")) Result.set_text (string_value_from_json (a_json, "text")) if attached {JSON_OBJECT} json_value (a_json, "user") as l_user then Result.set_user (twitter_user (Void, l_user)) end end

Code Example

Get the code from here: https://github.com/EiffelWebFramework/cypress

apis \twitter \twitter_tutorial \read_timeline