better image proxy

This commit is contained in:
partisan 2024-08-12 08:29:53 +02:00
parent 5e84d68b17
commit e5914a1cd6
2 changed files with 23 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"io"
"net/http"
)
@ -13,13 +14,21 @@ func handleImageProxy(w http.ResponseWriter, r *http.Request) {
return
}
// Fetch the image from the external URL
resp, err := http.Get(imageURL)
// Try to fetch the image from Bing first
bingURL := fmt.Sprintf("https://tse.mm.bing.net/th?q=%s", imageURL)
resp, err := http.Get(bingURL)
if err != nil || resp.StatusCode != http.StatusOK {
// If fetching from Bing fails, attempt to fetch from the original image URL
printWarn("Error fetching image from Bing, trying original URL.")
// Attempt to fetch the image directly
resp, err = http.Get(imageURL)
if err != nil {
printWarn("Error fetching image: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
defer resp.Body.Close()
// Check if the request was successful

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"sync"
@ -15,10 +14,10 @@ const retryDuration = 12 * time.Hour // Retry duration for unresponding piped in
var (
pipedInstances = []string{
"pipedapi.kavin.rocks",
"api.piped.yt",
"pipedapi.moomoo.me",
"pipedapi.darkness.services",
"pipedapi.kavin.rocks",
"piped-api.hostux.net",
"pipedapi.syncpundit.io",
"piped-api.cfe.re",
@ -101,10 +100,10 @@ func checkAndReactivateInstances() {
if isDisabled {
// Check if the instance is available again
if testInstanceAvailability(instance) {
log.Printf("Instance %s is now available and reactivated.", instance)
printInfo("Instance %s is now available and reactivated.", instance)
delete(disabledInstances, instance)
} else {
log.Printf("Instance %s is still not available.", instance)
printInfo("Instance %s is still not available.", instance)
}
}
}
@ -131,7 +130,7 @@ func makeHTMLRequest(query, safe, lang string, page int) (*VideoAPIResponse, err
url := fmt.Sprintf("https://%s/search?q=%s&filter=all&safe=%s&lang=%s&page=%d", instance, url.QueryEscape(query), safe, lang, page)
resp, err := http.Get(url)
if err != nil || resp.StatusCode != http.StatusOK {
log.Printf("Disabling instance %s due to error or status code: %v", instance, err)
printInfo("Disabling instance %s due to error or status code: %v", instance, err)
disabledInstances[instance] = true
lastError = fmt.Errorf("error making request to %s: %w", instance, err)
continue
@ -154,14 +153,14 @@ func handleVideoSearch(w http.ResponseWriter, settings UserSettings, query, safe
results := fetchVideoResults(query, settings.SafeSearch, settings.Language, page)
if len(results) == 0 {
log.Printf("No results from primary search, trying other nodes")
printWarn("No results from primary search, trying other nodes")
results = tryOtherNodesForVideoSearch(query, settings.SafeSearch, settings.Language, page, []string{hostID})
}
elapsed := time.Since(start)
tmpl, err := template.New("videos.html").Funcs(funcs).ParseFiles("templates/videos.html")
if err != nil {
log.Printf("Error parsing template: %v", err)
printErr("Error parsing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
@ -176,7 +175,7 @@ func handleVideoSearch(w http.ResponseWriter, settings UserSettings, query, safe
"Theme": settings.Theme,
})
if err != nil {
log.Printf("Error executing template: %v", err)
printErr("Error executing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
@ -184,7 +183,7 @@ func handleVideoSearch(w http.ResponseWriter, settings UserSettings, query, safe
func fetchVideoResults(query, safe, lang string, page int) []VideoResult {
apiResp, err := makeHTMLRequest(query, safe, lang, page)
if err != nil {
log.Printf("Error fetching video results: %v", err)
printWarn("Error fetching video results: %v", err)
return nil
}