Created
October 21, 2008 07:56
-
-
Save ymnk/18268 to your computer and use it in GitHub Desktop.
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
import java.net.{Authenticator, PasswordAuthentication} | |
import java.net.{URL, HttpURLConnection} | |
import scala.xml.{XML, Node} | |
import scala.collection.mutable.Map | |
class FriendOrFollow (screen_name:String) { | |
private val friends_url = "http://twitter.com/statuses/friends/%s.xml" | |
lazy val friends:Map[String, Node] = | |
getMap(friends_url.replace("%s", screen_name)) | |
private val followers_url = "http://twitter.com/statuses/followers/%s.xml" | |
lazy val followers:Map[String, Node] = | |
getMap(followers_url.replace("%s", screen_name)) | |
// intersection of sets of friends and followers users. | |
lazy val friendsandfollowers = friends.keySet ** followers.keySet | |
lazy val not_following_me_back:Map[String, Node] = | |
friends.clone -- friendsandfollowers.elements | |
lazy val not_following_them_back:Map[String, Node] = | |
followers.clone -- friendsandfollowers.elements | |
private final def getMap(url:String):Map[String, Node] = { | |
def page(n:Int, map:Map[String, Node]):Map[String, Node] = { | |
var urlConn = new URL(url+"?page="+n).openConnection match { | |
case con:HttpURLConnection => { | |
con.connect(); | |
con.getResponseCode match { | |
case 200 => { } | |
case _ => { } | |
} | |
con | |
} | |
} | |
val size=map.size | |
(XML.load(urlConn.getInputStream) \\ "user").foldLeft(map){ | |
(m, user)=> m + ((user \ "id" text) -> user) | |
} | |
match { | |
case _map if(_map.size<=size) => map | |
case _map => page(n+1, _map); | |
} | |
} | |
page(1, Map.empty[String, Node]) | |
} | |
} | |
object FriendOrFollowTest { | |
def main(arg:Array[String])={ | |
val (screen_name, username, passwd) = (arg(0), arg(1), arg(2)) | |
Authenticator.setDefault( | |
new Authenticator { | |
override def getPasswordAuthentication = { | |
new PasswordAuthentication(username, passwd.toCharArray); | |
} | |
} | |
) | |
val fof = new FriendOrFollow(screen_name) | |
println(screen_name+" is following these people, "+ | |
"but they are not following "+screen_name+" back.") | |
fof.not_following_me_back.foreach { case (id, node)=> | |
println(node \ "screen_name" text) | |
} | |
println("These people are following "+screen_name+", "+ | |
"but "+screen_name+" is not following them back.") | |
fof.not_following_them_back.foreach { case (id, node)=> | |
println(node \ "screen_name" text) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment