forked from cecobask/instagram-insights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
95 lines (86 loc) · 2.08 KB
/
model.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
package main
import (
"encoding/json"
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"os"
)
type UserData struct {
ProfileURL string `json:"href"`
Username string `json:"value"`
Timestamp int64 `json:"timestamp"`
}
type FollowData struct {
Following map[string]UserData
Followers map[string]UserData
}
type UserDataJson struct {
UserData []UserData `json:"string_list_data"`
}
func NewFollowData() *FollowData {
return &FollowData{
Following: make(map[string]UserData),
Followers: make(map[string]UserData),
}
}
func (fd *FollowData) ExtractFollowing() error {
data, err := os.ReadFile(pathFollowing)
if err != nil {
return err
}
jsonStruct := make(map[string][]UserDataJson)
if err = json.Unmarshal(data, &jsonStruct); err != nil {
return err
}
for _, following := range jsonStruct["relationships_following"] {
userData := following.UserData[0]
fd.Following[userData.Username] = userData
}
return nil
}
func (fd *FollowData) ExtractFollowers() error {
data, err := os.ReadFile(pathFollowers)
if err != nil {
return err
}
var jsonArray []UserDataJson
err = json.Unmarshal(data, &jsonArray)
if err != nil {
return err
}
for _, follower := range jsonArray {
userData := follower.UserData[0]
fd.Followers[userData.Username] = userData
}
return nil
}
func (fd *FollowData) ExtractAllData() error {
err := fd.ExtractFollowing()
if err != nil {
return err
}
err = fd.ExtractFollowers()
if err != nil {
return err
}
return nil
}
func (fd *FollowData) FindUnfollowers() {
var tableRows []table.Row
for username, userData := range fd.Following {
if _, found := fd.Followers[username]; !found {
tableRows = append(tableRows, table.Row{userData.Username, userData.ProfileURL})
}
}
if len(tableRows) == 0 {
fmt.Println("No unfollowers found!")
return
}
unfollowersTable := table.NewWriter()
unfollowersTable.SetAutoIndex(true)
unfollowersTable.SetOutputMirror(os.Stdout)
unfollowersTable.SetStyle(table.StyleBold)
unfollowersTable.AppendHeader(table.Row{"USERNAME", "PROFILE URL"})
unfollowersTable.AppendRows(tableRows)
unfollowersTable.Render()
}