forked from BassMatt/TikiTokBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (128 loc) · 4.04 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
func main() {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + os.Getenv("DISCORD_TOKEN"))
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreate)
// Listen for receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
client := &http.Client{}
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
reg, err := regexp.Compile(`https:\/\/vm\.tiktok\.com\/(.*)\/`)
if err != nil {
log.Fatalf("Error compiling regular expression %v", err)
return
}
// see if url matches regex pattern
match := reg.FindString(m.Content)
if match != "" {
req, err := http.NewRequest("GET", match, nil)
if err != nil {
log.Fatalf("Error creating HTTP Request %v", err)
return
}
// convert shortened request to longer tiktok url for scraping
headers := map[string]string{"Cookie": "69tikiman69", "User-Agent": "BOBKILL", "Referer": match}
req.Header.Set("User-Agent", headers["User-Agent"])
req.Header.Set("Cookie", headers["Cookie"])
req.Header.Set("Referer", headers["Referer"])
res, err := client.Do(req)
if err != nil {
log.Fatalf("Error doing the HTTP request %v", err)
return
}
longUrl := strings.Split(res.Request.URL.String(), "?")[0]
req, err = http.NewRequest("GET", longUrl, nil)
if err != nil {
log.Fatalf("Error creating longUrl request %v", err)
return
}
req.Header.Set("User-Agent", headers["User-Agent"])
req.Header.Set("Cookie", headers["Cookie"])
req.Header.Set("Referer", headers["Referer"])
res, err = client.Do(req)
if err != nil {
log.Fatalf("Error doing the longUrl HTTP request %v", err)
return
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("Error reading response body: %v", err)
return
}
bodyReplaced := strings.ReplaceAll(string(body), "amp;", "")
videoUrlRegEx, err := regexp.Compile(`content="(https://[^w-w]{3}(.*?)tiktok\.com/video/tos(.*?)&vr=)`)
if err != nil {
log.Fatalf("Error compiling regular expression 2: %v", err)
return
}
videoUrlMatches := videoUrlRegEx.FindAllStringSubmatch(bodyReplaced, 1)
if len(videoUrlMatches) == 0 {
log.Fatalf("No matches for videoUrlRegex: %v", videoUrlMatches)
}
videoUrl := videoUrlMatches[0][1]
fileBytes, err := GetVideo(videoUrl, headers, client)
if err != nil {
log.Fatalf("Error downloading video: %v", err)
}
fmt.Println("Sending the video!")
s.ChannelFileSend(m.ChannelID, "video.mp4", bytes.NewReader(fileBytes))
}
}
func GetVideo(url string, headers map[string]string, client *http.Client) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", headers["User-Agent"])
req.Header.Set("Cookie", headers["Cookie"])
req.Header.Set("Referer", headers["Referer"])
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fileBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return fileBytes, err
}