Created
February 25, 2012 18:10
-
-
Save agnaldo4j/1909864 to your computer and use it in GitHub Desktop.
Coletando seus amigos no twitter com scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.softsimples.social.twitter | |
import org.scribe.model.{Response, Token} | |
import com.softsimples.social.social.json.Json | |
import scala.collection.mutable.MutableList | |
import com.softsimples.social.social.oauth.OAuthWrapper | |
import scala.math.ceil | |
object CollectTwitterFriends { | |
val NUMBER_OF_USERS_PER_PAGE = 100 | |
val ATTRIBUTE_IN_JSON_WITH_IDS = "ids" | |
} | |
class CollectTwitterFriends(val wrapper:OAuthWrapper, basePath:String, accessToken:Token) { | |
def requestFriends: List[Map[String, Any]] = { | |
if (accessToken == null) throw new IllegalStateException("AccessToken not yet ajusted") | |
val response:Response = wrapper.sendRequest( wrapper.buildGetRequest(basePath+"friends/ids.json", accessToken) ) | |
fillInformationOfUsers( transformResponseToListOfIDs(response) ) | |
} | |
private def transformResponseToListOfIDs(response:Response): List[Int] = { | |
val result = Json.parse(wrapper.getResponseBody(response)).asInstanceOf[Map[String, Any]] | |
result.get(CollectTwitterFriends.ATTRIBUTE_IN_JSON_WITH_IDS).get.asInstanceOf[List[Int]] | |
} | |
private def fillInformationOfUsers(userIds:List[Int]): List[Map[String, Any]] = { | |
val pages = numberOfPages(userIds) | |
var currentStart = 0 | |
val users = MutableList[Map[String, Any]]() | |
for(i <- 1 to pages) { | |
val pageWithUsers = retrieveListOfUsers( buildStringOf100IdsSeparedByComma(userIds, currentStart) ) | |
for(cada <- pageWithUsers) users += cada | |
currentStart += CollectTwitterFriends.NUMBER_OF_USERS_PER_PAGE; | |
} | |
users.toList | |
} | |
private def numberOfPages(userIds:List[Int]): Int = { | |
ceil(userIds.size.toDouble / CollectTwitterFriends.NUMBER_OF_USERS_PER_PAGE.toDouble).toInt | |
} | |
private def retrieveListOfUsers(ids:String): List[Map[String, Any]] = { | |
parseInformationOfUsers( requestInformationOfUsers(ids) ) | |
} | |
private def requestInformationOfUsers(ids:String): Response = { | |
val request = wrapper.buildGetRequest(basePath+"users/lookup.json?user_id="+ids, accessToken) | |
wrapper.sendRequest( request ) | |
} | |
private def parseInformationOfUsers(response:Response): List[Map[String, Any]] = { | |
val jsonString = wrapper.getResponseBody(response) | |
Json.parse(jsonString).asInstanceOf[List[Map[String, Any]]] | |
} | |
private def buildStringOf100IdsSeparedByComma(userIds:List[Int], start:Int): String = { | |
val toExecute = userIds.size - start | |
val end = calculeEnd(start, toExecute) | |
fillStringBuffer(userIds, start, end) | |
} | |
private def fillStringBuffer(userIds:List[Int], start:Int, end:Int): String = { | |
val buffer = new StringBuffer() | |
for(i <- start until end) { | |
if (i > 0) buffer.append(",") | |
buffer.append( userIds(i).toLong ) | |
} | |
buffer.toString | |
} | |
private def calculeEnd(start:Int, toExecute:Int): Int = { | |
if (toExecute >= CollectTwitterFriends.NUMBER_OF_USERS_PER_PAGE) CollectTwitterFriends.NUMBER_OF_USERS_PER_PAGE | |
else start + toExecute | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment