/
earthquake.go
123 lines (101 loc) · 2.68 KB
/
earthquake.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
package src
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/earthquake-alert/erarthquake-alert-v2/src/jma"
)
const EARTHQUAKE_REPORT_TEMPLATE_FILE = "earthquake_report.gtpl"
const EARTHQUAKE_EPICENTER_TEMPLATE_FILE = "earthquake_epicenter.gtpl"
const EARTHQUAKE_INFO_TEMPLATE_FILE = "earthquake_info.gtpl"
const EARTHQUAKE_COMMENTS_TEMPLATE_FILE = "earthquake_comments.gtpl"
type EarthquakeType int
const (
EarthquakeReport EarthquakeType = 0
EarthquakeEpicenter EarthquakeType = 1
EarthquakeInfo EarthquakeType = 2
EarthquakeCancel EarthquakeType = 3
)
// 震度速報、震源に関する情報、震源・震度に関する情報
type Earthquake struct {
Row string
Parsed *jma.EarthquakeJmaXml
Images []string
}
func ParseEarthquake(row []byte) (*Earthquake, error) {
e, err := jma.ParseEarthquake(row)
if err != nil {
return nil, err
}
return &Earthquake{
Row: string(row),
Parsed: e,
}, nil
}
func (c *Earthquake) Assembly(ctx context.Context, db *sql.DB) error {
// TODO: あとで
return nil
}
func (c *Earthquake) SetImages() error {
// TODO: あとで
return nil
}
func (c *Earthquake) GetTitle() string {
title := c.Parsed.Head.Title
if c.GetInfoType() == jma.Cancel {
title = fmt.Sprintf("【取消】%s", title)
}
return title
}
func (c *Earthquake) GetText() (string, error) {
var tempFile string
switch c.GetType() {
case EarthquakeEpicenter:
tempFile = EARTHQUAKE_EPICENTER_TEMPLATE_FILE
case EarthquakeReport:
tempFile = EARTHQUAKE_REPORT_TEMPLATE_FILE
case EarthquakeInfo:
tempFile = EARTHQUAKE_INFO_TEMPLATE_FILE
case EarthquakeCancel:
tempFile = EARTHQUAKE_COMMENTS_TEMPLATE_FILE
}
return Template(tempFile, c)
}
func (c *Earthquake) GetType() EarthquakeType {
// 地の震度に関する情報
// 出現するもの
// - 震度速報
// - 震源・震度情報
intensity := c.Parsed.Body.Intensity
// 地震の詳細(各地の震度など)
// 出現するもの
// - 震源に関する情報
// - 震源・震度情報
earthquake := c.Parsed.Body.Earthquake
// 震源・震度情報
if intensity != nil && earthquake != nil {
return EarthquakeInfo
}
// 震度速報
if intensity != nil && earthquake == nil {
return EarthquakeReport
}
// 震源に関する情報
if intensity == nil && earthquake != nil {
return EarthquakeEpicenter
}
return EarthquakeCancel
}
func (c *Earthquake) GetTargetDate() (time.Time, error) {
return time.Now(), nil
}
func (c *Earthquake) GetInfoType() jma.InfoType {
return c.Parsed.Head.InfoType
}
func (c *Earthquake) GetImages() []string {
return c.Images
}
func (c *Earthquake) GetEventId() ([]uint64, error) {
return ParseEventID(c.Parsed.Head.EventID)
}