Skip to content

Commit

Permalink
Merge pull request #26 from kekkodance/main
Browse files Browse the repository at this point in the history
bugfix for magnet links, improvements and fixes to the codebase
  • Loading branch information
brandongallagher1999 authored Jun 30, 2022
2 parents f221871 + 92a9892 commit c2020ce
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 71 deletions.
130 changes: 77 additions & 53 deletions internal/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func Create(config *config.Conf) {
fmt.Println("error opening connection,", err)
return
}
discord.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: "online",
Activities: []*discordgo.Activity{
&discordgo.Activity{
Name: "you use .torrent",
Type: discordgo.ActivityTypeWatching,
},
},
})
discord.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: "dnd",
Activities: []*discordgo.Activity{
&discordgo.Activity{
Name: "you use .torrent",
Type: discordgo.ActivityTypeWatching,
},
},
})
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)
Expand All @@ -59,52 +59,76 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if string(m.Content[0]) == botConfig.Discord.Prefix {
//Trasform potential query into array
splitMessage := strings.Fields(m.Content)
if len(splitMessage) > 1 { //If .torrent <torrent> is called
switch splitMessage[0] {
case fmt.Sprintf("%s%s", botConfig.Discord.Prefix, botConfig.Discord.Command):
s.ChannelTyping(m.ChannelID)
queryString := strings.Join(splitMessage[1:], " ")
torrentLinks := torrentserviceutils.QueryTorrentService(queryString)
if len(torrentLinks) == 0 || torrentLinks == nil {

author := &discordgo.MessageEmbedAuthor{Name: m.Author.Username, IconURL: m.Author.AvatarURL(""),}
newField := &discordgo.MessageEmbedField{Name: "Not Found", Value: "Torrent not found on 1337x, please refine your search.", Inline: false}
fieldArray := make([]*discordgo.MessageEmbedField, 0)
fieldArray = append(fieldArray, newField)
embed := &discordgo.MessageEmbed{Type: discordgo.EmbedTypeLink, Author: author, Color: 16711680, Fields: fieldArray[:]}
complexMessage := &discordgo.MessageSend{Embed: embed}
_, err := s.ChannelMessageSendComplex(m.ChannelID, complexMessage)
if err != nil {
fmt.Println(err)
}
return
}
shortened := mgnetmeutils.GetMagnetLinks(torrentLinks[:])
fieldArray := make([]*discordgo.MessageEmbedField, 0)
var counter int = 1
for i := range shortened {
name := fmt.Sprintf("%d. %s ", counter, shortened[i].Title)
value := fmt.Sprintf("**[magnet](%s)** | Seeds: %d | Size: %s", shortened[i].Magnet, shortened[i].Seeds, shortened[i].Size)
newField := &discordgo.MessageEmbedField{Name: name, Value: value, Inline: false}
fieldArray = append(fieldArray, newField)
counter++
}
author := &discordgo.MessageEmbedAuthor{Name: m.Author.Username, IconURL: m.Author.AvatarURL(""),}
footer := &discordgo.MessageEmbedFooter{Text: "For more results, check 1337x."}
embed := &discordgo.MessageEmbed{Type: discordgo.EmbedTypeLink, Author: author, Footer: footer, Color: 15102219, Fields: fieldArray[:]}
complexMessage := &discordgo.MessageSend{Embed: embed}
_, err := s.ChannelMessageSendComplex(m.ChannelID, complexMessage)
if err != nil {
fmt.Println(err)
}
case fmt.Sprintf("%s%s", botConfig.Discord.Prefix, "help"):
s.ChannelMessageSend(m.ChannelID,
"``` .torrent <torrent name> // .torrent The Witcher 3 Wild Hunt \n .invite // Invite link to the Discord Bot```")
case fmt.Sprintf("%s%s", botConfig.Discord.Prefix, "github"):
s.ChannelMessageSend(m.ChannelID, "https://github.com/brandongallagher1999/1337x-Bot-Go")
}
command := splitMessage[0]
switch command {
case fmt.Sprintf("%s%s", botConfig.Discord.Prefix, botConfig.Discord.Command):
torrentCmd(s, m, splitMessage)
case fmt.Sprintf("%s%s", botConfig.Discord.Prefix, "help"):
helpCmd(s, m)
case fmt.Sprintf("%s%s", botConfig.Discord.Prefix, "github"):
githubCmd(s, m)
}
}

}
}

func torrentCmd(s *discordgo.Session, m *discordgo.MessageCreate, args []string) {
if len(args) <= 1 {
s.ChannelMessageSend(m.ChannelID, "No search query was given. Please run the command again with something to search.")
return
}

s.ChannelTyping(m.ChannelID)
queryString := strings.Join(args[1:], " ")
torrentLinks := torrentserviceutils.QueryTorrentService(queryString)

if len(torrentLinks) == 0 || torrentLinks == nil {
author := &discordgo.MessageEmbedAuthor{Name: m.Author.Username, IconURL: m.Author.AvatarURL("")}
newField := &discordgo.MessageEmbedField{Name: "Not Found", Value: "Torrent not found on 1337x, please refine your search.", Inline: false}

embed := &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeLink,
Author: author,
Color: 16711680,
Fields: []*discordgo.MessageEmbedField{newField},
}

complexMessage := &discordgo.MessageSend{Embed: embed}

_, err := s.ChannelMessageSendComplex(m.ChannelID, complexMessage)
if err != nil {
fmt.Println(err)
}
return
}

shortened := mgnetmeutils.GetMagnetLinks(torrentLinks[:])
fieldArray := make([]*discordgo.MessageEmbedField, 0)

for idx, torrent := range shortened {
name := fmt.Sprintf("%d. %s ", idx+1, torrent.Title)
value := fmt.Sprintf("**[magnet](%s)** | Seeds: %d | Size: %s", torrent.Magnet, torrent.Seeds, torrent.Size)
newField := &discordgo.MessageEmbedField{Name: name, Value: value, Inline: false}
fieldArray = append(fieldArray, newField)
}

author := &discordgo.MessageEmbedAuthor{Name: m.Author.Username, IconURL: m.Author.AvatarURL("")}
footer := &discordgo.MessageEmbedFooter{Text: "For more results, check 1337x."}
embed := &discordgo.MessageEmbed{Type: discordgo.EmbedTypeLink, Author: author, Footer: footer, Color: 15102219, Fields: fieldArray[:]}
complexMessage := &discordgo.MessageSend{Embed: embed}

_, err := s.ChannelMessageSendComplex(m.ChannelID, complexMessage)
if err != nil {
fmt.Println(err)
}
}

func helpCmd(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID,
"```.torrent <torrent name> // .torrent The Witcher 3 Wild Hunt```")
}

func githubCmd(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, "<https://github.com/brandongallagher1999/1337x-Bot-Go>")
}
21 changes: 12 additions & 9 deletions internal/mgnetmeutils/linkutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,31 @@ type MgnetmeResponse struct {
Message string `json:"message"`
}

type UpdateMagnetLink struct {
Index int
Link string
}

func GetMagnetLinks(torrentLinks []torrentserviceutils.TorrentServiceResponse) []torrentserviceutils.TorrentServiceResponse {
linkShortenerChannel := make(chan string)
for i := range torrentLinks {
linkShortenerChannel := make(chan UpdateMagnetLink)
for idx, torrent := range torrentLinks {
linkShortenWaitGroup.Add(1)
go shortenLink(linkShortenerChannel, torrentLinks[i].Magnet)
go shortenLink(linkShortenerChannel, idx, torrent.Magnet)
}

go func() {
linkShortenWaitGroup.Wait()
close(linkShortenerChannel)
}()

var i int = 0
for link := range linkShortenerChannel {
torrentLinks[i].Magnet = link
i++
for response := range linkShortenerChannel {
torrentLinks[response.Index].Magnet = response.Link
}

return torrentLinks
}

func shortenLink(chnl chan string, magnetLink string) {
func shortenLink(chnl chan UpdateMagnetLink, idx int, magnetLink string) {
response, err := http.Get("http://mgnet.me/api/create?&format=json&opt=&m=" + url.QueryEscape(magnetLink))
if err != nil {
log.Fatal(err)
Expand All @@ -55,6 +58,6 @@ func shortenLink(chnl chan string, magnetLink string) {
if err != nil {
log.Fatal(err)
}
chnl <- responseObject.Shorturl
chnl <- UpdateMagnetLink{Index: idx, Link: responseObject.Shorturl}
linkShortenWaitGroup.Done()
}
21 changes: 12 additions & 9 deletions internal/torrentserviceutils/torrentutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ type LongMagnetResponse struct {
Magnet string `json:"magnet"`
}

func getLongMagnets(chnl chan string, desc string) {
type UpdateMagnetLink struct {
Index int
Link string
}

func getLongMagnets(chnl chan UpdateMagnetLink, idx int, desc string) {
response, err := http.Get("http://torrent-service:3000/longMagnet/" + url.QueryEscape(desc))
if err != nil {
log.Fatal(err)
Expand All @@ -46,12 +51,12 @@ func getLongMagnets(chnl chan string, desc string) {
log.Fatal(err)
}

chnl <- responseObject.Magnet
chnl <- UpdateMagnetLink{Index: idx, Link: responseObject.Magnet}
longMagnetWaitGroup.Done()
}

func QueryTorrentService(query string) []TorrentServiceResponse {
longMagnetChannel := make(chan string)
longMagnetChannel := make(chan UpdateMagnetLink)
response, err := http.Get("http://torrent-service:3000/" + url.QueryEscape(query))
if err != nil {
log.Fatal(err)
Expand All @@ -69,20 +74,18 @@ func QueryTorrentService(query string) []TorrentServiceResponse {
return nil
}

for i := range responseObject {
for idx, torrent := range responseObject {
longMagnetWaitGroup.Add(1)
go getLongMagnets(longMagnetChannel, responseObject[i].Desc)
go getLongMagnets(longMagnetChannel, idx, torrent.Desc)
}

go func() {
longMagnetWaitGroup.Wait()
close(longMagnetChannel)
}()

var j int = 0
for mgnt := range longMagnetChannel {
responseObject[j].Magnet = mgnt
j++
for response := range longMagnetChannel {
responseObject[response.Index].Magnet = response.Link
}

return responseObject
Expand Down

0 comments on commit c2020ce

Please sign in to comment.