71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"log"
 | 
						|
	"time"
 | 
						|
)
 | 
						|
 | 
						|
const POCKETBASE_URL = `https://pocketbase-scratch.site.quack-lab.dev/api/collections`
 | 
						|
const POCKETBASE_REALTIME = `https://pocketbase-scratch.site.quack-lab.dev/api/realtime`
 | 
						|
const COLLECTION_NAME = "youtubedownload"
 | 
						|
const FULL_URL = POCKETBASE_URL + "/" + COLLECTION_NAME + "/records"
 | 
						|
const WEBSOCKET_SERVER = "ws://youtube-download-ws-server.site.quack-lab.dev/ws"
 | 
						|
const WEBSOCKET_SERVER_ALT = "ws://localhost:8080/ws"
 | 
						|
 | 
						|
func main() {
 | 
						|
	log.SetFlags(log.Lmicroseconds)
 | 
						|
	log.Println(FULL_URL)
 | 
						|
 | 
						|
	// res, err := http.Get(FULL_URL)
 | 
						|
	// if err != nil {
 | 
						|
	// 	log.Fatal(err)
 | 
						|
	// }
 | 
						|
	// defer res.Body.Close()
 | 
						|
	// body, err := io.ReadAll(res.Body)
 | 
						|
	// if err != nil {
 | 
						|
	// 	log.Printf("Error reading response body: %+v\n", err)
 | 
						|
	// 	return
 | 
						|
	// }
 | 
						|
	// if res.StatusCode != http.StatusOK {
 | 
						|
	// 	log.Printf("Non-OK HTTP status: %d\nResponse body: %s\n", res.StatusCode, body)
 | 
						|
	// 	return
 | 
						|
	// }
 | 
						|
 | 
						|
	// var data APIResponse
 | 
						|
	// err = json.Unmarshal(body, &data)
 | 
						|
	// if err != nil {
 | 
						|
	// 	log.Printf("Error unmarshaling JSON: %+v\n", err)
 | 
						|
	// 	return
 | 
						|
	// }
 | 
						|
	// log.Printf("Data: %+v\n", data)
 | 
						|
 | 
						|
	// listener := new(RealtimeListener)
 | 
						|
	// listener.Url = POCKETBASE_REALTIME
 | 
						|
	// listener.Collections = []string{COLLECTION_NAME}
 | 
						|
	// listener.initialize()
 | 
						|
 | 
						|
	ws := new(WSConnection)
 | 
						|
	ws.url = WEBSOCKET_SERVER
 | 
						|
	ws.Open()
 | 
						|
 | 
						|
	status := make(chan error, 16)
 | 
						|
	for {
 | 
						|
		select {
 | 
						|
		case event := <-ws.ReadChan:
 | 
						|
			eventCopy := event
 | 
						|
			log.Printf("New event: %+v", eventCopy)
 | 
						|
			go func() {
 | 
						|
				// Download(eventCopy, status)
 | 
						|
				DownloadURL(eventCopy, status)
 | 
						|
				// go DownloadNative(event, status)
 | 
						|
				for status := range status {
 | 
						|
					log.Printf("Status: %s\n", status)
 | 
						|
				}
 | 
						|
			}()
 | 
						|
		case <-time.After(1 * time.Minute):
 | 
						|
			// Perform some action or simply continue to avoid deadlock
 | 
						|
			log.Println("Consumer is alive, but has no new events.")
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |