comparison src/getexr/getexr.go @ 61:49656dc40069

add qtuti.
author pyon@macmini
date Fri, 11 Sep 2020 20:06:27 +0900
parents 38b64afbaf79
children
comparison
equal deleted inserted replaced
60:058fb0a2cda8 61:49656dc40069
19 19
20 var ver = "0.2.0" 20 var ver = "0.2.0"
21 21
22 func main() { 22 func main() {
23 23
24 version := flag.Bool( "v", false, "print version" ) 24 version := flag.Bool("v", false, "print version")
25 verbose := flag.Bool( "V", false, "verbose" ) 25 verbose := flag.Bool("V", false, "verbose")
26 file := flag.String( "f", "usd.dat", "data file" ) 26 file := flag.String("f", "usd.dat", "data file")
27 flag.Parse() 27 flag.Parse()
28 28
29 if ( *version ) { 29 if (*version) {
30 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)
31 os.Exit( 0 ) 31 os.Exit(0)
32 } 32 }
33 33
34 t := time.Now() 34 t := time.Now()
35 now := t.Format( "2006-01-02 15:04:05" ) 35 now := t.Format("2006-01-02 15:04:05")
36 36
37 // Get USD Rate 37 // Get USD Rate
38 PrintVerbose( *verbose, "> get rate from yahoo..." ) 38 PrintVerbose(*verbose, "> get rate from yahoo...")
39 url := "http://finance.yahoo.co.jp/" 39 url := "http://finance.yahoo.co.jp/"
40 resp, err := http.Get( url ) 40 resp, err := http.Get(url)
41 if err != nil { 41 if err != nil {
42 log.Fatal( err ) 42 log.Fatal(err)
43 } 43 }
44 PrintVerbose( *verbose, "success\n" ) 44 PrintVerbose(*verbose, "success\n")
45 45
46 z := html.NewTokenizer( resp.Body ) 46 z := html.NewTokenizer(resp.Body)
47 usd, err := ParseHtml( z ) 47 usd, err := ParseHtml(z)
48 if err != nil { 48 if err != nil {
49 log.Fatal( err ) 49 log.Fatal(err)
50 } 50 }
51 defer resp.Body.Close() 51 defer resp.Body.Close()
52 PrintVerbose( *verbose, fmt.Sprintf( "> rate = %.2f\n", usd ) ) 52 PrintVerbose(*verbose, fmt.Sprintf("> rate = %.2f\n", usd))
53 53
54 content, err := ioutil.ReadFile( *file ) 54 content, err := ioutil.ReadFile(*file)
55 if err != nil { 55 if err != nil {
56 log.Fatal( err ) 56 log.Fatal(err)
57 } 57 }
58 fc := 0 58 fc := 0
59 59
60 buf1 := strings.Split( string( content ), "\n" ) 60 buf1 := strings.Split(string(content), "\n")
61 61
62 // Compare Alert Rate 62 // Compare Alert Rate
63 buf2 := strings.Split( buf1[ 0 ], "," ) 63 buf2 := strings.Split(buf1[ 0 ], ",")
64 flg := buf2[0] 64 flg := buf2[0]
65 thr, _ := strconv.ParseFloat( buf2[1], 32 ) 65 thr, _ := strconv.ParseFloat(buf2[1], 32)
66 66
67 if ( strings.EqualFold( "H", flg ) && thr < usd ) { 67 if (strings.EqualFold("H", flg) && thr < usd) {
68 fc = -1 68 fc = -1
69 content = bytes.Replace( content, []byte( "H" ), []byte( "-" ), 1 ) 69 content = bytes.Replace(content, []byte("H"), []byte("-"), 1)
70 if err := AlertMail( "H", now, usd ); err != nil { 70 if err := AlertMail("H", now, usd); err != nil {
71 log.Fatal( err ) 71 log.Fatal(err)
72 } 72 }
73 PrintVerbose( *verbose, "> rate higher.\n> sending alert mail.\n" ) 73 PrintVerbose(*verbose, "> rate higher.\n> sending alert mail.\n")
74 74
75 } else if ( strings.EqualFold( "L", flg ) && thr > usd ) { 75 } else if (strings.EqualFold("L", flg) && thr > usd) {
76 fc = -1 76 fc = -1
77 content = bytes.Replace( content, []byte( "L" ), []byte( "-" ), 1 ) 77 content = bytes.Replace(content, []byte("L"), []byte("-"), 1)
78 if err := AlertMail( "L", now, usd ); err != nil { 78 if err := AlertMail("L", now, usd); err != nil {
79 log.Fatal( err ) 79 log.Fatal(err)
80 } 80 }
81 PrintVerbose( *verbose, "> rate lower.\n> sending alert mail.\n" ) 81 PrintVerbose(*verbose, "> rate lower.\n> sending alert mail.\n")
82 } 82 }
83 83
84 // Compare Past Rate 84 // Compare Past Rate
85 buf2 = strings.Split( buf1[ len( buf1 ) - 2 ], "," ) 85 buf2 = strings.Split(buf1[ len(buf1) - 2 ], ",")
86 ymd0 := buf2[0] 86 ymd0 := buf2[0]
87 usd0, _ := strconv.ParseFloat( buf2[1], 32 ) 87 usd0, _ := strconv.ParseFloat(buf2[1], 32)
88 88
89 if ( math.Abs( usd0 - usd ) > 1.0 ) { 89 if (math.Abs(usd0 - usd) > 1.0) {
90 fc = 1 90 fc = 1
91 // Post E-mail 91 // Post E-mail
92 PrintVerbose( *verbose, "> sending notify mail.\n" ) 92 PrintVerbose(*verbose, "> sending notify mail.\n")
93 if err := NotifyMail( now, usd, ymd0, usd0 ); err != nil { 93 if err := NotifyMail(now, usd, ymd0, usd0); err != nil {
94 log.Fatal( err ) 94 log.Fatal(err)
95 } 95 }
96 PrintVerbose( *verbose, "> rate changed.\n" ) 96 PrintVerbose(*verbose, "> rate changed.\n")
97 } else { 97 } else {
98 PrintVerbose( *verbose, "> not changed.\n" ) 98 PrintVerbose(*verbose, "> not changed.\n")
99 } 99 }
100 100
101 // Save Rate 101 // Save Rate
102 if ( fc != 0 ) { 102 if (fc != 0) {
103 if ( fc == 1 ) { 103 if (fc == 1) {
104 buf := fmt.Sprintf( "%s,%.2f\n", now, usd ) 104 buf := fmt.Sprintf("%s,%.2f\n", now, usd)
105 content = append( content, buf... ) 105 content = append(content, buf...)
106 } 106 }
107 if err := ioutil.WriteFile( *file, content, 0777 ); err != nil { 107 if err := ioutil.WriteFile(*file, content, 0777); err != nil {
108 log.Fatal( err ) 108 log.Fatal(err)
109 } 109 }
110 PrintVerbose( *verbose, "> data file overwrited.\n" ) 110 PrintVerbose(*verbose, "> data file overwrited.\n")
111 } 111 }
112 112
113 PrintVerbose( *verbose, "> finish.\n" ) 113 PrintVerbose(*verbose, "> finish.\n")
114 } 114 }
115 115
116 func ParseHtml( z *html.Tokenizer ) ( float64, error ) { 116 func ParseHtml(z *html.Tokenizer) (float64, error) {
117 for { 117 for {
118 tt := z.Next() 118 tt := z.Next()
119 switch tt { 119 switch tt {
120 case html.ErrorToken: 120 case html.ErrorToken:
121 return 0.0, nil 121 return 0.0, nil
122 122
123 case html.StartTagToken: 123 case html.StartTagToken:
124 tag, _ := z.TagName() 124 tag, _ := z.TagName()
125 key, val, _ := z.TagAttr() 125 key, val, _ := z.TagAttr()
126 if string( tag ) == "strong" && string( key ) == "class" && string( val ) == "bkLine" { 126 if string(tag) == "strong" && string(key) == "class" && string(val) == "bkLine" {
127 z.Next() 127 z.Next()
128 //fmt.Printf( "%s %s %s\n", key, val, z.Token() ) 128 //fmt.Printf("%s %s %s\n", key, val, z.Token())
129 return strconv.ParseFloat( z.Token().String(), 32 ) 129 return strconv.ParseFloat(z.Token().String(), 32)
130 } 130 }
131 } 131 }
132 } 132 }
133 } 133 }
134 134
135 func NotifyMail( now string, usd float64, old string, usd0 float64 ) error { 135 func NotifyMail(now string, usd float64, old string, usd0 float64) error {
136 address := "muty@willcom.com" 136 address := "muty@willcom.com"
137 msg := "To: " + address + "\r\n" + 137 msg := "To: " + address + "\r\n" +
138 "Subject: Exchange-Mail\r\n" + 138 "Subject: Exchange-Mail\r\n" +
139 "\r\n" + 139 "\r\n" +
140 "rate changed\r\n" + 140 "rate changed\r\n" +
141 now + "\r\n" + 141 now + "\r\n" +
142 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n\r\n" + 142 fmt.Sprintf("[ USD/JPY : %.2f ].", usd) + "\r\n\r\n" +
143 old + "\r\n" + 143 old + "\r\n" +
144 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd0 ) + "\r\n" // mail body 144 fmt.Sprintf("[ USD/JPY : %.2f ].", usd0) + "\r\n" // mail body
145 if err := SendMail( address, msg ); err != nil { 145 if err := SendMail(address, msg); err != nil {
146 return err 146 return err
147 } 147 }
148 return nil 148 return nil
149 } 149 }
150 150
151 func AlertMail( flg, now string, usd float64 ) error { 151 func AlertMail(flg, now string, usd float64) error {
152 address := "muty@willcom.com" 152 address := "muty@willcom.com"
153 msg := "To: " + address + "\r\n" + 153 msg := "To: " + address + "\r\n" +
154 "Subject: Exchange-Mail\r\n" + 154 "Subject: Exchange-Mail\r\n" +
155 "\r\n" + 155 "\r\n" +
156 "rate changed\r\n" + 156 "rate changed\r\n" +
157 now + "\r\n" + 157 now + "\r\n" +
158 fmt.Sprintf( "[ USD/JPY : %.2f ].", usd ) + "\r\n" // mail body 158 fmt.Sprintf("[ USD/JPY : %.2f ].", usd) + "\r\n" // mail body
159 if err := SendMail( address, msg ); err != nil { 159 if err := SendMail(address, msg); err != nil {
160 return err 160 return err
161 } 161 }
162 return nil 162 return nil
163 } 163 }
164 164
165 func SendMail( address, msg string ) error { 165 func SendMail(address, msg string) error {
166 hostname := "sdm.sakura.ne.jp" 166 hostname := "sdm.sakura.ne.jp"
167 auth := smtp.PlainAuth( "", "bad_user@sdm.sakura.ne.jp", "hogehoge3", hostname ) 167 auth := smtp.PlainAuth("", "bad_user@sdm.sakura.ne.jp", "hogehoge3", hostname)
168 to := []string{ address } 168 to := []string{ address }
169 err := smtp.SendMail( hostname + ":587", auth, "bad_user@sdm.sakura.ne.jp", to, []byte( msg ) ) 169 err := smtp.SendMail(hostname + ":587", auth, "bad_user@sdm.sakura.ne.jp", to, []byte(msg))
170 if err != nil { 170 if err != nil {
171 return err 171 return err
172 } 172 }
173 return nil 173 return nil
174 } 174 }
175 175
176 func PrintVerbose( y bool, msg string ) { 176 func PrintVerbose(y bool, msg string) {
177 if y { 177 if y {
178 fmt.Fprintf( os.Stderr, msg ) 178 fmt.Fprintf(os.Stderr, msg)
179 } 179 }
180 } 180 }
181 181