-
Notifications
You must be signed in to change notification settings - Fork 0
/
instaresp.go
193 lines (145 loc) · 3.61 KB
/
instaresp.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package instaresp
import (
"database/sql"
"encoding/json"
"time"
"github.com/dekoch/gouniversal/shared/io/sqlite3"
)
const TableName = "instaresp"
type InstaResp struct {
UserID string
UserName string
Checked time.Time
Response Response
}
type RespFile struct {
FileID string
DisplayURL string
IsVideo bool
VideoURL string
}
type Response struct {
Data Data `json:"data"`
Status string `json:"status"`
}
type Data struct {
User User `json:"user"`
}
type User struct {
Eottm Eottm `json:"edge_owner_to_timeline_media"`
}
type Eottm struct {
Count uint `json:"count"`
PageInfo PageInfo `json:"page_info"`
Edges []Edge `json:"edges"`
}
type PageInfo struct {
HasNextPage bool `json:"has_next_page"`
EndCursor string `json:"end_cursor"`
}
type Edge struct {
Node Node `json:"node"`
}
type Node struct {
ID string `json:"id"`
DisplayURL string `json:"display_url"`
IsVideo bool `json:"is_video"`
VideoURL string `json:"video_url"`
Owner Owner `json:"owner"`
EdgeSidecarToChildren Estc `json:"edge_sidecar_to_children"`
}
type Owner struct {
ID string `json:"id"`
UserName string `json:"username"`
}
type Estc struct {
Edges []ChildrenEdge `json:"edges"`
}
type ChildrenEdge struct {
Node ChildrenNode `json:"node"`
}
type ChildrenNode struct {
ID string `json:"id"`
DisplayURL string `json:"display_url"`
IsVideo bool `json:"is_video"`
VideoURL string `json:"video_url"`
}
func (ir *Response) Unmarshal(b []byte) error {
return json.Unmarshal(b, &ir)
}
func (ir *Response) Marshal() ([]byte, error) {
return json.Marshal(ir)
}
func (ir *InstaResp) GetFiles() []RespFile {
var (
ret []RespFile
n RespFile
)
for _, edge := range ir.Response.Data.User.Eottm.Edges {
// if edge has childrenedges, ignor main file
if len(edge.Node.EdgeSidecarToChildren.Edges) == 0 {
n.FileID = edge.Node.ID
n.DisplayURL = edge.Node.DisplayURL
n.IsVideo = edge.Node.IsVideo
n.VideoURL = edge.Node.VideoURL
ret = append(ret, n)
} else {
for _, child := range edge.Node.EdgeSidecarToChildren.Edges {
n.FileID = child.Node.ID
n.DisplayURL = child.Node.DisplayURL
n.IsVideo = child.Node.IsVideo
n.VideoURL = child.Node.VideoURL
ret = append(ret, n)
}
}
}
return ret
}
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("checked", sqlite3.TypeDATE, false, false)
lyt.AddField("response", sqlite3.TypeTEXT, false, false)
return dbconn.CreateTableFromLayout(lyt)
}
func (ir *InstaResp) Save(tx *sql.Tx) error {
resp, err := ir.Response.Marshal()
if err != nil {
return err
}
_, err = tx.Exec("INSERT OR REPLACE INTO `"+TableName+"` (userid, username, checked, response) values(?,?,?,?)", ir.UserID, ir.UserName, ir.Checked, string(resp))
return err
}
func (ir *InstaResp) Load(dbconn *sqlite3.SQLite) (bool, error) {
var (
err error
found bool
resp string
)
func() {
var rows *sql.Rows
for i := 0; i <= 3; i++ {
switch i {
case 0:
rows, err = dbconn.DB.Query("SELECT username, checked, response FROM `"+TableName+"` WHERE userid=?", ir.UserID)
case 1:
defer rows.Close()
case 2:
for rows.Next() {
err = rows.Scan(&ir.UserName, &ir.Checked, &resp)
found = true
}
case 3:
if found {
err = ir.Response.Unmarshal([]byte(resp))
}
}
if err != nil {
return
}
}
}()
return found, err
}