Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created March 21, 2010 09:17
Show Gist options
  • Save yuroyoro/339173 to your computer and use it in GitHub Desktop.
Save yuroyoro/339173 to your computer and use it in GitHub Desktop.
import java.net.{Authenticator, PasswordAuthentication}
import scala.Math._
import scala.xml._
import scala.io.Source
object TwitterRetweetCrawler {
val rtUrl ="http://api.twitter.com/statuses/retweets_of_me.xml"
val stUrl ="http://api.twitter.com/1/statuses/retweets/%s.xml"
// Usage : scala TwitterRetweetCrawler <your_id> <your_password>
def main(args:Array[String]) = {
val Array( userid, passwd ) = args
// Basic認証
Authenticator.setDefault(
new Authenticator {
override def getPasswordAuthentication =
new PasswordAuthentication( userid, passwd.toCharArray)
}
)
// RetweetされたPostを取得する
val source = Source.fromURL( rtUrl )
val xml = XML.loadString( source.getLines.mkString )
(xml \ "status").map{ s =>
val id = ( s \ "id" text).toLong
val text = s \ "text" text
val retweetedBy = {
val stxml = XML.loadString(
Source.fromURL( stUrl format id ).getLines.mkString )
(stxml \ "status" \ "user" ).map { st =>
( st \ "name" text , st \ "screen_name" text ) }
}
( id, text, retweetedBy )
}.foreach{ case( id, text, retweetedBy ) =>
val cxml = XML.loadString("<c>" + (text) + "</c>")
println("""
%s
retweeted by %s users.[%s]
""".format( cxml.child.text, retweetedBy.size, retweetedBy.map{ case(name, screen) =>
"%s(%s)".format( name, screen ) }.mkString(",")))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment