Compare commits
47 Commits
36a245bad3
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| ae7a0b79d9 | |||
| 0453f02dd0 | |||
| 0f8fe62587 | |||
| c13053f116 | |||
| b765f1cbb1 | |||
| a914fcdbf9 | |||
| 6510e1abc3 | |||
| 51ad3d22f2 | |||
| a1878fb39a | |||
| b9b9dd0e10 | |||
| 2fec1fe46f | |||
| 6be3f231db | |||
| 52b4bb6a1a | |||
| 17d9128577 | |||
| f98837bc9d | |||
| a33446e5e7 | |||
| 6966568ec1 | |||
| bef0de7e5b | |||
| 14e68fa3ed | |||
| e5e9905824 | |||
| 6647489083 | |||
| 978fb3f950 | |||
| 4c5d046d2d | |||
| 853eae6c4e | |||
| ffbfce3ebb | |||
| adcaa81750 | |||
| e7589c4188 | |||
| dd95bcf73e | |||
| 12cfe02319 | |||
| d668c2d3c9 | |||
| 3dac24062e | |||
| 61f8f50030 | |||
| 0df7326c1c | |||
| 8fc0d3062b | |||
| 56b88c8f51 | |||
| a1ed26f736 | |||
| ec76743a7c | |||
| 6e3297df4f | |||
| 968d670b29 | |||
| 872cdf45f3 | |||
| 184e045243 | |||
| 80a5b8c05f | |||
| a22c3e6b2f | |||
| f887166ca5 | |||
| 9b7ca0a125 | |||
| a003083b8b | |||
| 4e81567338 |
17
README.md
17
README.md
@@ -2,3 +2,20 @@
|
|||||||
Rewrite of my Discord bot, BunnyBot, in Go.
|
Rewrite of my Discord bot, BunnyBot, in Go.
|
||||||
|
|
||||||
Previous version, written in Javascript using Node.js: https://git.dtam.pw/daniel/discord-bot-js
|
Previous version, written in Javascript using Node.js: https://git.dtam.pw/daniel/discord-bot-js
|
||||||
|
|
||||||
|
## To do
|
||||||
|
Restore previous functionality:
|
||||||
|
- Allow roll ranges
|
||||||
|
- Add 8 ball
|
||||||
|
- Add compute
|
||||||
|
- Add voice commands
|
||||||
|
|
||||||
|
New functionalities:
|
||||||
|
- ~~Add caching mechanism for images~~
|
||||||
|
- Routely delete cache
|
||||||
|
- Make sure same image isn't repeated for x amount of time
|
||||||
|
- ~~Add ability to make coinflip with options to replace heads/tails~~
|
||||||
|
- Add statistics tracking
|
||||||
|
|
||||||
|
## Building standalone
|
||||||
|
Instructions for myself on how to build this for an Alpine Linux container: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build bunnybot.go commands.go images.go auth.go
|
||||||
|
|||||||
2
auth.go
2
auth.go
@@ -17,7 +17,7 @@ type Auth struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// build the auth struct
|
// build the auth struct
|
||||||
func buildAuth() {
|
func build_auth() {
|
||||||
// read the auth json file
|
// read the auth json file
|
||||||
json, err := ioutil.ReadFile("auth.json")
|
json, err := ioutil.ReadFile("auth.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
70
bunnybot.go
70
bunnybot.go
@@ -6,17 +6,31 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
auth Auth
|
auth Auth
|
||||||
|
|
||||||
|
// cache settings
|
||||||
|
cache_location string = "cache/"
|
||||||
|
cache_time = 3 * time.Hour // 3 hours
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
// set the randomized seed
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
// build the authentication struct
|
// build the authentication struct
|
||||||
buildAuth()
|
build_auth()
|
||||||
|
|
||||||
|
// attempt to make our cached
|
||||||
|
if _, err := os.Stat(cache_location); os.IsNotExist(err) {
|
||||||
|
os.Mkdir(cache_location, 0777)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -49,27 +63,59 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
|
func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
// ignore messages from the bot
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// make sure we match our bot's message token
|
// make sure we match our bot's message token
|
||||||
if len(m.Content) > 2 {
|
if len(m.Content) > 2 {
|
||||||
|
// get the input token
|
||||||
input_token := m.Content[0:2]
|
input_token := m.Content[0:2]
|
||||||
if strings.ToLower(string(input_token)) != "t." {
|
|
||||||
|
// check if we match our token
|
||||||
|
found := false
|
||||||
|
|
||||||
|
// try to find our token
|
||||||
|
if strings.ToLower(string(input_token)) == "b." || strings.ToLower(string(input_token)) == "//" {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// return if this message isn't for us
|
||||||
|
if found == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else { // message too short, don't do anything
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// get our message without our bot's token
|
// get our message's content without our bot's token
|
||||||
message := m.Content[2:]
|
content := strings.ToLower(m.Content[2:])
|
||||||
|
|
||||||
// awwnime (test)
|
// split our message's content into parts based on space
|
||||||
if message == "awwnime" {
|
message := strings.Fields(content)
|
||||||
|
|
||||||
|
// determine our actions
|
||||||
|
if message[0] == "coinflip" || message[0] == "coin" { // flip a coin
|
||||||
|
s.ChannelMessageSend(m.ChannelID, coinflip(m.Author.ID, content))
|
||||||
|
} else if message[0] == "roll" { // roll a number
|
||||||
|
s.ChannelMessageSend(m.ChannelID, roll(m.Author.ID))
|
||||||
|
} else if message[0] == "source" { // print source code
|
||||||
|
s.ChannelMessageSend(m.ChannelID, source())
|
||||||
|
} else if message[0] == "retarded" { // retarded youtube video
|
||||||
|
s.ChannelMessageSend(m.ChannelID, "https://youtu.be/kav7tifmyTg")
|
||||||
|
} else if message[0] == "moon" { // wsb moon stock ticker copypasta
|
||||||
|
s.ChannelMessageSend(m.ChannelID, moon(content)) // print moon text
|
||||||
|
} else if len(message[0]) > 0 { // as long as there is a message, try to find a picture
|
||||||
// get url
|
// get url
|
||||||
url := <-get_redditbooru_image("awwnime")
|
url := <-get_image(message[0])
|
||||||
|
|
||||||
// print message with url
|
// make sure we have a url returned
|
||||||
s.ChannelMessageSend(m.ChannelID, url)
|
if len(url) > 0 {
|
||||||
|
s.ChannelMessageSend(m.ChannelID, url)
|
||||||
|
} else {
|
||||||
|
s.ChannelMessageSend(m.ChannelID, "I couldn't find that, sauce?")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//get_redditbooru("awwnime")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
70
commands.go
Normal file
70
commands.go
Normal file
File diff suppressed because one or more lines are too long
@@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
This contains our functions used for image searches
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
"math/rand"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/buger/jsonparser"
|
|
||||||
)
|
|
||||||
|
|
||||||
// get the length of array for our parser
|
|
||||||
func getArrayLen(value []byte) (int, error) {
|
|
||||||
ret := 0
|
|
||||||
arrayCallback := func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
|
||||||
ret++
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := jsonparser.ArrayEach(value, arrayCallback); err != nil {
|
|
||||||
return 0, fmt.Errorf("getArrayLen ArrayEach error: %v", err)
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// redditbooru request
|
|
||||||
func get_redditbooru_image(sub string) <-chan string{
|
|
||||||
// make the channel
|
|
||||||
ret := make(chan string)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(ret)
|
|
||||||
|
|
||||||
// create the proper url with the subreddit
|
|
||||||
url := "https://" + sub + ".redditbooru.com/images/?limit=1000"
|
|
||||||
|
|
||||||
// set 5 second timeout on request
|
|
||||||
client := http.Client {
|
|
||||||
Timeout: 5 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the content of the page
|
|
||||||
resp, err := client.Get(url)
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// read response
|
|
||||||
out, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// randomize the seed
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
|
|
||||||
// get a random number for the image
|
|
||||||
outlen,err := getArrayLen(out)
|
|
||||||
random_img := rand.Intn(outlen)
|
|
||||||
|
|
||||||
// select a random url from our list
|
|
||||||
img_url,err := jsonparser.GetString(out, "[" + strconv.Itoa(random_img) + "]", "cdnUrl")
|
|
||||||
|
|
||||||
// set the return value
|
|
||||||
ret <- img_url
|
|
||||||
}()
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
14
go.mod
Normal file
14
go.mod
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
module GoBunnyBot
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/buger/jsonparser v1.1.1
|
||||||
|
github.com/bwmarrin/discordgo v0.27.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
|
golang.org/x/crypto v0.7.0 // indirect
|
||||||
|
golang.org/x/sys v0.6.0 // indirect
|
||||||
|
)
|
||||||
45
go.sum
Normal file
45
go.sum
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
|
||||||
|
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||||
|
github.com/bwmarrin/discordgo v0.27.0 h1:4ZK9KN+rGIxZ0fdGTmgdCcliQeW8Zhu6MnlFI92nf0Q=
|
||||||
|
github.com/bwmarrin/discordgo v0.27.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
|
||||||
|
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
224
images.go
Normal file
224
images.go
Normal file
@@ -0,0 +1,224 @@
|
|||||||
|
/*
|
||||||
|
This contains our functions used for image searches
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
"math/rand"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/buger/jsonparser"
|
||||||
|
)
|
||||||
|
|
||||||
|
// get the length of array for our parser
|
||||||
|
func getArrayLen(value []byte) (int, error) {
|
||||||
|
ret := 0
|
||||||
|
arrayCallback := func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
||||||
|
ret++
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := jsonparser.ArrayEach(value, arrayCallback); err != nil {
|
||||||
|
return 0, fmt.Errorf("getArrayLen ArrayEach error: %v", err)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the length of object for our parser
|
||||||
|
func getObjectLen(value []byte) (int, error) {
|
||||||
|
ret := 0
|
||||||
|
objectCallback := func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
|
||||||
|
ret++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := jsonparser.ObjectEach(value, objectCallback); err != nil {
|
||||||
|
return 0, fmt.Errorf("getObjectLen ObjectEach error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// search based on the most appropriate location
|
||||||
|
func get_image(sub string) <-chan string {
|
||||||
|
// make the channell
|
||||||
|
ret := make(chan string)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(ret)
|
||||||
|
|
||||||
|
// setup variable
|
||||||
|
var image string
|
||||||
|
var cache_exists bool = false
|
||||||
|
|
||||||
|
// check if a cached version exists
|
||||||
|
file, err := os.Stat(cache_location + sub)
|
||||||
|
|
||||||
|
// if it exists, check it's timestamp
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
// get the current time
|
||||||
|
currenttime := time.Now()
|
||||||
|
|
||||||
|
// get the file's last modified time
|
||||||
|
modifiedtime := file.ModTime()
|
||||||
|
|
||||||
|
// get the difference in time
|
||||||
|
difftime := currenttime.Sub(modifiedtime)
|
||||||
|
|
||||||
|
// check if it meets our cutoff time
|
||||||
|
if difftime > cache_time {
|
||||||
|
// too old, delete the old file
|
||||||
|
os.Remove(cache_location + sub)
|
||||||
|
} else {
|
||||||
|
// else it's new enough, let it be used later
|
||||||
|
cache_exists = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check reddit
|
||||||
|
image = <-get_subreddit_image(sub, cache_exists)
|
||||||
|
ret <- image
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
// nothing found
|
||||||
|
ret <- ""
|
||||||
|
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// imgur request
|
||||||
|
func get_imgur_image(sub string) <-chan string {
|
||||||
|
// make channel
|
||||||
|
ret := make(chan string)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(ret)
|
||||||
|
|
||||||
|
// create the proper url with the subreddit
|
||||||
|
url := "https://api.imgur.com/3/gallery/r/" + sub + "/time/1"
|
||||||
|
|
||||||
|
// set 5 second timeout on request
|
||||||
|
client := http.Client {
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the request
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Add("Authorization", "Client-ID " + auth.imgur)
|
||||||
|
|
||||||
|
// get the content of the page
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// read response
|
||||||
|
out, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading response from imgur, ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get a random number for the image
|
||||||
|
//outlen, _ := getArrayLen(out)
|
||||||
|
//random_img := rand.Intn(outlen)
|
||||||
|
|
||||||
|
// parse the data (fix this)
|
||||||
|
img_url, _ := jsonparser.GetString(out, "[0]", "[0]", "link")
|
||||||
|
fmt.Println(string(img_url))
|
||||||
|
|
||||||
|
ret <- ""
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// subreddit request
|
||||||
|
func get_subreddit_image(sub string, cache bool) <-chan string {
|
||||||
|
ret := make(chan string)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(ret)
|
||||||
|
|
||||||
|
// information variable
|
||||||
|
var out []byte
|
||||||
|
|
||||||
|
// if a cached version exists, use that instead
|
||||||
|
if cache == true {
|
||||||
|
// read our cached file
|
||||||
|
outdata, err := ioutil.ReadFile(cache_location + sub)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading from cached reddit file, ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy to our info var
|
||||||
|
out = outdata
|
||||||
|
|
||||||
|
} else { // else pull it from the web
|
||||||
|
// create the proper url with the subreddit
|
||||||
|
url := "https://www.reddit.com/r/" + sub + "/.json?show=all&limit=100"
|
||||||
|
|
||||||
|
// set 5 second timeout on request
|
||||||
|
client := http.Client {
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Add("User-Agent", "BunnyBot")
|
||||||
|
|
||||||
|
// get the content of the page
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// read response
|
||||||
|
outdata, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading response from reddit, ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy to our info var
|
||||||
|
out = outdata
|
||||||
|
|
||||||
|
// attempt to make a cache file, don't do anything if it doesn't get created (who cares?)
|
||||||
|
os.Create(cache_location + sub)
|
||||||
|
var file, _ = os.OpenFile(cache_location + sub, os.O_RDWR, 0777)
|
||||||
|
defer file.Close()
|
||||||
|
file.Write(outdata)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// make sure we aren't grabbing a text post by cylcing through looking for an image
|
||||||
|
limit64, _ := jsonparser.GetInt(out, "data", "dist")
|
||||||
|
limit := int(limit64) // convert from int64 to int
|
||||||
|
|
||||||
|
// loop through and try to find a post that isn't a text post
|
||||||
|
for i := 0; i < limit; i++ {
|
||||||
|
// get a random number
|
||||||
|
random_img := rand.Intn(limit)
|
||||||
|
|
||||||
|
// check the post hint to see what type of post it is
|
||||||
|
hint, _ := jsonparser.GetString(out, "data", "children", "[" + strconv.Itoa(random_img) + "]", "data", "post_hint")
|
||||||
|
|
||||||
|
// get the domain to also check againist
|
||||||
|
domain, _ := jsonparser.GetString(out, "data", "children", "[" + strconv.Itoa(random_img) + "]", "data", "domain")
|
||||||
|
|
||||||
|
// make sure that it is an image, or at least a gif
|
||||||
|
if hint == "image" || hint == "link" || hint == "rich:video" || domain == "i.redd.it" || domain == "i.imgur.com" {
|
||||||
|
image, _ := jsonparser.GetString(out, "data", "children", "[" + strconv.Itoa(random_img) + "]", "data", "url")
|
||||||
|
ret <- image
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user