Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.