Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
refactors message entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Dec 6, 2018
1 parent 47315c4 commit 71e1fbd
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 350 deletions.
42 changes: 42 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"path"
"strings"
)

Expand Down Expand Up @@ -30,3 +32,43 @@ func deduplicateDownloadItems(DownloadItems []*DownloadItem) []*DownloadItem {

return result
}

func updateDiscordStatus() {
dg.UpdateStatus(
0,
fmt.Sprintf("%d pictures downloaded", countDownloadedImages()),
)
}

func Pagify(text string, delimiter string) []string {
result := make([]string, 0)
textParts := strings.Split(text, delimiter)
currentOutputPart := ""
for _, textPart := range textParts {
if len(currentOutputPart)+len(textPart)+len(delimiter) <= 1992 {
if len(currentOutputPart) > 0 || len(result) > 0 {
currentOutputPart += delimiter + textPart
} else {
currentOutputPart += textPart
}
} else {
result = append(result, currentOutputPart)
currentOutputPart = ""
if len(textPart) <= 1992 {
currentOutputPart = textPart
}
}
}
if currentOutputPart != "" {
result = append(result, currentOutputPart)
}
return result
}

func filepathExtension(filepath string) string {
if strings.Contains(filepath, "?") {
filepath = strings.Split(filepath, "?")[0]
}
filepath = path.Ext(filepath)
return filepath
}
92 changes: 92 additions & 0 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package main
import (
"fmt"
"net/url"
"time"

"github.com/bwmarrin/discordgo"
"mvdan.cc/xurls"
)

func getDownloadLinks(inputURL string, channelID string, interactive bool) map[string]string {
Expand Down Expand Up @@ -150,3 +154,91 @@ func getDownloadLinks(inputURL string, channelID string, interactive bool) map[s

return skipDuplicateLinks(map[string]string{inputURL: ""}, channelID, interactive)
}

// getDownloadItemsOfMessage will extract all unique download links out of a message
func getDownloadItemsOfMessage(message *discordgo.Message) []*DownloadItem {
var downloadItems []*DownloadItem

linkTime, err := message.Timestamp.Parse()
if err != nil {
linkTime = time.Now()
}

rawLinks := getRawLinksOfMessage(message)
for _, rawLink := range rawLinks {
downloadLinks := getDownloadLinks(
rawLink.Link,
message.ChannelID,
false,
)
for link, filename := range downloadLinks {
if rawLink.Filename != "" {
filename = rawLink.Filename
}

downloadItems = append(downloadItems, &DownloadItem{
Link: link,
Filename: filename,
Time: linkTime,
})
}
}

downloadItems = deduplicateDownloadItems(downloadItems)

return downloadItems
}

// getRawLinksOfMessage will extract all raw links of a message
func getRawLinksOfMessage(message *discordgo.Message) []*DownloadItem {
var links []*DownloadItem

if message.Author == nil {
message.Author = new(discordgo.User)
}

for _, attachment := range message.Attachments {
links = append(links, &DownloadItem{
Link: attachment.URL,
Filename: attachment.Filename,
})
}

foundLinks := xurls.Strict.FindAllString(message.Content, -1)
for _, foundLink := range foundLinks {
links = append(links, &DownloadItem{
Link: foundLink,
})
}

for _, embed := range message.Embeds {
if embed.URL != "" {
links = append(links, &DownloadItem{
Link: embed.URL,
})
}

if embed.Description != "" {
foundLinks = xurls.Strict.FindAllString(embed.Description, -1)
for _, foundLink := range foundLinks {
links = append(links, &DownloadItem{
Link: foundLink,
})
}
}

if embed.Image != nil && embed.Image.URL != "" {
links = append(links, &DownloadItem{
Link: embed.Image.URL,
})
}

if embed.Video != nil && embed.Video.URL != "" {
links = append(links, &DownloadItem{
Link: embed.Video.URL,
})
}
}

return links
}
Loading

0 comments on commit 71e1fbd

Please sign in to comment.