-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (126 loc) · 3.35 KB
/
main.go
File metadata and controls
169 lines (126 loc) · 3.35 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
const AUDITOR_URL = "http://oh-mahoning-auditor.publicaccessnow.com/DesktopModules/OWS/IM.aspx?&mpropertynumber=%s&_OWS_=lxC:1,lxP:0,s:1x,m:427,pm:116,p:89,lxSrc:dnn$ctr427$PropertyInfo,key:Module.Load,file:/DesktopModules/PropertyInfo/App_LocalResources/PropertyInfo.ascx.resx,pp:0"
func main() {
arg := os.Args
if len(arg) == 1 {
sync()
fmt.Fprintf(os.Stderr, "error: no command given\n")
os.Exit(1)
}
cmd := arg[1]
input := "" //FIXME
if len(arg) > 2 {
input = arg[2]
}
if cmd == "add" {
add(input)
} else if cmd == "remove" {
remove(input)
} else if cmd == "list" {
list()
} else if cmd == "sync" {
sync()
fmt.Println("Sync and compare done.")
} else {
fmt.Fprintf(os.Stderr, "error: invalid command\n")
os.Exit(1)
}
}
func sync() {
db := _get_or_create_db()
rows, _ := db.Query("SELECT id, parcel_id, owner FROM properties")
var id int
var parcel_id string
var owner string
updates := make(map[int]string)
for rows.Next() {
rows.Scan(&id, &parcel_id, &owner)
new_owner := _get_owner(parcel_id)
if owner != new_owner {
fmt.Printf("New owner for %s: %s\n", parcel_id, new_owner)
updates[id] = new_owner
}
}
for k, v := range updates {
statement, _ := db.Prepare("UPDATE properties SET owner=? WHERE id=?")
statement.Exec(v, k)
}
db.Close()
}
func list() {
db := _get_or_create_db()
rows, _ := db.Query("SELECT id, parcel_id, owner FROM properties")
var id int
var parcel_id string
var owner string
for rows.Next() {
rows.Scan(&id, &parcel_id, &owner)
fmt.Println(parcel_id + " " + owner)
}
db.Close()
}
func add(parcel_id string) {
parcel_id = strings.TrimSpace(parcel_id)
db := _get_or_create_db()
owner := _get_owner(parcel_id)
var id int
err := db.QueryRow(`SELECT id FROM properties WHERE parcel_id=?`, parcel_id).Scan(&id)
if err == sql.ErrNoRows {
statement, _ := db.Prepare("INSERT INTO properties(parcel_id, owner) values(?,?)")
statement.Exec(parcel_id, owner)
fmt.Printf("Parcel added %s: %s\n", parcel_id, owner)
} else {
fmt.Fprintf(os.Stderr, "error: This parcel already exists in the database\n")
os.Exit(1)
}
db.Close()
}
func remove(parcel_id string) {
parcel_id = strings.TrimSpace(parcel_id)
db := _get_or_create_db()
statement, err := db.Prepare("DELETE FROM properties WHERE parcel_id=?")
_checkErr(err)
statement.Exec(parcel_id)
fmt.Printf("Parcel removed: %s\n", parcel_id)
db.Close()
}
func _get_or_create_db() *sql.DB {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
db, _ := sql.Open("sqlite3", exPath+string(os.PathSeparator)+"snipe.db")
statement, _ := db.Prepare("CREATE TABLE IF NOT EXISTS properties (id INTEGER PRIMARY KEY, parcel_id TEXT, owner TEXT)")
statement.Exec()
return db
}
func _checkErr(err error) {
if err != nil {
panic(err)
}
}
func _get_owner(parcel_id string) string {
url := fmt.Sprintf(AUDITOR_URL, parcel_id)
res, _ := http.Get(url)
body, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
r, _ := regexp.Compile("Owner Name</td><td >(.+)</td>")
match := r.FindStringSubmatch(string(body[:]))
if len(match) < 2 {
fmt.Fprintf(os.Stderr, "No property found for that parcel_id\n")
os.Exit(1)
}
return strings.TrimSpace(match[1])
}