changeset 14:001e2aa380ad

getexr: add alert.
author pyon@macmini
date Mon, 08 Jan 2018 10:16:11 +0900
parents 4fb189ae0a46
children 3673b244ad94
files src/getexr/getexr.go
diffstat 1 files changed, 81 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/getexr/getexr.go	Wed Jan 03 08:10:34 2018 +0900
+++ b/src/getexr/getexr.go	Mon Jan 08 10:16:11 2018 +0900
@@ -1,6 +1,7 @@
 package main
 
 import (
+    "bytes"
     "fmt"
     "flag"
     "io/ioutil"
@@ -16,7 +17,7 @@
     "golang.org/x/net/html"
 )
 
-var ver = "0.1.1"
+var ver = "0.2.0"
 
 func main() {
 
@@ -30,6 +31,9 @@
         os.Exit( 0 )
     }
 
+    t := time.Now()
+    now := t.Format( "2006-01-02 15:04:05" )
+
     // Get USD Rate
     PrintVerbose( *verbose, "> get rate from yahoo..." )
     url := "http://finance.yahoo.co.jp/"
@@ -37,7 +41,7 @@
     if err != nil {
         log.Fatal( err )
     }
-    PrintVerbose( *verbose, "done\n" )
+    PrintVerbose( *verbose, "success\n" )
 
     z := html.NewTokenizer( resp.Body )
     usd, err := ParseHtml( z )
@@ -45,39 +49,65 @@
         log.Fatal( err )
     }
     defer resp.Body.Close()
+    PrintVerbose( *verbose, fmt.Sprintf( "> rate = %.2f\n", usd ) )
 
-    // Compare Past Rate
     content, err := ioutil.ReadFile( *file )
     if err != nil {
         log.Fatal( err )
     }
+    fc := 0
 
     buf1 := strings.Split( string( content ), "\n" )
-    buf2 := strings.Split( buf1[ len( buf1 ) - 2 ], "," )
+
+    // Compare Alert Rate
+    buf2 := strings.Split( buf1[ 0 ], "," )
+    flg := buf2[0]
+    thr, _ := strconv.ParseFloat( buf2[1], 32 )
+
+    if ( strings.EqualFold( "H", flg ) && thr < usd ) {
+        fc = -1
+        content = bytes.Replace( content, []byte( "H" ), []byte( "-" ), 1 )
+        if err := AlertMail( "H", now, usd ); err != nil {
+            log.Fatal( err )
+        }
+        PrintVerbose( *verbose, "> rate higher.\n> sending alert mail.\n" )
+
+    } else if ( strings.EqualFold( "L", flg ) && thr > usd ) {
+        fc = -1
+        content = bytes.Replace( content, []byte( "L" ), []byte( "-" ), 1 )
+        if err := AlertMail( "L", now, usd ); err != nil {
+            log.Fatal( err )
+        }
+        PrintVerbose( *verbose, "> rate lower.\n> sending alert mail.\n" )
+    }
+
+    // Compare Past Rate
+    buf2 = strings.Split( buf1[ len( buf1 ) - 2 ], "," )
+    ymd0 := buf2[0]
     usd0, _ := strconv.ParseFloat( buf2[1], 32 )
 
     if ( math.Abs( usd0 - usd ) > 1.0 ) {
-        PrintVerbose( *verbose, "> rate changed.\n" )
-        t := time.Now()
-        now := t.Format( "2006-01-02 15:04:05" )
-
-        // Save Rate
-        buf := fmt.Sprintf( "%s,%.2f\n", now, usd )
-        content = append( content,  buf... )
-
-        err = ioutil.WriteFile( *file, content, 0777 )
-        if err != nil {
+        fc = 1
+        // Post E-mail
+        PrintVerbose( *verbose, "> sending notify mail.\n" )
+        if err := NotifyMail( now, usd, ymd0, usd0 ); err != nil {
             log.Fatal( err )
         }
+        PrintVerbose( *verbose, "> rate changed.\n" )
+    } else {
+        PrintVerbose( *verbose, "> not changed.\n" )
+    }
 
-        // Post E-mail
-        PrintVerbose( *verbose, "> sending mail.\n" )
-        err = SendMail( now, usd, buf2[0], usd0 )
-        if err != nil {
+    // Save Rate
+    if ( fc != 0 ) {
+        if ( fc == 1 ) {
+            buf := fmt.Sprintf( "%s,%.2f\n", now, usd )
+            content = append( content,  buf... )
+        }
+        if err := ioutil.WriteFile( *file, content, 0777 ); err != nil {
             log.Fatal( err )
         }
-    } else {
-        PrintVerbose( *verbose, "> no change.\n" )
+        PrintVerbose( *verbose, "> data file overwrited.\n" )
     }
 
     PrintVerbose( *verbose, "> finish.\n" )
@@ -102,20 +132,41 @@
     }
 }
 
-func SendMail( now string, usd float64, old string, usd0 float64 ) error {
+func NotifyMail( now string, usd float64, old string, usd0 float64 ) error {
     address := "muty@willcom.com"
+    msg := "To: " + address + "\r\n" +
+           "Subject: Exchange-Mail\r\n" +
+           "\r\n" +
+           "rate changed\r\n" +
+           now + "\r\n" +
+           fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n\r\n" +
+           old + "\r\n" +
+           fmt.Sprintf( "[ USD/JPY : %.2f ].", usd0 ) + "\r\n" // mail body
+    if err := SendMail( address, msg ); err != nil {
+        return err
+    }
+    return nil
+}
+
+func AlertMail( flg, now string, usd float64 ) error {
+    address := "muty@willcom.com"
+    msg := "To: " + address + "\r\n" +
+           "Subject: Exchange-Mail\r\n" +
+           "\r\n" +
+           "rate changed\r\n" +
+           now + "\r\n" +
+           fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n" // mail body
+    if err := SendMail( address, msg ); err != nil {
+        return err
+    }
+    return nil
+}
+
+func SendMail( address, msg string ) error {
     hostname := "sdm.sakura.ne.jp"
     auth := smtp.PlainAuth( "", "bad_user@sdm.sakura.ne.jp", "hogehoge3", hostname )
     to := []string{ address }
-    msg := []byte( "To: " + address + "\r\n" +
-                "Subject: Exchange-Mail\r\n" +
-                "\r\n" +
-                "rate changed\r\n" +
-                now + "\r\n" +
-                fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n\r\n" +
-                old + "\r\n" +
-                fmt.Sprintf( "[ USD/JPY : %.2f ].", usd0 ) + "\r\n" ) // mail body
-    err := smtp.SendMail( hostname + ":587", auth, "bad_user@sdm.sakura.ne.jp", to, msg )
+    err := smtp.SendMail( hostname + ":587", auth, "bad_user@sdm.sakura.ne.jp", to, []byte( msg ) )
     if err != nil {
         return err
     }