Created
August 4, 2010 12:15
-
-
Save yuroyoro/508039 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.util.Date | |
import java.text.SimpleDateFormat | |
import java.net.URL | |
import scala.xml._ | |
import scala.xml.parsing.XhtmlParser | |
import scala.io.Source | |
case class User( userId:Int, nickname:String, twitterId:Option[String], join:Boolean = true ) | |
class Event( eventId:String, eventXml:Node ) { | |
lazy val usersXml = XML.load( new URL( "http://api.atnd.org/events/users/?event_id=%s" format eventId )) | |
val sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+09:00'") | |
def value( tag:String ) = eventXml \ tag text | |
def optionValue( tag:String ) = (eventXml \ tag ).headOption.map{ v => v text }.filter{ _.nonEmpty } | |
def dateValue( tag:String ) = optionValue( tag ) map { d => sdf.parse(d) } | |
def intValue( tag:String ) = optionValue(tag) map { _.toInt } getOrElse(0) | |
def doubleValue( tag:String ) = optionValue(tag) map{ _.toDouble } getOrElse(0.0) | |
val title:String = value("title") | |
val catchs:String = value("catch") | |
val description:String = value("description") | |
val eventUrl:String = value("event-url") | |
val startedAt:Option[Date] = dateValue("started-at") | |
val endedAt:Option[Date] = dateValue("ended-at") | |
val url:String = value("url") | |
val limit:Int = intValue("limit") | |
val address:String = value("address") | |
val place:String = value("place") | |
val lat:Double = doubleValue("lat") | |
val lon:Double = doubleValue("lon") | |
val ownerId:Int = intValue("owner-id") | |
val ownerNickname:String = value("owner-nickname") | |
val accepted:Int = intValue("accepted") | |
val waiting:Int = intValue("waiting") | |
val updatedAt:Option[Date] = dateValue("updated-at") | |
lazy val users: Seq[User] = usersXml \\ "user" map { u => | |
val id = u \ "user-id" text | |
val tid = ATNDApi.twitterId( id.toInt ) filter { _ != "-" } | |
User( id.toInt, u \ "nickname" text, tid, ( u \ "status" text ) == "1" ) | |
} | |
lazy val joins = users.filter{ _.join } | |
lazy val waitings = users.filter{ _.join == false } | |
} | |
object ATNDApi { | |
private def loadEventXml( eventId:String ) = | |
XML.load( new URL("http://api.atnd.org/events/?event_id=%s" format eventId )) \\ "event" headOption | |
def event( eventId:String ):Option[Event] = | |
loadEventXml( eventId ) map { xml => new Event( eventId, xml ) } | |
def twitterId( id:Int):Option[String] = { | |
XhtmlParser( Source.fromURL( new URL("http://atnd.org/users/%s" format id ) )) \\ "dl" filter { dl => | |
(dl \ "dt" text) == "Twitter ID" | |
} filter { dl => | |
(dl \ "dt" text) == "Twitter ID" | |
} map { dl => dl \ "dd" text } headOption | |
} | |
} | |
object ATNDEventTwitterList { | |
def main( args:Array[String] ) = { | |
args.headOption.foreach { eventId => | |
ATNDApi.event( eventId ) foreach { event => | |
val users = event.users | |
val tmax = users.map{ _.twitterId }.flatten.map{ _.length }.max | |
val fmt = "%6d : %s : %-" + tmax + "s : %s" | |
println( event.title ) | |
users.map { u => | |
fmt format( u.userId, if( u.join ) "参加" else "補欠", u.twitterId.getOrElse("-"), u.nickname ) | |
} foreach { println } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment