-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
143 lines (120 loc) · 3.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"flag"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/gen2brain/beeep"
"github.com/gocolly/colly"
"github.com/lestrrat-go/strftime"
)
type earthquake struct {
Date string
Time string
DateTime time.Time
Latitude float64
Longitude float64
Depth float64
Md float64
Ml float64
Mw float64
Yer string
}
var (
earthquakes []earthquake
day string
hour string
minute string
duration time.Duration
min float64
)
func init() {
day, _ = strftime.Format(`%Y.%m.%d`, time.Now())
hour, _ = strftime.Format(`%H`, time.Now())
minute, _ = strftime.Format(`%M`, time.Now())
var period int
flag.IntVar(&period, "period", 5, "periodic interval")
var min int
flag.IntVar(&min, "min", 3, "min")
flag.Parse()
duration = time.Minute * time.Duration(period)
}
func main() {
log.Printf("[*] BDTİM sayfası her %s de bir kontrol edilecek.", duration)
run()
select {}
}
func run() {
c := colly.NewCollector()
c.OnHTML("body > pre", func(e *colly.HTMLElement) {
table := strings.Split(e.Text, "--------------")
rows := strings.Split(table[len(table)-1], "\n")
loc, err := time.LoadLocation("Europe/Istanbul")
if err != nil {
log.Fatal(err)
}
LOCALDATETIME := "2006-01-02T03:04:05GMT"
for _, row := range rows {
if len(row) > 0 {
parsed := regexp.MustCompile(`\s+`).Split(row, 10)
latitude, _ := strconv.ParseFloat(parsed[2], 64)
longitude, _ := strconv.ParseFloat(parsed[3], 64)
depth, _ := strconv.ParseFloat(parsed[4], 64)
md, _ := strconv.ParseFloat(parsed[5], 64)
ml, _ := strconv.ParseFloat(parsed[6], 64)
mw, _ := strconv.ParseFloat(parsed[7], 64)
dots := regexp.MustCompile(`\.`)
dateString := dots.ReplaceAllString(parsed[0], "-") + "T" + parsed[1] + "GMT"
dateTime, _ := time.ParseInLocation(LOCALDATETIME, dateString, loc)
spaces := regexp.MustCompile(`\s+`)
d := earthquake{
Date: parsed[0],
Time: parsed[1],
DateTime: dateTime,
Latitude: latitude,
Longitude: longitude,
Depth: depth,
Md: md,
Ml: ml,
Mw: mw,
Yer: spaces.ReplaceAllString(parsed[8], " "),
}
earthquakes = append(earthquakes, d)
}
}
})
log.Print("Sayfa ziyaret ediliyor.")
c.Visit("http://www.koeri.boun.edu.tr/scripts/lst4.asp")
last := last(earthquakes)
filtered := filter(last)
for _, x := range filtered {
beeep.Notify(
"BDTİM Uyarı "+x.Date+" "+x.Time,
fmt.Sprintf("%s de %v şiddetinde deprem oldu.", x.Yer, x.Ml),
"assets/logo.png",
)
}
time.AfterFunc(duration, run)
}
func last(list []earthquake) []earthquake {
newlist := list[:0]
now := time.Now()
for _, x := range list {
if duration.Minutes() >= now.Sub(x.DateTime).Minutes() {
newlist = append(newlist, x)
}
}
return newlist
}
func filter(list []earthquake) []earthquake {
newlist := list[:0]
for _, x := range list {
if x.Ml > min {
newlist = append(newlist, x)
}
}
return newlist
}