Mercurial > mercurial > hgweb_golang.cgi
comparison src/getexr/getexr.go @ 14:001e2aa380ad
getexr: add alert.
| author | pyon@macmini |
|---|---|
| date | Mon, 08 Jan 2018 10:16:11 +0900 |
| parents | 4fb189ae0a46 |
| children | 38b64afbaf79 |
comparison
equal
deleted
inserted
replaced
| 13:4fb189ae0a46 | 14:001e2aa380ad |
|---|---|
| 1 package main | 1 package main |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "bytes" | |
| 4 "fmt" | 5 "fmt" |
| 5 "flag" | 6 "flag" |
| 6 "io/ioutil" | 7 "io/ioutil" |
| 7 "log" | 8 "log" |
| 8 "math" | 9 "math" |
| 14 "time" | 15 "time" |
| 15 | 16 |
| 16 "golang.org/x/net/html" | 17 "golang.org/x/net/html" |
| 17 ) | 18 ) |
| 18 | 19 |
| 19 var ver = "0.1.1" | 20 var ver = "0.2.0" |
| 20 | 21 |
| 21 func main() { | 22 func main() { |
| 22 | 23 |
| 23 version := flag.Bool( "v", false, "print version" ) | 24 version := flag.Bool( "v", false, "print version" ) |
| 24 verbose := flag.Bool( "V", false, "verbose" ) | 25 verbose := flag.Bool( "V", false, "verbose" ) |
| 28 if ( *version ) { | 29 if ( *version ) { |
| 29 fmt.Fprintf( os.Stderr, "getexr - Get Exchange Rate [ version = %s ]\n",ver ) | 30 fmt.Fprintf( os.Stderr, "getexr - Get Exchange Rate [ version = %s ]\n",ver ) |
| 30 os.Exit( 0 ) | 31 os.Exit( 0 ) |
| 31 } | 32 } |
| 32 | 33 |
| 34 t := time.Now() | |
| 35 now := t.Format( "2006-01-02 15:04:05" ) | |
| 36 | |
| 33 // Get USD Rate | 37 // Get USD Rate |
| 34 PrintVerbose( *verbose, "> get rate from yahoo..." ) | 38 PrintVerbose( *verbose, "> get rate from yahoo..." ) |
| 35 url := "http://finance.yahoo.co.jp/" | 39 url := "http://finance.yahoo.co.jp/" |
| 36 resp, err := http.Get( url ) | 40 resp, err := http.Get( url ) |
| 37 if err != nil { | 41 if err != nil { |
| 38 log.Fatal( err ) | 42 log.Fatal( err ) |
| 39 } | 43 } |
| 40 PrintVerbose( *verbose, "done\n" ) | 44 PrintVerbose( *verbose, "success\n" ) |
| 41 | 45 |
| 42 z := html.NewTokenizer( resp.Body ) | 46 z := html.NewTokenizer( resp.Body ) |
| 43 usd, err := ParseHtml( z ) | 47 usd, err := ParseHtml( z ) |
| 44 if err != nil { | 48 if err != nil { |
| 45 log.Fatal( err ) | 49 log.Fatal( err ) |
| 46 } | 50 } |
| 47 defer resp.Body.Close() | 51 defer resp.Body.Close() |
| 52 PrintVerbose( *verbose, fmt.Sprintf( "> rate = %.2f\n", usd ) ) | |
| 48 | 53 |
| 49 // Compare Past Rate | |
| 50 content, err := ioutil.ReadFile( *file ) | 54 content, err := ioutil.ReadFile( *file ) |
| 51 if err != nil { | 55 if err != nil { |
| 52 log.Fatal( err ) | 56 log.Fatal( err ) |
| 53 } | 57 } |
| 58 fc := 0 | |
| 54 | 59 |
| 55 buf1 := strings.Split( string( content ), "\n" ) | 60 buf1 := strings.Split( string( content ), "\n" ) |
| 56 buf2 := strings.Split( buf1[ len( buf1 ) - 2 ], "," ) | 61 |
| 62 // Compare Alert Rate | |
| 63 buf2 := strings.Split( buf1[ 0 ], "," ) | |
| 64 flg := buf2[0] | |
| 65 thr, _ := strconv.ParseFloat( buf2[1], 32 ) | |
| 66 | |
| 67 if ( strings.EqualFold( "H", flg ) && thr < usd ) { | |
| 68 fc = -1 | |
| 69 content = bytes.Replace( content, []byte( "H" ), []byte( "-" ), 1 ) | |
| 70 if err := AlertMail( "H", now, usd ); err != nil { | |
| 71 log.Fatal( err ) | |
| 72 } | |
| 73 PrintVerbose( *verbose, "> rate higher.\n> sending alert mail.\n" ) | |
| 74 | |
| 75 } else if ( strings.EqualFold( "L", flg ) && thr > usd ) { | |
| 76 fc = -1 | |
| 77 content = bytes.Replace( content, []byte( "L" ), []byte( "-" ), 1 ) | |
| 78 if err := AlertMail( "L", now, usd ); err != nil { | |
| 79 log.Fatal( err ) | |
| 80 } | |
| 81 PrintVerbose( *verbose, "> rate lower.\n> sending alert mail.\n" ) | |
| 82 } | |
| 83 | |
| 84 // Compare Past Rate | |
| 85 buf2 = strings.Split( buf1[ len( buf1 ) - 2 ], "," ) | |
| 86 ymd0 := buf2[0] | |
| 57 usd0, _ := strconv.ParseFloat( buf2[1], 32 ) | 87 usd0, _ := strconv.ParseFloat( buf2[1], 32 ) |
| 58 | 88 |
| 59 if ( math.Abs( usd0 - usd ) > 1.0 ) { | 89 if ( math.Abs( usd0 - usd ) > 1.0 ) { |
| 60 PrintVerbose( *verbose, "> rate changed.\n" ) | 90 fc = 1 |
| 61 t := time.Now() | 91 // Post E-mail |
| 62 now := t.Format( "2006-01-02 15:04:05" ) | 92 PrintVerbose( *verbose, "> sending notify mail.\n" ) |
| 63 | 93 if err := NotifyMail( now, usd, ymd0, usd0 ); err != nil { |
| 64 // Save Rate | |
| 65 buf := fmt.Sprintf( "%s,%.2f\n", now, usd ) | |
| 66 content = append( content, buf... ) | |
| 67 | |
| 68 err = ioutil.WriteFile( *file, content, 0777 ) | |
| 69 if err != nil { | |
| 70 log.Fatal( err ) | 94 log.Fatal( err ) |
| 71 } | 95 } |
| 96 PrintVerbose( *verbose, "> rate changed.\n" ) | |
| 97 } else { | |
| 98 PrintVerbose( *verbose, "> not changed.\n" ) | |
| 99 } | |
| 72 | 100 |
| 73 // Post E-mail | 101 // Save Rate |
| 74 PrintVerbose( *verbose, "> sending mail.\n" ) | 102 if ( fc != 0 ) { |
| 75 err = SendMail( now, usd, buf2[0], usd0 ) | 103 if ( fc == 1 ) { |
| 76 if err != nil { | 104 buf := fmt.Sprintf( "%s,%.2f\n", now, usd ) |
| 105 content = append( content, buf... ) | |
| 106 } | |
| 107 if err := ioutil.WriteFile( *file, content, 0777 ); err != nil { | |
| 77 log.Fatal( err ) | 108 log.Fatal( err ) |
| 78 } | 109 } |
| 79 } else { | 110 PrintVerbose( *verbose, "> data file overwrited.\n" ) |
| 80 PrintVerbose( *verbose, "> no change.\n" ) | |
| 81 } | 111 } |
| 82 | 112 |
| 83 PrintVerbose( *verbose, "> finish.\n" ) | 113 PrintVerbose( *verbose, "> finish.\n" ) |
| 84 } | 114 } |
| 85 | 115 |
| 100 } | 130 } |
| 101 } | 131 } |
| 102 } | 132 } |
| 103 } | 133 } |
| 104 | 134 |
| 105 func SendMail( now string, usd float64, old string, usd0 float64 ) error { | 135 func NotifyMail( now string, usd float64, old string, usd0 float64 ) error { |
| 106 address := "muty@willcom.com" | 136 address := "muty@willcom.com" |
| 137 msg := "To: " + address + "\r\n" + | |
| 138 "Subject: Exchange-Mail\r\n" + | |
| 139 "\r\n" + | |
| 140 "rate changed\r\n" + | |
| 141 now + "\r\n" + | |
| 142 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n\r\n" + | |
| 143 old + "\r\n" + | |
| 144 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd0 ) + "\r\n" // mail body | |
| 145 if err := SendMail( address, msg ); err != nil { | |
| 146 return err | |
| 147 } | |
| 148 return nil | |
| 149 } | |
| 150 | |
| 151 func AlertMail( flg, now string, usd float64 ) error { | |
| 152 address := "muty@willcom.com" | |
| 153 msg := "To: " + address + "\r\n" + | |
| 154 "Subject: Exchange-Mail\r\n" + | |
| 155 "\r\n" + | |
| 156 "rate changed\r\n" + | |
| 157 now + "\r\n" + | |
| 158 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n" // mail body | |
| 159 if err := SendMail( address, msg ); err != nil { | |
| 160 return err | |
| 161 } | |
| 162 return nil | |
| 163 } | |
| 164 | |
| 165 func SendMail( address, msg string ) error { | |
| 107 hostname := "sdm.sakura.ne.jp" | 166 hostname := "sdm.sakura.ne.jp" |
| 108 auth := smtp.PlainAuth( "", "bad_user@sdm.sakura.ne.jp", "hogehoge3", hostname ) | 167 auth := smtp.PlainAuth( "", "bad_user@sdm.sakura.ne.jp", "hogehoge3", hostname ) |
| 109 to := []string{ address } | 168 to := []string{ address } |
| 110 msg := []byte( "To: " + address + "\r\n" + | 169 err := smtp.SendMail( hostname + ":587", auth, "bad_user@sdm.sakura.ne.jp", to, []byte( msg ) ) |
| 111 "Subject: Exchange-Mail\r\n" + | |
| 112 "\r\n" + | |
| 113 "rate changed\r\n" + | |
| 114 now + "\r\n" + | |
| 115 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n\r\n" + | |
| 116 old + "\r\n" + | |
| 117 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd0 ) + "\r\n" ) // mail body | |
| 118 err := smtp.SendMail( hostname + ":587", auth, "bad_user@sdm.sakura.ne.jp", to, msg ) | |
| 119 if err != nil { | 170 if err != nil { |
| 120 return err | 171 return err |
| 121 } | 172 } |
| 122 return nil | 173 return nil |
| 123 } | 174 } |
