Scala version of an emailer. So far it finds the lazy players. It needs to send them email yet. scala-emailer
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 26 Nov 2009 20:06:56 +0100
branchscala-emailer
changeset 153ac818c32c6e9
parent 152 07e3bcb65c1d
child 154 d9d5376d5f23
Scala version of an emailer. So far it finds the lazy players. It needs to send them email yet.
emailer/pom.xml
emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala
emailer/src/test/scala/cz/xelfi/quoridor/emailer/MySpec.scala
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emailer/pom.xml	Thu Nov 26 20:06:56 2009 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     1.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     1.7 +  <modelVersion>4.0.0</modelVersion>
     1.8 +  <parent>
     1.9 +    <artifactId>all-quoridor</artifactId>
    1.10 +    <groupId>org.apidesign</groupId>
    1.11 +    <version>1.0</version>
    1.12 +  </parent>
    1.13 +  <groupId>org.apidesign</groupId>
    1.14 +  <artifactId>emailer</artifactId>
    1.15 +  <version>1.0</version>
    1.16 +  <inceptionYear>2008</inceptionYear>
    1.17 +  <build>
    1.18 +    <sourceDirectory>src/main/scala</sourceDirectory>
    1.19 +    <testSourceDirectory>src/test/scala</testSourceDirectory>
    1.20 +    <plugins>
    1.21 +      <plugin>
    1.22 +        <groupId>org.scala-tools</groupId>
    1.23 +        <artifactId>maven-scala-plugin</artifactId>
    1.24 +        <executions>
    1.25 +          <execution>
    1.26 +            <goals>
    1.27 +              <goal>compile</goal>
    1.28 +              <goal>testCompile</goal>
    1.29 +            </goals>
    1.30 +          </execution>
    1.31 +        </executions>
    1.32 +        <configuration>
    1.33 +          <scalaVersion>${scala.version}</scalaVersion>
    1.34 +          <args>
    1.35 +            <arg>-target:jvm-1.5</arg>
    1.36 +          </args>
    1.37 +        </configuration>
    1.38 +      </plugin>
    1.39 +    </plugins>
    1.40 +  </build>
    1.41 +  <repositories>
    1.42 +    <repository>
    1.43 +      <id>scala-tools.org</id>
    1.44 +      <name>Scala-Tools Maven2 Repository</name>
    1.45 +      <url>http://scala-tools.org/repo-releases</url>
    1.46 +    </repository>
    1.47 +  </repositories>
    1.48 +  <pluginRepositories>
    1.49 +    <pluginRepository>
    1.50 +      <id>scala-tools.org</id>
    1.51 +      <name>Scala-Tools Maven2 Repository</name>
    1.52 +      <url>http://scala-tools.org/repo-releases</url>
    1.53 +    </pluginRepository>
    1.54 +  </pluginRepositories>
    1.55 +  <dependencies>
    1.56 +    <dependency>
    1.57 +      <groupId>org.scala-lang</groupId>
    1.58 +      <artifactId>scala-library</artifactId>
    1.59 +      <version>${scala.version}</version>
    1.60 +    </dependency>
    1.61 +    <dependency>
    1.62 +      <groupId>junit</groupId>
    1.63 +      <artifactId>junit</artifactId>
    1.64 +      <version>4.4</version>
    1.65 +      <scope>test</scope>
    1.66 +    </dependency>
    1.67 +    <dependency>
    1.68 +      <groupId>org.specs</groupId>
    1.69 +      <artifactId>specs</artifactId>
    1.70 +      <version>1.2.5</version>
    1.71 +      <scope>test</scope>
    1.72 +    </dependency>
    1.73 +    <!--
    1.74 +    <dependency>
    1.75 +      <groupId>javax.mail</groupId>
    1.76 +      <artifactId>mail</artifactId>
    1.77 +      <version>1.4.2</version>
    1.78 +    </dependency>
    1.79 +    -->
    1.80 +  </dependencies>
    1.81 +  <reporting>
    1.82 +    <plugins>
    1.83 +      <plugin>
    1.84 +        <groupId>org.scala-tools</groupId>
    1.85 +        <artifactId>maven-scala-plugin</artifactId>
    1.86 +        <configuration>
    1.87 +          <scalaVersion>${scala.version}</scalaVersion>
    1.88 +        </configuration>
    1.89 +      </plugin>
    1.90 +    </plugins>
    1.91 +  </reporting>
    1.92 +  <properties>
    1.93 +    <scala.version>2.7.0</scala.version>
    1.94 +  </properties>
    1.95 +    <name>Emails lazy players</name>
    1.96 +    <packaging>jar</packaging>
    1.97 +    <description>Obtains list of players without any activity for a while and sends them an email.</description>
    1.98 +</project>
    1.99 +
   1.100 +
   1.101 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala	Thu Nov 26 20:06:56 2009 +0100
     2.3 @@ -0,0 +1,111 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * The contents of this file are subject to the terms of either the GNU
     2.8 + * General Public License Version 2 only ("GPL") or the Common
     2.9 + * Development and Distribution License("CDDL") (collectively, the
    2.10 + * "License"). You may not use this file except in compliance with the
    2.11 + * License. You can obtain a copy of the License at
    2.12 + * http://www.netbeans.org/cddl-gplv2.html
    2.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.14 + * specific language governing permissions and limitations under the
    2.15 + * License.  When distributing the software, include this License Header
    2.16 + * Notice in each file and include the License file at
    2.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.18 + * particular file as subject to the "Classpath" exception as provided
    2.19 + * by Sun in the GPL Version 2 section of the License file that
    2.20 + * accompanied this code. If applicable, add the following below the
    2.21 + * License Header, with the fields enclosed by brackets [] replaced by
    2.22 + * your own identifying information:
    2.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.24 + *
    2.25 + * Contributor(s):
    2.26 + *
    2.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    2.28 + */
    2.29 +
    2.30 +package cz.xelfi.quoridor.emailer
    2.31 +
    2.32 +import scala.xml._
    2.33 +import java.net.URL
    2.34 +import scala.io._
    2.35 +import scala.collection.Map
    2.36 +import scala.collection.mutable.HashMap
    2.37 +
    2.38 +object Main {
    2.39 +  def main(args: Array[String]) {
    2.40 +    val api = new URL("http://quoridor.xelfi.cz/api/");
    2.41 +
    2.42 +    val id = login(api, "emailer", "otravuj")
    2.43 +    Console.println("id: " + id)
    2.44 +    
    2.45 +    val ret = allPlayers(
    2.46 +      api,
    2.47 +      3600 * 1000 * 24
    2.48 +    )
    2.49 +    for (val address <- emails(api, id, ret.keySet)) {
    2.50 +        Console.println(address)
    2.51 +    }
    2.52 +  }
    2.53 +
    2.54 +  def login(url : URL, name : String, password : String) : String = {
    2.55 +    val u = new URL(url, "login?name=" + name + "&password=" + password);
    2.56 +    val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
    2.57 +    conn.setRequestMethod("PUT")
    2.58 +    conn.setDoInput(true)
    2.59 +    conn.connect
    2.60 +    val reply = Source.fromInputStream(conn.getInputStream)
    2.61 +    return reply.mkString("")
    2.62 +  }
    2.63 +
    2.64 +  def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = {
    2.65 +    val u = new URL(url, "games");
    2.66 +    val conn = u.openConnection
    2.67 +    conn.setRequestProperty("Accept", "text/xml")
    2.68 +
    2.69 +    val ret = new HashMap[String,List[Node]]
    2.70 +
    2.71 +    val before = System.currentTimeMillis - okDelay
    2.72 +    val games = XML.load(conn.getInputStream)
    2.73 +    for (val g : Node <- games \\ "gameId") {
    2.74 +      val modified = (g \\ "@modified").text.toLong
    2.75 +      if (modified < before) {
    2.76 +        val status = (g \\ "@status").text;
    2.77 +        val name =
    2.78 +          if (status == "blackMove") {
    2.79 +            (g \\ "@black").text
    2.80 +          } else if (status == "whiteMove") {
    2.81 +            (g \\ "@white").text
    2.82 +          } else {
    2.83 +            null
    2.84 +          }
    2.85 +        if (name != null) {
    2.86 +          val prev = ret.getOrElse(name, Nil)
    2.87 +          val next = g :: prev
    2.88 +          ret.put(name, next);
    2.89 +        }
    2.90 +      }
    2.91 +    }
    2.92 +    return ret;
    2.93 +  }
    2.94 +
    2.95 +  def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = {
    2.96 +    var ret : List[String] = Nil;
    2.97 +    for (val p <- ids) {
    2.98 +      val u = new URL(url, "users/" + p + "?loginID=" + loginID)
    2.99 +      val conn = u.openConnection
   2.100 +      conn.setRequestProperty("Accept", "text/xml")
   2.101 +      val xml = XML.load(conn.getInputStream)
   2.102 +      val res = (xml \\ "property").filter({
   2.103 +          n => n.attribute("name") match {
   2.104 +            case Some(attr) => attr.text == "email"
   2.105 +            case _ => false
   2.106 +          }
   2.107 +      })
   2.108 +      if (!res.isEmpty) {
   2.109 +        ret = res(0).text :: ret;
   2.110 +      }
   2.111 +    }
   2.112 +    return ret
   2.113 +  }
   2.114 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala	Thu Nov 26 20:06:56 2009 +0100
     3.3 @@ -0,0 +1,17 @@
     3.4 +package cz.xelfi.quoridor.emailer
     3.5 +
     3.6 +import org.junit._
     3.7 +import Assert._
     3.8 +
     3.9 +@Test
    3.10 +class AppTest {
    3.11 +
    3.12 +    @Test
    3.13 +    def testOK() = assertTrue(true)
    3.14 +
    3.15 +//    @Test
    3.16 +//    def testKO() = assertTrue(false)
    3.17 +
    3.18 +}
    3.19 +
    3.20 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MySpec.scala	Thu Nov 26 20:06:56 2009 +0100
     4.3 @@ -0,0 +1,17 @@
     4.4 +package org.apidesign.emailer
     4.5 +
     4.6 +import org.specs._
     4.7 +import org.specs.runner.{ConsoleRunner, JUnit4}
     4.8 +
     4.9 +class MySpecTest extends JUnit4(MySpec)
    4.10 +//class MySpecSuite extends ScalaTestSuite(MySpec)
    4.11 +object MySpecRunner extends ConsoleRunner(MySpec)
    4.12 +
    4.13 +object MySpec extends Specification {
    4.14 +  "This wonderful system" should {
    4.15 +    "save the world" in {
    4.16 +      val list = Nil
    4.17 +      list must beEmpty
    4.18 +    }
    4.19 +  }
    4.20 +}
     5.1 --- a/pom.xml	Thu Nov 19 09:35:29 2009 +0100
     5.2 +++ b/pom.xml	Thu Nov 26 20:06:56 2009 +0100
     5.3 @@ -38,6 +38,7 @@
     5.4      <module>visidor</module>
     5.5      <module>webidor</module>
     5.6      <module>freemarkerdor</module>
     5.7 +    <module>emailer</module>
     5.8    </modules>
     5.9      <name>Quoridor related projects</name>
    5.10      <description>Master project that agregates all quoridor related functionality.