You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

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