-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod-instagram.go
166 lines (143 loc) · 4.43 KB
/
mod-instagram.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/Davincible/goinsta"
"github.com/fatih/color"
)
var (
pathConfigModuleInstagram = pathConfigModules + string(os.PathSeparator) + "instagram.json"
instagramConfig configModuleInstagram
)
type configModuleInstagram struct {
WaitMins int `json:"waitMins,omitempty"`
Accounts []configModuleInstagramAcc `json:"accounts"`
}
type configModuleInstagramAcc struct {
// Main
Name string `json:"moduleName"`
ID string `json:"id"`
Destinations []string `json:"destinations"`
WaitMins *int `json:"waitMins,omitempty"`
}
func loadConfig_Module_Instagram() error {
prefixHere := "loadConfig_Module_Instagram(): "
// TODO: Creation prompts if missing
// LOAD JSON CONFIG
if _, err := os.Stat(pathConfigModuleInstagram); err != nil {
return fmt.Errorf("instagram config file not found: %s", err)
} else {
configBytes, err := os.ReadFile(pathConfigModuleInstagram)
if err != nil {
return fmt.Errorf("failed to read instagram config file: %s", err)
} else {
// Fix backslashes
configStr := string(configBytes)
configStr = strings.ReplaceAll(configStr, "\\", "\\\\")
for strings.Contains(configStr, "\\\\\\") {
configStr = strings.ReplaceAll(configStr, "\\\\\\", "\\\\")
}
// Parse
if err = json.Unmarshal([]byte(configStr), &instagramConfig); err != nil {
return fmt.Errorf("failed to parse instagram config file: %s", err)
}
// Output?
if generalConfig.OutputSettings {
s, err := json.MarshalIndent(instagramConfig, "", "\t")
if err != nil {
log.Println(color.HiRedString(prefixHere+"failed to output...\t%s", err))
} else {
log.Println(color.HiYellowString(prefixHere+"\n%s", color.YellowString(string(s))))
}
}
}
}
return nil
}
var (
instagramUsername string
instagramPassword string
instagramConnected bool = false
instagramScraper *goinsta.Instagram
)
func openInstagram() error {
l := logInstructions{
Location: "openInstagram",
Task: "login",
Inline: false,
Color: color.MagentaString,
}
if instagramUsername == "" || instagramPassword == "" {
return errors.New("instagram credentials are incomplete")
} else {
log.Println(l.LogI(true, "Connecting to Instagram..."))
//TODO: Proxy Support
// Login Loop
instagramLoginCount := 0
do_instagram_login:
instagramLoginCount++
if instagramLoginCount > 1 {
time.Sleep(3 * time.Second)
}
if instagramScraper, err := goinsta.Import(pathDataCookiesInstagram); err != nil {
instagramScraper = goinsta.New(instagramUsername, instagramPassword)
if err := instagramScraper.Login(); err != nil {
log.Println(l.SetFlag(&lError).Log("Login Error: %s", err.Error()))
if instagramLoginCount <= 3 {
goto do_instagram_login
} else {
log.Println(l.SetFlag(&lError).Log("Failed to login to Instagram, the bot will not fetch this media..."))
l.ClearFlag()
return errors.New("login failed")
}
} else {
log.Println(l.LogCI(color.HiMagentaString, true, "Connected to %s via new login", instagramUsername))
instagramConnected = true
defer instagramScraper.Export(pathDataCookiesInstagram)
}
} else {
log.Println(l.LogCI(color.HiMagentaString, true, "Connected to %s via cache", instagramUsername))
instagramConnected = true
}
//TODO: Reinforce Proxy Support
}
return nil
}
func handleInstagramAccount(account configModuleInstagramAcc) error {
log.Printf(color.HiGreenString("<DEBUG> instagram account event fired: %s"), account.ID)
return nil
}
/*func handleInstagramAccCmdOpts(config *configModuleInstagramAcc,
optionMap map[string]*discordgo.ApplicationCommandInteractionDataOption,
s *discordgo.Session, i *discordgo.InteractionCreate) error {*/
func getInstagramAccConfigIndex(name string) int {
for k, feed := range instagramConfig.Accounts {
if strings.EqualFold(name, feed.Name) {
return k
}
}
return -1
}
func getInstagramAccConfig(name string) *configModuleInstagramAcc {
i := getInstagramAccConfigIndex(name)
if i == -1 {
return nil
} else {
return &instagramConfig.Accounts[i]
}
}
func existsInstagramConfig(name string) bool {
for _, feed := range instagramConfig.Accounts {
if strings.EqualFold(name, feed.Name) {
return true
}
}
return false
}
// func updateInstagramAccConfig(name string, config configModuleInstagramAcc) bool {
// func deleteInstagramAccConfig(name string) error