Skip to content

Commit b9388cf

Browse files
committed
gelbooru: Fix for bad source links
1 parent a4e812a commit b9388cf

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

plugin/gelbooru/api.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gelbooru
22

33
import (
44
"fmt"
5+
"net/url"
56
"strings"
67
)
78

@@ -78,8 +79,8 @@ func (p *Post) Caption() string {
7879

7980
sb.WriteString(fmt.Sprintf("🔗 <a href=\"%s\">Post #%d</a> - ", p.PostURL(), p.Id))
8081
sb.WriteString(fmt.Sprintf("🖼️ <a href=\"%s\">Direktlink</a>", p.DirectURL()))
81-
if p.Source != "" {
82-
sb.WriteString(fmt.Sprintf(" - 🌐 <a href=\"%s\">Quelle</a>\n", p.Source))
82+
if validSource := p.ValidSource(); validSource != "" {
83+
sb.WriteString(fmt.Sprintf(" - 🌐 <a href=\"%s\">Quelle</a>\n", validSource))
8384
}
8485

8586
return sb.String()
@@ -94,8 +95,8 @@ func (p *Post) AltCaption() string {
9495
sb.WriteString("️🔞 <b>NSFW</b> - ")
9596
}
9697
sb.WriteString(fmt.Sprintf("🔗 <a href=\"%s\">Post #%d</a>", p.PostURL(), p.Id))
97-
if p.Source != "" {
98-
sb.WriteString(fmt.Sprintf(" - 🌐 <a href=\"%s\">Quelle</a>\n", p.Source))
98+
if validSource := p.ValidSource(); validSource != "" {
99+
sb.WriteString(fmt.Sprintf(" - 🌐 <a href=\"%s\">Quelle</a>\n", validSource))
99100
}
100101

101102
return sb.String()
@@ -105,6 +106,37 @@ func (p *Post) PostURL() string {
105106
return fmt.Sprintf(PostURL, p.Id)
106107
}
107108

109+
func (p *Post) ValidSource() string {
110+
if p.Source == "" {
111+
return ""
112+
}
113+
114+
sources := strings.Split(p.Source, "|")
115+
for _, src := range sources {
116+
src = strings.TrimSpace(src)
117+
if src == "" {
118+
continue
119+
}
120+
121+
parsedURL, err := url.Parse(src)
122+
if err != nil {
123+
continue
124+
}
125+
126+
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
127+
continue
128+
}
129+
130+
if parsedURL.Host == "" {
131+
continue
132+
}
133+
134+
return src
135+
}
136+
137+
return ""
138+
}
139+
108140
func (p *Post) IsImage() bool {
109141
if strings.HasSuffix(p.FileURL(), ".jpg") ||
110142
strings.HasSuffix(p.FileURL(), ".jpeg") ||

0 commit comments

Comments
 (0)