Skip to content

Instantly share code, notes, and snippets.

@toby55kij
Created December 7, 2010 15:54
Show Gist options
  • Select an option

  • Save toby55kij/731942 to your computer and use it in GitHub Desktop.

Select an option

Save toby55kij/731942 to your computer and use it in GitHub Desktop.
G*ワークショップ&忘年LT大会(2010/12/09)で発表した内容のGroovyスクリプトです。
//GetScoreRuns.groovy - bjリーグの2010-2011チーム別総得点・総失点を出力する
@Grab('net.sourceforge.htmlunit:htmlunit:2.8')
import java.net.URL
import com.gargoylesoftware.htmlunit.BrowserVersion
import com.gargoylesoftware.htmlunit.WebClient
proxyHost = null // set the proxy host name if you are in Firewall
proxyPort = 18080
if (proxyHost != null) {
c = new WebClient(BrowserVersion.FIREFOX_3, proxyHost , proxyPort)
} else {
c = new WebClient(BrowserVersion.FIREFOX_3)
}
//各チームの結果を書き込む
data = [:]
//チーム名を取り出す
def getTeam = {
it[0].textContent.normalize().stripIndent().stripMargin().readLines()[1]
}
//得点を取り出す
def getScore = {
it[0].textContent.normalize().stripIndent().stripMargin().readLines()[2].toInteger()
}
//試合数を結果に追加
def addGame = { data, team ->
if (!data[team]) {
data[team] = [game:0, score:0, runs:0, win:0, lose:0]
}
data[team].game++
}
//bjtvから結果一覧を取得
url = "http://www.basketballjapantv.com/members/mypage?search_team_id=&search_month_in=&x=47&y=11&year_in=2010"
allIndexPage = c.getPage(new URL(url))
//結果一覧から各試合の結果URLを取り出す
xpathTarget = "//a[starts-with(@href,'http://www.bj-league.com/bj/GameDetail.do')]"
anchors = allIndexPage.getByXPath(xpathTarget)
//各試合について
anchors.each {
//試合結果を取得
gameURL = it.hrefAttribute
println gameURL
resultPage = c.getPage(new URL(gameURL))
//チーム名を取得
team1 = getTeam(resultPage.getByXPath("//div[@class='team1']//a"))
addGame(data, team1)
team2 = getTeam(resultPage.getByXPath("//div[@class='team2']//a"))
addGame(data, team2)
//各チームの得点を取得
score1 = getScore(resultPage.getByXPath("//th[@class='scr1']"))
score2 = getScore(resultPage.getByXPath("//th[@class='scr2']"))
//チーム毎に得点・失点を記録
data[team1].score += score1
data[team1].runs += score2
data[team2].score += score2
data[team2].runs += score1
//チームの勝敗を記録
if (score1 > score2) {
data[team1].win++
data[team2].lose++
} else {
data[team1].lose++
data[team2].win++
}
}
//
c.closeAllWindows()
//各チームの結果を勝率でソート
sortData = data.sort { entry ->
entry.value.win / entry.value.game
}
//各チームの結果を勝率の良い方から表示
sortData.reverseEach { entry ->
println "${entry.key} 試合数:${entry.value.game}"
println "総得点:${entry.value.score}, 総失点:${entry.value.runs}, 総得失点差:${entry.value.score - entry.value.runs}"
}
//TODO:各カンファレンス毎に結果を表示
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment