Created
September 17, 2008 14:30
-
-
Save ymnk/11239 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 | |
import java.text.SimpleDateFormat | |
import java.util.Locale | |
object TwitterTimeline { | |
def main(arg:Array[String]){ | |
val url = if(arg.length == 2) { | |
val (username, passwd) = (arg(0), arg(1)) | |
Authenticator.setDefault( | |
new Authenticator { | |
override def getPasswordAuthentication = { | |
new PasswordAuthentication(username, passwd.toCharArray); | |
} | |
} | |
) | |
"http://twitter.com/statuses/friends_timeline.xml" | |
} | |
else { | |
"http://twitter.com/statuses/public_timeline.xml" | |
} | |
val df = new SimpleDateFormat("EEE MMM dd HH:mm:ss +0000 yyyy", Locale.US) | |
val interval = 60 * 1000 | |
def loop(_lastTime:Long) { | |
var lastTime=_lastTime | |
val urlConn = new URL(url).openConnection.asInstanceOf[HttpURLConnection] | |
urlConn.connect(); | |
urlConn.getResponseCode | |
for (s <- XML.load(urlConn.getInputStream) \ "status" reverse ; | |
created_at = s \ "created_at" text ; | |
time = df.parse(created_at).getTime if(time>lastTime)) { | |
lastTime=time | |
var (text, user_name) = (s \ "text" text, s \ "user" \ "name" text) | |
println(created_at+" ["+user_name+"] "+text) | |
} | |
Thread.sleep(interval) | |
loop(lastTime) | |
} | |
loop(0L) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment