Search/video.go

212 lines
5.5 KiB
Go
Raw Normal View History

2024-04-10 20:40:12 +00:00
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"sync"
2024-04-10 20:40:12 +00:00
"time"
)
const retryDuration = 12 * time.Hour // Retry duration for unresponding piped instances
var (
pipedInstances = []string{
"pipedapi.kavin.rocks",
"api.piped.yt",
"pipedapi.moomoo.me",
"pipedapi.darkness.services",
"piped-api.hostux.net",
"pipedapi.syncpundit.io",
"piped-api.cfe.re",
"pipedapi.in.projectsegfau.lt",
"piapi.ggtyler.dev",
"piped-api.codespace.cz",
"pipedapi.coldforge.xyz",
"pipedapi.osphost.fi",
}
disabledInstances = make(map[string]bool)
mu sync.Mutex
)
2024-04-10 20:40:12 +00:00
// VideoResult reflects the structured data for a video result
type VideoResult struct {
Href string
Title string
Date string
Views string
Creator string
Publisher string
Image string
Duration string
}
// VideoAPIResponse matches the structure of the JSON response from the Piped API
type VideoAPIResponse struct {
Items []struct {
URL string `json:"url"`
Title string `json:"title"`
UploaderName string `json:"uploaderName"`
Views int `json:"views"`
Thumbnail string `json:"thumbnail"`
Duration int `json:"duration"`
UploadedDate string `json:"uploadedDate"`
Type string `json:"type"`
} `json:"items"`
}
// Function to format views similarly to the Python code
func formatViews(views int) string {
switch {
case views >= 1_000_000_000:
return fmt.Sprintf("%.1fB views", float64(views)/1_000_000_000)
case views >= 1_000_000:
return fmt.Sprintf("%.1fM views", float64(views)/1_000_000)
2024-04-10 20:40:12 +00:00
case views >= 10_000:
return fmt.Sprintf("%.1fK views", float64(views)/1_000)
case views == 1:
return fmt.Sprintf("%d view", views)
2024-04-10 20:40:12 +00:00
default:
return fmt.Sprintf("%d views", views)
}
}
// formatDuration formats video duration as done in the Python code
func formatDuration(seconds int) string {
if 0 > seconds {
return "Live"
}
2024-04-10 20:40:12 +00:00
hours := seconds / 3600
minutes := (seconds % 3600) / 60
seconds = seconds % 60
2024-04-10 20:40:12 +00:00
if hours > 0 {
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
return fmt.Sprintf("%02d:%02d", minutes, seconds)
}
func init() {
go checkDisabledInstancesPeriodically()
}
func checkDisabledInstancesPeriodically() {
checkAndReactivateInstances() // Initial immediate check
ticker := time.NewTicker(retryDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
checkAndReactivateInstances()
}
2024-04-10 20:40:12 +00:00
}
}
2024-04-10 20:40:12 +00:00
func checkAndReactivateInstances() {
mu.Lock()
defer mu.Unlock()
for instance, isDisabled := range disabledInstances {
if isDisabled {
// Check if the instance is available again
if testInstanceAvailability(instance) {
log.Printf("Instance %s is now available and reactivated.", instance)
delete(disabledInstances, instance)
} else {
log.Printf("Instance %s is still not available.", instance)
}
}
2024-04-10 20:40:12 +00:00
}
}
2024-04-10 20:40:12 +00:00
func testInstanceAvailability(instance string) bool {
resp, err := http.Get(fmt.Sprintf("https://%s/search?q=%s&filter=all", instance, url.QueryEscape("test")))
if err != nil || resp.StatusCode != http.StatusOK {
return false
2024-04-10 20:40:12 +00:00
}
return true
}
2024-04-10 20:40:12 +00:00
func makeHTMLRequest(query string) (*VideoAPIResponse, error) {
var lastError error
mu.Lock()
defer mu.Unlock()
for _, instance := range pipedInstances {
if disabledInstances[instance] {
continue // Skip this instance because it's still disabled
}
url := fmt.Sprintf("https://%s/search?q=%s&filter=all", instance, url.QueryEscape(query))
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)
disabledInstances[instance] = true
lastError = fmt.Errorf("error making request to %s: %w", instance, err)
continue
}
defer resp.Body.Close()
var apiResp VideoAPIResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
lastError = fmt.Errorf("error decoding response from %s: %w", instance, err)
continue
}
return &apiResp, nil
}
return nil, fmt.Errorf("all instances failed, last error: %v", lastError)
2024-04-10 20:40:12 +00:00
}
// handleVideoSearch adapted from the Python `videoResults`, handles video search requests
func handleVideoSearch(w http.ResponseWriter, query, safe, lang string, page int) {
start := time.Now()
// Modify `makeHTMLRequest` to also accept `safe`, `lang`, and `page` parameters if necessary
apiResp, err := makeHTMLRequest(query)
if err != nil {
log.Printf("Error fetching video results: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
var results []VideoResult
for _, item := range apiResp.Items {
if item.Type == "channel" || item.Type == "playlist" {
continue
}
if item.UploadedDate == "" {
item.UploadedDate = "Now"
}
2024-04-10 20:40:12 +00:00
results = append(results, VideoResult{
2024-04-11 09:45:39 +00:00
Href: fmt.Sprintf("https://youtube.com%s", item.URL),
2024-04-10 20:40:12 +00:00
Title: item.Title,
Date: item.UploadedDate,
Views: formatViews(item.Views),
Creator: item.UploaderName,
Publisher: "Piped",
Image: fmt.Sprintf("/img_proxy?url=%s", url.QueryEscape(item.Thumbnail)),
Duration: formatDuration(item.Duration),
})
}
elapsed := time.Since(start)
tmpl, err := template.ParseFiles("templates/videos.html")
if err != nil {
log.Printf("Error parsing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
tmpl.Execute(w, map[string]interface{}{
"Results": results,
"Query": query,
"Fetched": fmt.Sprintf("%.2f seconds", elapsed.Seconds()),
})
}