feat: support searching well-known urls in subdirectory

This commit is contained in:
rook1e 2023-03-28 02:28:13 +08:00 committed by Frédéric Guillot
parent acc9186a59
commit 9a826bbe6f
2 changed files with 42 additions and 33 deletions

View File

@ -143,24 +143,30 @@ func parseYoutubeVideoPage(websiteURL string) string {
func tryWellKnownUrls(websiteURL, userAgent, cookie, username, password string) (Subscriptions, *errors.LocalizedError) { func tryWellKnownUrls(websiteURL, userAgent, cookie, username, password string) (Subscriptions, *errors.LocalizedError) {
var subscriptions Subscriptions var subscriptions Subscriptions
knownURLs := map[string]string{ knownURLs := map[string]string{
"/atom.xml": "atom", "atom.xml": "atom",
"/feed.xml": "atom", "feed.xml": "atom",
"/feed/": "atom", "feed/": "atom",
"/rss.xml": "rss", "rss.xml": "rss",
"/rss/": "rss", "rss/": "rss",
} }
lastCharacter := websiteURL[len(websiteURL)-1:] websiteURLRoot := url.RootURL(websiteURL)
if lastCharacter == "/" { baseURLs := []string{
websiteURL = websiteURL[:len(websiteURL)-1] // Look for knownURLs in the root.
websiteURLRoot,
}
// Look for knownURLs in current subdirectory, such as 'example.com/blog/'.
websiteURL, _ = url.AbsoluteURL(websiteURL, "./")
if websiteURL != websiteURLRoot {
baseURLs = append(baseURLs, websiteURL)
} }
for _, baseURL := range baseURLs {
for knownURL, kind := range knownURLs { for knownURL, kind := range knownURLs {
fullURL, err := url.AbsoluteURL(websiteURL, knownURL) fullURL, err := url.AbsoluteURL(baseURL, knownURL)
if err != nil { if err != nil {
continue continue
} }
clt := client.NewClientWithConfig(fullURL, config.Opts) clt := client.NewClientWithConfig(fullURL, config.Opts)
clt.WithCredentials(username, password) clt.WithCredentials(username, password)
clt.WithUserAgent(userAgent) clt.WithUserAgent(userAgent)
@ -186,6 +192,7 @@ func tryWellKnownUrls(websiteURL, userAgent, cookie, username, password string)
} }
} }
} }
}
return subscriptions, nil return subscriptions, nil
} }

View File

@ -25,6 +25,8 @@ func TestAbsoluteURL(t *testing.T) {
scenarios := [][]string{ scenarios := [][]string{
{"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"}, {"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
{"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"}, {"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
{"https://example.org/", "https://example.org/path", "./"},
{"https://example.org/folder/", "https://example.org/folder/", "./"},
{"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"}, {"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
{"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"}, {"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
{"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"}, {"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},