/
twitter.go
139 lines (126 loc) · 3.43 KB
/
twitter.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
package twitterhandle
import (
"encoding/json"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"../config"
"github.com/ChimeraCoder/anaconda"
"github.com/bwmarrin/discordgo"
)
// Users is a list of user IDs to track
var Users []string
// Twitter is the twitter API client
var Twitter *anaconda.TwitterApi
// Config holds information regarding the config
var Config *config.Config
// Track tracks the twitter users for the channel every minute
func Track(s *discordgo.Session) {
startTime := time.Now()
ticker := time.NewTicker(60 * time.Second)
for {
select {
case <-ticker.C:
// Get channels
var fileNames []string
filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
fileNames = append(fileNames, path)
return nil
})
var channels []string
channelRegex, err := regexp.Compile(`(\d+)\.json`)
for _, file := range fileNames {
if channelRegex.MatchString(file) {
channels = append(channels, channelRegex.FindStringSubmatch(file)[1])
}
}
for _, chID := range channels {
// Get webhook
var webhook *discordgo.Webhook
webhooks, _ := s.ChannelWebhooks(chID)
if len(webhooks) == 0 {
webhook, err = s.WebhookCreate(chID, Config.Discord.Username, Config.Discord.Avatar)
if err == nil {
// Get users
var users []anaconda.User
b, _ := ioutil.ReadFile("./" + chID + ".json")
json.Unmarshal(b, &users)
for _, user := range users {
// Get tweets
v := url.Values{}
v.Set("screen_name", user.ScreenName)
v.Set("count", "200")
tweets, err := Twitter.GetUserTimeline(v)
if err != nil {
continue
}
for _, tweet := range tweets {
// Check if tweet was after last run
tweetTime, _ := tweet.CreatedAtTime()
if startTime.After(tweetTime) {
continue
}
s.WebhookExecute(webhook.ID, webhook.Token, false, &discordgo.WebhookParams{
Content: "https://twitter.com/" + user.ScreenName + "/status/" + tweet.IdStr,
Username: user.ScreenName,
AvatarURL: user.ProfileImageUrlHttps,
})
}
}
continue
}
} else {
webhook = webhooks[0]
}
// Get users
var users []anaconda.User
b, _ := ioutil.ReadFile("./" + chID + ".json")
json.Unmarshal(b, &users)
for _, user := range users {
// Get tweets
v := url.Values{}
v.Set("screen_name", user.ScreenName)
v.Set("count", "200")
tweets, err := Twitter.GetUserTimeline(v)
if err != nil {
continue
}
for _, tweet := range tweets {
// Check if tweet was after last run
tweetTime, _ := tweet.CreatedAtTime()
if startTime.After(tweetTime) {
continue
}
s.WebhookExecute(webhook.ID, webhook.Token, false, &discordgo.WebhookParams{
Content: "https://twitter.com/" + user.ScreenName + "/status/" + tweet.IdStr,
Username: user.ScreenName,
AvatarURL: user.ProfileImageUrlHttps,
})
}
}
}
startTime = time.Now()
}
}
}
// Verify checks if the twitter handles given are valid
func Verify(IDs []string) (users []anaconda.User, err error) {
users, err = Twitter.GetUsersLookup(strings.Join(IDs, ", "), nil)
if err != nil {
return nil, err
}
// Remove protected accounts
i := 0
for _, user := range users {
if !user.Protected {
users[i] = user
i++
}
}
users = users[:i]
return users, err
}