-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
155 lines (137 loc) · 4.6 KB
/
types.go
File metadata and controls
155 lines (137 loc) · 4.6 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/nyaruka/phonenumbers"
)
var baseUrl = "https://www.fs.fed.us"
type Forest struct {
Name string `json:"name"`
State string `json:"state"`
Url string `json:"url"`
Id int `json:"id"`
Projects []ProjectUpdate `json:"projects"`
SopaReports []string `json:"sopa_reports"`
}
func (forest Forest) AsCsv() [][]string {
rows := [][]string{}
baseRow := []string{
forest.Name,
forest.State,
forest.Url,
fmt.Sprint(forest.Id),
}
for _, project := range forest.Projects {
rows = append(rows, append(baseRow, []string{
project.Name,
project.Id,
strings.Join(project.Purposes, ", "),
project.Status,
project.Decision,
project.ExpectedImplementation,
project.Contact.Name,
project.Contact.Email,
project.Contact.Phone,
project.Description,
project.WebLink,
project.Location,
project.Region,
project.District,
project.SopaReportDate,
project.ProjectCode,
}...))
}
return rows
}
type ProjectUpdate struct {
Name string `json:"name"`
Id string `json:"id"`
Purposes []string `json:"purpose"`
Status string `json:"status"`
Decision string `json:"decision"`
ExpectedImplementation string `json:"expected_implementation"`
Contact Contact `json:"contact"`
Description string `json:"description"`
WebLink string `json:"web_link"`
Location string `json:"location"`
Region string `json:"region"`
District string `json:"district"`
SopaReportDate string `json:"sopa_report_date"`
ProjectCode string `json:"project_code"`
ProjectDocuments []ProjectDocument `json:"project_documents"`
}
type ProjectDocument struct {
DateString string `json:"date_string"`
Date time.Time `json:"date"`
Category string `json:"category"`
Name string `json:"name"`
Url string `json:"url"`
}
type Contact struct {
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
}
func (project *ProjectUpdate) SetNameAndCode(html string) {
nameSplit := strings.Split(html, "<br/>")
project.Name = nameSplit[0] // TODO
if len(nameSplit) > 1 {
project.ProjectCode = nameSplit[1]
}
}
func (project *ProjectUpdate) SetPurposes(text string) {
purposes := strings.Split(text, " - ")
for i, purpose := range purposes {
purpose = strings.Trim(purpose, "-")
purpose = strings.TrimSpace(purpose)
purposes[i] = purpose
}
project.Purposes = purposes
}
func (project *ProjectUpdate) SetStatus(html string) {
project.Status = strings.ReplaceAll(html, "<br/>", "\n")
}
func (project *ProjectUpdate) SetContacts(html string) {
contacts := strings.Split(html, "<br/>")
if len(contacts) != 3 {
log.Fatal("bad contacts")
}
var phone_string = contacts[1] // TODO phonenumbers doesnt seem to handle numbers starting with 0 well
phone_number, _ := phonenumbers.Parse(strings.ToLower(phone_string), "US") // format phone number, toLower catches more "ext" strings
formatted_number := phonenumbers.Format(phone_number, phonenumbers.NATIONAL)
project.Contact = Contact{
Name: contacts[0],
Phone: formatted_number,
Email: contacts[2],
}
}
func (project *ProjectUpdate) SetSopaReportDateFromURL(url string) {
project.SopaReportDate = GetSopaReportDateFromURL(url)
}
func GetSopaReportDateFromURL(url string) string {
return url[len(url)-12 : len(url)-5] // example url: https://www.fs.fed.us/sopa/components/reports/sopa-110519-2021-07.html
}
var descriptionAndLink = regexp.MustCompile("Description:(.*)Web Link:(.*)")
var getProjectID = regexp.MustCompile(`http.*project=(\d*)`)
func (project *ProjectUpdate) SetDescription(text string) {
if matches := descriptionAndLink.FindStringSubmatch(text); len(matches) == 3 {
project.Description = matches[1]
project.WebLink = trim(matches[2])
ids := getProjectID.FindStringSubmatch(project.WebLink)
if len(ids) > 1 {
project.Id = ids[1]
}
} else {
project.Description = strings.Replace(text, "Description:", "", 1)
project.Description = project.Description[1:] // remove first char, which isn't an ascii space
}
}
var singleSpacePattern = regexp.MustCompile(`\s+`)
func trim(s string) string {
s = strings.TrimSpace(s)
s = singleSpacePattern.ReplaceAllString(s, " ")
return s
}