better image proxy
This commit is contained in:
parent
ca15fb7ec5
commit
735819d072
2 changed files with 23 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -13,12 +14,20 @@ func handleImageProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the image from the external URL
|
// Try to fetch the image from Bing first
|
||||||
resp, err := http.Get(imageURL)
|
bingURL := fmt.Sprintf("https://tse.mm.bing.net/th?q=%s", imageURL)
|
||||||
if err != nil {
|
resp, err := http.Get(bingURL)
|
||||||
printWarn("Error fetching image: %v", err)
|
if err != nil || resp.StatusCode != http.StatusOK {
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
// If fetching from Bing fails, attempt to fetch from the original image URL
|
||||||
return
|
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()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|
17
video.go
17
video.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -15,10 +14,10 @@ const retryDuration = 12 * time.Hour // Retry duration for unresponding piped in
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pipedInstances = []string{
|
pipedInstances = []string{
|
||||||
"pipedapi.kavin.rocks",
|
|
||||||
"api.piped.yt",
|
"api.piped.yt",
|
||||||
"pipedapi.moomoo.me",
|
"pipedapi.moomoo.me",
|
||||||
"pipedapi.darkness.services",
|
"pipedapi.darkness.services",
|
||||||
|
"pipedapi.kavin.rocks",
|
||||||
"piped-api.hostux.net",
|
"piped-api.hostux.net",
|
||||||
"pipedapi.syncpundit.io",
|
"pipedapi.syncpundit.io",
|
||||||
"piped-api.cfe.re",
|
"piped-api.cfe.re",
|
||||||
|
@ -101,10 +100,10 @@ func checkAndReactivateInstances() {
|
||||||
if isDisabled {
|
if isDisabled {
|
||||||
// Check if the instance is available again
|
// Check if the instance is available again
|
||||||
if testInstanceAvailability(instance) {
|
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)
|
delete(disabledInstances, instance)
|
||||||
} else {
|
} 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)
|
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)
|
resp, err := http.Get(url)
|
||||||
if err != nil || resp.StatusCode != http.StatusOK {
|
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
|
disabledInstances[instance] = true
|
||||||
lastError = fmt.Errorf("error making request to %s: %w", instance, err)
|
lastError = fmt.Errorf("error making request to %s: %w", instance, err)
|
||||||
continue
|
continue
|
||||||
|
@ -154,14 +153,14 @@ func handleVideoSearch(w http.ResponseWriter, settings UserSettings, query, safe
|
||||||
|
|
||||||
results := fetchVideoResults(query, settings.SafeSearch, settings.Language, page)
|
results := fetchVideoResults(query, settings.SafeSearch, settings.Language, page)
|
||||||
if len(results) == 0 {
|
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})
|
results = tryOtherNodesForVideoSearch(query, settings.SafeSearch, settings.Language, page, []string{hostID})
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
tmpl, err := template.New("videos.html").Funcs(funcs).ParseFiles("templates/videos.html")
|
tmpl, err := template.New("videos.html").Funcs(funcs).ParseFiles("templates/videos.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error parsing template: %v", err)
|
printErr("Error parsing template: %v", err)
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -176,7 +175,7 @@ func handleVideoSearch(w http.ResponseWriter, settings UserSettings, query, safe
|
||||||
"Theme": settings.Theme,
|
"Theme": settings.Theme,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error executing template: %v", err)
|
printErr("Error executing template: %v", err)
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
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 {
|
func fetchVideoResults(query, safe, lang string, page int) []VideoResult {
|
||||||
apiResp, err := makeHTMLRequest(query, safe, lang, page)
|
apiResp, err := makeHTMLRequest(query, safe, lang, page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching video results: %v", err)
|
printWarn("Error fetching video results: %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue