1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Thu Nov 26 20:06:56 2009 +0100
1.3 @@ -0,0 +1,111 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.emailer
1.31 +
1.32 +import scala.xml._
1.33 +import java.net.URL
1.34 +import scala.io._
1.35 +import scala.collection.Map
1.36 +import scala.collection.mutable.HashMap
1.37 +
1.38 +object Main {
1.39 + def main(args: Array[String]) {
1.40 + val api = new URL("http://quoridor.xelfi.cz/api/");
1.41 +
1.42 + val id = login(api, "emailer", "otravuj")
1.43 + Console.println("id: " + id)
1.44 +
1.45 + val ret = allPlayers(
1.46 + api,
1.47 + 3600 * 1000 * 24
1.48 + )
1.49 + for (val address <- emails(api, id, ret.keySet)) {
1.50 + Console.println(address)
1.51 + }
1.52 + }
1.53 +
1.54 + def login(url : URL, name : String, password : String) : String = {
1.55 + val u = new URL(url, "login?name=" + name + "&password=" + password);
1.56 + val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
1.57 + conn.setRequestMethod("PUT")
1.58 + conn.setDoInput(true)
1.59 + conn.connect
1.60 + val reply = Source.fromInputStream(conn.getInputStream)
1.61 + return reply.mkString("")
1.62 + }
1.63 +
1.64 + def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = {
1.65 + val u = new URL(url, "games");
1.66 + val conn = u.openConnection
1.67 + conn.setRequestProperty("Accept", "text/xml")
1.68 +
1.69 + val ret = new HashMap[String,List[Node]]
1.70 +
1.71 + val before = System.currentTimeMillis - okDelay
1.72 + val games = XML.load(conn.getInputStream)
1.73 + for (val g : Node <- games \\ "gameId") {
1.74 + val modified = (g \\ "@modified").text.toLong
1.75 + if (modified < before) {
1.76 + val status = (g \\ "@status").text;
1.77 + val name =
1.78 + if (status == "blackMove") {
1.79 + (g \\ "@black").text
1.80 + } else if (status == "whiteMove") {
1.81 + (g \\ "@white").text
1.82 + } else {
1.83 + null
1.84 + }
1.85 + if (name != null) {
1.86 + val prev = ret.getOrElse(name, Nil)
1.87 + val next = g :: prev
1.88 + ret.put(name, next);
1.89 + }
1.90 + }
1.91 + }
1.92 + return ret;
1.93 + }
1.94 +
1.95 + def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = {
1.96 + var ret : List[String] = Nil;
1.97 + for (val p <- ids) {
1.98 + val u = new URL(url, "users/" + p + "?loginID=" + loginID)
1.99 + val conn = u.openConnection
1.100 + conn.setRequestProperty("Accept", "text/xml")
1.101 + val xml = XML.load(conn.getInputStream)
1.102 + val res = (xml \\ "property").filter({
1.103 + n => n.attribute("name") match {
1.104 + case Some(attr) => attr.text == "email"
1.105 + case _ => false
1.106 + }
1.107 + })
1.108 + if (!res.isEmpty) {
1.109 + ret = res(0).text :: ret;
1.110 + }
1.111 + }
1.112 + return ret
1.113 + }
1.114 +}