arnaucube
arnaucube/flock-botnetPublic Self-hosted repository
flock-botnet/readConfigTokensAndConnect.go
readConfigTokensAndConnect.goRaw
 1package main
 2
 3import (
 4	"encoding/json"
 5	"fmt"
 6	"io/ioutil"
 7
 8	"github.com/dghubble/go-twitter/twitter"
 9	"github.com/dghubble/oauth1"
10)
11
12type Config struct {
13	Title               string `json:"title"`
14	Consumer_key        string `json:"consumer_key"`
15	Consumer_secret     string `json:"consumer_secret"`
16	Access_token_key    string `json:"access_token_key"`
17	Access_token_secret string `json:"access_token_secret"`
18}
19type Flock struct {
20	ScreenNames []string
21	Clients     []*twitter.Client
22}
23
24func readConfigTokensAndConnect() (flock Flock) {
25	var config []Config
26	var clients []*twitter.Client
27
28	file, e := ioutil.ReadFile("flockConfig.json")
29	if e != nil {
30		fmt.Println("error:", e)
31	}
32	content := string(file)
33	json.Unmarshal([]byte(content), &config)
34	fmt.Println("flockConfig.json read comlete")
35
36	fmt.Print("connecting to twitter api --> ")
37	for i := 0; i < len(config); i++ {
38		configu := oauth1.NewConfig(config[i].Consumer_key, config[i].Consumer_secret)
39		token := oauth1.NewToken(config[i].Access_token_key, config[i].Access_token_secret)
40		httpClient := configu.Client(oauth1.NoContext, token)
41		// twitter client
42		client := twitter.NewClient(httpClient)
43		clients = append(clients, client)
44		flock.ScreenNames = append(flock.ScreenNames, config[i].Title)
45	}
46	flock.Clients = clients
47
48	fmt.Println("connection successfull")
49
50	return flock
51}