miniflux/internal/integration/rssbridge/rssbridge.go

85 lines
2.1 KiB
Go
Raw Permalink Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2024-03-24 20:19:03 +01:00
package rssbridge // import "miniflux.app/v2/internal/integration/rssbridge"
2023-10-22 20:10:56 +02:00
import (
"encoding/json"
"fmt"
2024-01-13 23:31:45 +01:00
"log/slog"
2023-10-22 20:10:56 +02:00
"net/http"
"net/url"
2024-01-13 23:31:45 +01:00
"strings"
"time"
2023-10-22 20:10:56 +02:00
)
2024-01-13 23:31:45 +01:00
const defaultClientTimeout = 30 * time.Second
2023-10-22 20:10:56 +02:00
type Bridge struct {
URL string `json:"url"`
BridgeMeta BridgeMeta `json:"bridgeMeta"`
}
type BridgeMeta struct {
Name string `json:"name"`
}
2024-01-13 23:31:45 +01:00
func DetectBridges(rssBridgeURL, websiteURL string) ([]*Bridge, error) {
endpointURL, err := url.Parse(rssBridgeURL)
2023-10-22 20:10:56 +02:00
if err != nil {
2024-01-13 23:31:45 +01:00
return nil, fmt.Errorf("RSS-Bridge: unable to parse bridge URL: %w", err)
2023-10-22 20:10:56 +02:00
}
2024-01-13 23:31:45 +01:00
values := endpointURL.Query()
2023-10-22 20:10:56 +02:00
values.Add("action", "findfeed")
values.Add("format", "atom")
values.Add("url", websiteURL)
2024-01-13 23:31:45 +01:00
endpointURL.RawQuery = values.Encode()
slog.Debug("Detecting RSS bridges", slog.String("url", endpointURL.String()))
request, err := http.NewRequest(http.MethodGet, endpointURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("RSS-Bridge: unable to create request: %w", err)
}
2023-10-22 20:10:56 +02:00
2024-01-13 23:31:45 +01:00
httpClient := &http.Client{Timeout: defaultClientTimeout}
response, err := httpClient.Do(request)
2023-10-22 20:10:56 +02:00
if err != nil {
2024-01-13 23:31:45 +01:00
return nil, fmt.Errorf("RSS-Bridge: unable to execute request: %w", err)
2023-10-22 20:10:56 +02:00
}
defer response.Body.Close()
2024-01-13 23:31:45 +01:00
2023-10-22 20:10:56 +02:00
if response.StatusCode == http.StatusNotFound {
2024-01-13 23:31:45 +01:00
return nil, nil
2023-10-22 20:10:56 +02:00
}
2024-01-13 23:31:45 +01:00
2023-10-22 20:10:56 +02:00
if response.StatusCode > 400 {
return nil, fmt.Errorf("RSS-Bridge: unexpected status code %d", response.StatusCode)
2023-10-22 20:10:56 +02:00
}
2024-01-13 23:31:45 +01:00
var bridgeResponse []*Bridge
2023-10-22 20:10:56 +02:00
if err := json.NewDecoder(response.Body).Decode(&bridgeResponse); err != nil {
return nil, fmt.Errorf("RSS-Bridge: unable to decode bridge response: %w", err)
}
2024-01-13 23:31:45 +01:00
for _, bridge := range bridgeResponse {
slog.Debug("Found RSS bridge",
slog.String("name", bridge.BridgeMeta.Name),
slog.String("url", bridge.URL),
)
if strings.HasPrefix(bridge.URL, "./") {
bridge.URL = rssBridgeURL + bridge.URL[2:]
slog.Debug("Rewrited relative RSS bridge URL",
slog.String("name", bridge.BridgeMeta.Name),
slog.String("url", bridge.URL),
)
}
}
return bridgeResponse, nil
2023-10-22 20:10:56 +02:00
}