argos/optionFollowRandom.go
optionFollowRandom.goRaw
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "os"
7 "strconv"
8 "strings"
9 "time"
10
11 "github.com/dghubble/go-twitter/twitter"
12)
13
14func waitTime(minutes int) {
15 //wait to avoid the twitter api limitation
16 timeToSleep := time.Duration(minutes) * time.Minute
17 fmt.Println("waiting " + strconv.Itoa(minutes) + " min to avoid twitter api limitation")
18 fmt.Println(time.Now().Local())
19 time.Sleep(timeToSleep)
20}
21func getUserToFollowFromUser(client *twitter.Client, fromUser string) string {
22 friends, httpResp, err := client.Friends.List(&twitter.FriendListParams{
23 ScreenName: fromUser,
24 Count: 1,
25 })
26 if err != nil {
27 fmt.Println(err)
28 }
29 if httpResp.Status != "200 OK" {
30 c.Red(httpResp.Status)
31 c.Purple("maybe twitter has blocked the account, CTRL+C, wait 15 minutes and try again")
32 }
33 return friends.Users[0].ScreenName
34}
35
36func followUser(client *twitter.Client, screenName string) {
37 user, _, _ := client.Friendships.Create(&twitter.FriendshipCreateParams{
38 ScreenName: screenName,
39 })
40 fmt.Print("followed: ")
41 c.Green("@" + user.ScreenName + " - " + user.Description)
42}
43
44func followRandom(client *twitter.Client, nFollow int, screenName string) {
45 fmt.Println("Starting to follow " + strconv.Itoa(nFollow) + " users")
46 for i := 0; i < nFollow; i++ {
47 userToFollow := getUserToFollowFromUser(client, screenName)
48 followUser(client, userToFollow)
49 screenName = userToFollow
50 waitTime(1)
51 }
52
53}
54
55func optionFollowRandom(client *twitter.Client) {
56 c.Red("how many accounts to follow?")
57 newcommand := bufio.NewReader(os.Stdin)
58 answer, _ := newcommand.ReadString('\n')
59 answer = strings.TrimSpace(answer)
60 fmt.Print("Number of users to follow: ")
61 c.Purple(answer)
62 nFollow, err := strconv.Atoi(answer)
63 if err != nil {
64 fmt.Println("incorrect entry, need a positive number")
65 }
66
67 fmt.Print("entry @username of a user, to get a 1st user to follow, that will be a user that the 1st user is following, and the 2nd user will be a user that the 3rd user is following): @")
68 newcommand = bufio.NewReader(os.Stdin)
69 firstScreenName, _ := newcommand.ReadString('\n')
70 firstScreenName = strings.TrimSpace(firstScreenName)
71 fmt.Print("first user to follow: @")
72 c.Purple(firstScreenName)
73
74 c.Red("Are you sure? [y/n]")
75 newcommand = bufio.NewReader(os.Stdin)
76 answer, _ = newcommand.ReadString('\n')
77 answer = strings.TrimSpace(answer)
78 switch answer {
79 case "y":
80 fmt.Println("ok, you are sure")
81 followRandom(client, nFollow, firstScreenName)
82 break
83 default:
84 fmt.Println("Operation cancelled")
85 break
86 }
87}