-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
83 lines (67 loc) · 1.73 KB
/
import.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
package cmd
import (
"bufio"
"context"
"log"
"os"
firebase "firebase.google.com/go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/api/option"
"github.com/pinheirolucas/insta_cleaner/helper"
"github.com/pinheirolucas/insta_cleaner/whitelist"
)
// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import",
Short: "Add the users to database from a whitelist",
Run: runImport,
}
func init() {
whitelistCmd.AddCommand(importCmd)
}
func runImport(cmd *cobra.Command, args []string) {
if len(args) == 0 {
createError("no whitelist file")
}
wl := args[0]
c, err := loadConfig()
if err != nil {
handleError(err)
}
insta, err := helper.InitLocalGoinsta(c.username, c.password, c.session)
if err != nil {
log.Fatalf("helper.InitLocalGoinsta: %v \n", err)
}
app, err := firebase.NewApp(
context.Background(),
&firebase.Config{
DatabaseURL: c.databaseURL,
},
option.WithCredentialsFile(c.credentials),
)
if err != nil {
log.Fatalf("firebase.NewApp: %v \n", err)
}
whitelistService, err := whitelist.NewService(context.Background(), viper.GetString("whitelist_service_type"), app)
if err != nil {
log.Fatalf("whitelist.NewService: %v \n", err)
}
file, err := os.Open(wl)
if err != nil {
log.Fatalf("os.Open: %v \n", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
u := scanner.Text()
log.Printf("importing %s \n", u)
user, err := insta.Profiles.ByName(u)
if err != nil {
log.Fatalf("(*goinsta.Instagram).Profiles.ByName: %v \n", err)
}
if err := whitelistService.CreateIfNotExists(user.ID, user.Username); err != nil {
log.Fatalf("(cleaner.WhitelistService).CreateIfNotExists: %v \n", err)
}
}
}