Skip to content

Commit

Permalink
Merge branch 'main' into resize_icons
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoisin authored Dec 14, 2024
2 parents 7fb50f1 + 945d436 commit 953fc76
Showing 1 changed file with 28 additions and 42 deletions.
70 changes: 28 additions & 42 deletions internal/reader/rewrite/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package rewrite // import "miniflux.app/v2/internal/reader/rewrite"

import "regexp"
import (
"net/url"
"strings"
)

// List of predefined rewrite rules (alphabetically sorted)
// Available rules: "add_image_title", "add_youtube_video"
Expand Down Expand Up @@ -39,49 +42,32 @@ var predefinedRules = map[string]string{
"youtube.com": "add_youtube_video",
}

type RefererRule struct {
URLPattern *regexp.Regexp
Referer string
}
// GetRefererForURL returns the referer for the given URL if it exists, otherwise an empty string.
func GetRefererForURL(u string) string {
parsedUrl, err := url.Parse(u)
if err != nil {
return ""
}

// List of predefined referer rules
var PredefinedRefererRules = []RefererRule{
{
URLPattern: regexp.MustCompile(`^https://\w+\.sinaimg\.cn`),
Referer: "https://weibo.com",
},
{
URLPattern: regexp.MustCompile(`^https://i\.pximg\.net`),
Referer: "https://www.pixiv.net",
},
{
URLPattern: regexp.MustCompile(`^https://cdnfile\.sspai\.com`),
Referer: "https://sspai.com",
},
{
URLPattern: regexp.MustCompile(`^https://(?:\w|-)+\.cdninstagram\.com`),
Referer: "https://www.instagram.com",
},
{
URLPattern: regexp.MustCompile(`^https://sp1\.piokok\.com`),
Referer: "https://sp1.piokok.com",
},
{
URLPattern: regexp.MustCompile(`^https://f\.video\.weibocdn\.com`),
Referer: "https://weibo.com",
},
{
URLPattern: regexp.MustCompile(`^https://img\.hellogithub\.com`),
Referer: "https://hellogithub.com",
},
}
switch parsedUrl.Hostname() {
case "i.pximg.net":
return "https://www.pixiv.net"
case "sp1.piokok.com":
return "https://sp1.piokok.com"
case "cdnfile.sspai.com":
return "https://sspai.com"
case "f.video.weibocdn.com":
return "https://weibo.com"
case "img.hellogithub.com":
return "https://hellogithub.com"
}

// GetRefererForURL returns the referer for the given URL if it exists, otherwise an empty string.
func GetRefererForURL(url string) string {
for _, rule := range PredefinedRefererRules {
if rule.URLPattern.MatchString(url) {
return rule.Referer
}
switch {
case strings.HasSuffix(parsedUrl.Hostname(), ".sinaimg.cn"):
return "https://weibo.com"
case strings.HasSuffix(parsedUrl.Hostname(), ".cdninstagram.com"):
return "https://www.instagram.com"
}

return ""
}

0 comments on commit 953fc76

Please sign in to comment.