-
Notifications
You must be signed in to change notification settings - Fork 0
/
instauser.go
152 lines (111 loc) · 2.96 KB
/
instauser.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
144
145
146
147
148
149
150
151
152
package instauser
import (
"database/sql"
"time"
"github.com/dekoch/gouniversal/shared/io/sqlite3"
)
const TableName = "instauser"
type InstaUser struct {
UserID string
UserName string
AccountID string
Tag string
NewTags []string
Following bool
Follow time.Time
Unfollow time.Time
}
func LoadConfig(dbconn *sqlite3.SQLite) error {
var lyt sqlite3.Layout
lyt.SetTableName(TableName)
lyt.AddField("userid", sqlite3.TypeTEXT, true, false)
lyt.AddField("username", sqlite3.TypeTEXT, false, false)
lyt.AddField("accountid", sqlite3.TypeTEXT, false, false)
lyt.AddField("tag", sqlite3.TypeTEXT, false, false)
lyt.AddField("following", sqlite3.TypeNUMERIC, false, false)
lyt.AddField("follow", sqlite3.TypeDATE, false, false)
lyt.AddField("unfollow", sqlite3.TypeDATE, false, false)
return dbconn.CreateTableFromLayout(lyt)
}
func (inf *InstaUser) Save(tx *sql.Tx) error {
_, err := tx.Exec("INSERT OR REPLACE INTO `"+TableName+"` (userid, username, accountid, tag, following, follow, unfollow) values(?,?,?,?,?,?,?)", inf.UserID, inf.UserName, inf.AccountID, inf.Tag, inf.Following, inf.Follow, inf.Unfollow)
return err
}
func (inf *InstaUser) Load(userid string, dbconn *sqlite3.SQLite) (bool, error) {
var (
err error
found bool
)
func() {
for i := 0; i <= 2; i++ {
switch i {
case 0:
dbconn.Rows, err = dbconn.DB.Query("SELECT userid, username, accountid, tag, following, follow, unfollow FROM `"+TableName+"` WHERE userid=?", userid)
case 1:
defer dbconn.Rows.Close()
case 2:
for dbconn.Rows.Next() {
err = dbconn.Rows.Scan(&inf.UserID, &inf.UserName, &inf.AccountID, &inf.Tag, &inf.Following, &inf.Follow, &inf.Unfollow)
found = true
}
}
if err != nil {
return
}
}
}()
return found, err
}
func Exists(userid string, dbconn *sqlite3.SQLite) (bool, error) {
var (
err error
found bool
)
func() {
for i := 0; i <= 2; i++ {
switch i {
case 0:
dbconn.Rows, err = dbconn.DB.Query("SELECT userid FROM `"+TableName+"` WHERE userid=?", userid)
case 1:
defer dbconn.Rows.Close()
case 2:
for dbconn.Rows.Next() {
found = true
}
}
if err != nil {
return
}
}
}()
return found, err
}
func GetUsersFollowingBetween(fromdate, todate time.Time, dbconn *sqlite3.SQLite) ([]InstaUser, error) {
var (
err error
ret []InstaUser
)
func() {
for i := 0; i <= 2; i++ {
switch i {
case 0:
dbconn.Rows, err = dbconn.DB.Query("SELECT userid, username, accountid, tag, following, follow, unfollow FROM `"+TableName+"` WHERE following=TRUE AND follow BETWEEN ? AND ?", fromdate, todate)
case 1:
defer dbconn.Rows.Close()
case 2:
var n InstaUser
for dbconn.Rows.Next() {
err = dbconn.Rows.Scan(&n.UserID, &n.UserName, &n.AccountID, &n.Tag, &n.Following, &n.Follow, &n.Unfollow)
if err != nil {
return
}
ret = append(ret, n)
}
}
if err != nil {
return
}
}
}()
return ret, err
}