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

Commit

Permalink
adds support for interactive channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Winkler committed Nov 19, 2016
1 parent fde848e commit 8c482c3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
63 changes: 59 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

var (
ChannelWhitelist map[string]string
InteractiveChannelWhitelist map[string]string
BaseDownloadPath string
RegexpUrlTwitter *regexp.Regexp
RegexpUrlTistory *regexp.Regexp
Expand All @@ -38,10 +39,12 @@ var (
ImagesDownloaded int
dg *discordgo.Session
DownloadTistorySites bool
interactiveChannelLinkTemp map[string]string
DiscordUserId string
)

const (
VERSION string = "1.10"
VERSION string = "1.11"
RELEASE_URL string = "https://github.com/Seklfreak/discord-image-downloader-go/releases/latest"
IMGUR_CLIENT_ID string = "a39473314df3f59"
REGEXP_URL_TWITTER string = `^http(s?):\/\/pbs\.twimg\.com\/media\/[^\./]+\.(jpg|png)((\:[a-z]+)?)$`
Expand Down Expand Up @@ -97,6 +100,8 @@ func main() {
}

ChannelWhitelist = cfg.Section("channels").KeysHash()
InteractiveChannelWhitelist = cfg.Section("interactive channels").KeysHash()
interactiveChannelLinkTemp = make(map[string]string)

RegexpUrlTwitter, err = regexp.Compile(REGEXP_URL_TWITTER)
if err != nil {
Expand Down Expand Up @@ -179,6 +184,7 @@ func main() {

fmt.Printf("Client is now connected as %s (ID: %s). Press CTRL-C to exit.\n",
u.Username, u.ID)
DiscordUserId = u.ID

err = dg.UpdateStatus(1, "")
if err != nil {
Expand Down Expand Up @@ -270,20 +276,69 @@ func getDownloadLinks(url string) map[string]string {

func handleDiscordMessage(m *discordgo.Message) {
if folderName, ok := ChannelWhitelist[m.ChannelID]; ok {
downloadPath := folderName
for _, iAttachment := range m.Attachments {
downloadFromUrl(iAttachment.URL, iAttachment.Filename, downloadPath)
downloadFromUrl(iAttachment.URL, iAttachment.Filename, folderName)
}
foundUrls := xurls.Strict.FindAllString(m.Content, -1)
for _, iFoundUrl := range foundUrls {
links := getDownloadLinks(iFoundUrl)
for link, filename := range links {
downloadFromUrl(link, filename, downloadPath)
downloadFromUrl(link, filename, folderName)
}
}
} else if folderName, ok := InteractiveChannelWhitelist[m.ChannelID]; ok {
if DiscordUserId != "" && m.Author.ID != DiscordUserId {
if link, ok := interactiveChannelLinkTemp[m.ChannelID]; ok {
if m.Content == "." {
dg.ChannelMessageSend(m.ChannelID, "Download started\n")
links := getDownloadLinks(link)
for linkR, filename := range links {
downloadFromUrl(linkR, filename, folderName)
}
delete(interactiveChannelLinkTemp, m.ChannelID)
} else if IsValid(m.Content) {
dg.ChannelMessageSend(m.ChannelID, "Download started\n")
links := getDownloadLinks(link)
for linkR, filename := range links {
downloadFromUrl(linkR, filename, m.Content)
}
delete(interactiveChannelLinkTemp, m.ChannelID)
} else {
dg.ChannelMessageSend(m.ChannelID, "invalid path")
}
} else {
_ = folderName
for _, iAttachment := range m.Attachments {
dg.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Where do you want to save <%s>?\nType **.** for default path %s", iAttachment.URL, folderName))
interactiveChannelLinkTemp[m.ChannelID] = iAttachment.URL
}
foundUrls := xurls.Strict.FindAllString(m.Content, -1)
for _, iFoundUrl := range foundUrls {
dg.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Where do you want to save <%s>?\nType **.** for default path %s", iFoundUrl, folderName))
interactiveChannelLinkTemp[m.ChannelID] = iFoundUrl
}
}
}
}
}

// http://stackoverflow.com/a/35240286/1443726
func IsValid(fp string) bool {
// Check if file already exists
if _, err := os.Stat(fp); err == nil {
return true
}

// Attempt to create it
var d []byte
if err := ioutil.WriteFile(fp, d, 0644); err == nil {
os.Remove(fp) // And delete it
return true
}

return false
}

func getTwitterUrls(url string) (map[string]string, error) {
parts := strings.Split(url, ":")
if len(parts) < 2 {
Expand Down
22 changes: 12 additions & 10 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,23 @@ func TestGetTistoryWithCDNUrls(t *testing.T) {

var getPossibleTistorySiteUrlsTests = []urlsTestpair{
{
"http://shaq32.tistory.com/395",
"http://soonduck.tistory.com/482",
map[string]string{
"http://cfile24.uf.tistory.com/original/2658554B580BDC4C0924CA": "A10Y4472.jpg",
"http://cfile21.uf.tistory.com/original/26469D4B580BDC4D0D0387": "A27R9111.jpg",
"http://cfile29.uf.tistory.com/original/2744A54B580BDC4F0EFAEE": "A10Y5545.jpg",
"http://cfile28.uf.tistory.com/original/2758534B580BDC500969E4": "A10Y5613.jpg",
"http://cfile5.uf.tistory.com/original/230AA53C57D2D7F42124AB": "20160909-A27R3648.jpg",
"http://cfile9.uf.tistory.com/original/2675943C57D2D7F532FA76": "20160909-A27R3784.jpg",
"http://cfile23.uf.tistory.com/original/230E6C3C57D2D7F71D973C": "20160909-A27R4027.jpg",
"http://cfile6.uf.tistory.com/original/250BC13C57D2D7F820836E": "20160909-A27R4535.jpg",
"http://cfile5.uf.tistory.com/original/217A0F3C57D2D7F228A3EE": "20160909-A27R3325.jpg",
},
},
{
"http://shaq32.tistory.com/m/395",
"http://soonduck.tistory.com/m/482",
map[string]string{
"http://cfile24.uf.tistory.com/original/2658554B580BDC4C0924CA": "",
"http://cfile21.uf.tistory.com/original/26469D4B580BDC4D0D0387": "",
"http://cfile29.uf.tistory.com/original/2744A54B580BDC4F0EFAEE": "",
"http://cfile28.uf.tistory.com/original/2758534B580BDC500969E4": "",
"http://cfile5.uf.tistory.com/original/230AA53C57D2D7F42124AB": "20160909-A27R3648.jpg",
"http://cfile9.uf.tistory.com/original/2675943C57D2D7F532FA76": "20160909-A27R3784.jpg",
"http://cfile23.uf.tistory.com/original/230E6C3C57D2D7F71D973C": "20160909-A27R4027.jpg",
"http://cfile6.uf.tistory.com/original/250BC13C57D2D7F820836E": "20160909-A27R4535.jpg",
"http://cfile5.uf.tistory.com/original/217A0F3C57D2D7F228A3EE": "20160909-A27R3325.jpg",
},
},
{
Expand Down

0 comments on commit 8c482c3

Please sign in to comment.