emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 26 Nov 2009 20:06:56 +0100
branchscala-emailer
changeset 153 ac818c32c6e9
child 154 d9d5376d5f23
permissions -rw-r--r--
Scala version of an emailer. So far it finds the lazy players. It needs to send them email yet.
jtulach@153
     1
/*
jtulach@153
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@153
     3
 *
jtulach@153
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@153
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@153
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@153
     7
 * "License"). You may not use this file except in compliance with the
jtulach@153
     8
 * License. You can obtain a copy of the License at
jtulach@153
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@153
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@153
    11
 * specific language governing permissions and limitations under the
jtulach@153
    12
 * License.  When distributing the software, include this License Header
jtulach@153
    13
 * Notice in each file and include the License file at
jtulach@153
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@153
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@153
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@153
    17
 * accompanied this code. If applicable, add the following below the
jtulach@153
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@153
    19
 * your own identifying information:
jtulach@153
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@153
    21
 *
jtulach@153
    22
 * Contributor(s):
jtulach@153
    23
 *
jtulach@153
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@153
    25
 */
jtulach@153
    26
jtulach@153
    27
package cz.xelfi.quoridor.emailer
jtulach@153
    28
jtulach@153
    29
import scala.xml._
jtulach@153
    30
import java.net.URL
jtulach@153
    31
import scala.io._
jtulach@153
    32
import scala.collection.Map
jtulach@153
    33
import scala.collection.mutable.HashMap
jtulach@153
    34
jtulach@153
    35
object Main {
jtulach@153
    36
  def main(args: Array[String]) {
jtulach@153
    37
    val api = new URL("http://quoridor.xelfi.cz/api/");
jtulach@153
    38
jtulach@153
    39
    val id = login(api, "emailer", "otravuj")
jtulach@153
    40
    Console.println("id: " + id)
jtulach@153
    41
    
jtulach@153
    42
    val ret = allPlayers(
jtulach@153
    43
      api,
jtulach@153
    44
      3600 * 1000 * 24
jtulach@153
    45
    )
jtulach@153
    46
    for (val address <- emails(api, id, ret.keySet)) {
jtulach@153
    47
        Console.println(address)
jtulach@153
    48
    }
jtulach@153
    49
  }
jtulach@153
    50
jtulach@153
    51
  def login(url : URL, name : String, password : String) : String = {
jtulach@153
    52
    val u = new URL(url, "login?name=" + name + "&password=" + password);
jtulach@153
    53
    val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
jtulach@153
    54
    conn.setRequestMethod("PUT")
jtulach@153
    55
    conn.setDoInput(true)
jtulach@153
    56
    conn.connect
jtulach@153
    57
    val reply = Source.fromInputStream(conn.getInputStream)
jtulach@153
    58
    return reply.mkString("")
jtulach@153
    59
  }
jtulach@153
    60
jtulach@153
    61
  def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = {
jtulach@153
    62
    val u = new URL(url, "games");
jtulach@153
    63
    val conn = u.openConnection
jtulach@153
    64
    conn.setRequestProperty("Accept", "text/xml")
jtulach@153
    65
jtulach@153
    66
    val ret = new HashMap[String,List[Node]]
jtulach@153
    67
jtulach@153
    68
    val before = System.currentTimeMillis - okDelay
jtulach@153
    69
    val games = XML.load(conn.getInputStream)
jtulach@153
    70
    for (val g : Node <- games \\ "gameId") {
jtulach@153
    71
      val modified = (g \\ "@modified").text.toLong
jtulach@153
    72
      if (modified < before) {
jtulach@153
    73
        val status = (g \\ "@status").text;
jtulach@153
    74
        val name =
jtulach@153
    75
          if (status == "blackMove") {
jtulach@153
    76
            (g \\ "@black").text
jtulach@153
    77
          } else if (status == "whiteMove") {
jtulach@153
    78
            (g \\ "@white").text
jtulach@153
    79
          } else {
jtulach@153
    80
            null
jtulach@153
    81
          }
jtulach@153
    82
        if (name != null) {
jtulach@153
    83
          val prev = ret.getOrElse(name, Nil)
jtulach@153
    84
          val next = g :: prev
jtulach@153
    85
          ret.put(name, next);
jtulach@153
    86
        }
jtulach@153
    87
      }
jtulach@153
    88
    }
jtulach@153
    89
    return ret;
jtulach@153
    90
  }
jtulach@153
    91
jtulach@153
    92
  def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = {
jtulach@153
    93
    var ret : List[String] = Nil;
jtulach@153
    94
    for (val p <- ids) {
jtulach@153
    95
      val u = new URL(url, "users/" + p + "?loginID=" + loginID)
jtulach@153
    96
      val conn = u.openConnection
jtulach@153
    97
      conn.setRequestProperty("Accept", "text/xml")
jtulach@153
    98
      val xml = XML.load(conn.getInputStream)
jtulach@153
    99
      val res = (xml \\ "property").filter({
jtulach@153
   100
          n => n.attribute("name") match {
jtulach@153
   101
            case Some(attr) => attr.text == "email"
jtulach@153
   102
            case _ => false
jtulach@153
   103
          }
jtulach@153
   104
      })
jtulach@153
   105
      if (!res.isEmpty) {
jtulach@153
   106
        ret = res(0).text :: ret;
jtulach@153
   107
      }
jtulach@153
   108
    }
jtulach@153
   109
    return ret
jtulach@153
   110
  }
jtulach@153
   111
}