Skip to content

Instantly share code, notes, and snippets.

@agnaldo4j
Created February 25, 2012 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agnaldo4j/1910086 to your computer and use it in GitHub Desktop.
Save agnaldo4j/1910086 to your computer and use it in GitHub Desktop.
Recuperando a home e a user time line do twitter com scala
package com.softsimples.social.twitter
import com.softsimples.social.social.json.Json
import scala.collection.mutable.MutableList
import org.scribe.model.{Token, Response}
import com.softsimples.social.social.oauth.OAuthWrapper
class RetrieveTimeLine(val wrapper:OAuthWrapper, basePath:String, accessToken:Token) {
def userTimeLine(): List[Map[String, Any]] = {
if (accessToken == null) throw new IllegalStateException("AccessToken not yet ajusted")
val response:Response = wrapper.sendRequest( wrapper.buildGetRequest(basePath+"statuses/user_timeline.json", accessToken) )
parseMessages( wrapper.getResponseBody(response) )
}
def homeTimeLineSince(since_id:String): List[Map[String, Any]] = {
if (accessToken == null) throw new IllegalStateException("AccessToken not yet ajusted")
tryGet800Messages(since_id)
}
private def tryGet800Messages(since_id:String): List[Map[String, Any]] = {
val messages = MutableList[Map[String, Any]]()
for (page <- 1 to 4) processPage(since_id, page, messages)
messages.toList
}
private def processPage(since_id: String, page: Int, messages: MutableList[Map[String, Any]]) {
val messagesOnPage = parseMessages( requestHomeTimeLineSinceAndPage(since_id, page) )
processMessagesOnPage(messagesOnPage, messages)
}
private def parseMessages(result:String): List[Map[String, Any]] = {
Json.parse(result).asInstanceOf[List[Map[String, Any]]]
}
private def processMessagesOnPage(messagesOnPage:List[Map[String, Any]], messages: MutableList[Map[String, Any]]) {
if (messagesOnPage.size > 0) {
for (cada <- messagesOnPage) messages += cada
}
}
private def requestHomeTimeLineSinceAndPage(since_id:String, page:Int): String = {
val path = basePath+"statuses/home_timeline.json?exclude_replies=true&include_rts=true&count=200&since_id="+since_id+"&page="+page
val request = wrapper.buildGetRequest(path, accessToken)
val response:Response = wrapper.sendRequest( request )
wrapper.getResponseBody(response)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment