Remove unused api and realtime

This commit is contained in:
2024-06-18 18:20:16 +02:00
parent e0635c3bc9
commit d94b581d41
4 changed files with 146 additions and 151 deletions

View File

@@ -1,75 +1,75 @@
package main package main
import ( // import (
"bytes" // "bytes"
"context" // "context"
"encoding/json" // "encoding/json"
"fmt" // "fmt"
"io" // "io"
"log" // "log"
"net/http" // "net/http"
) // )
type APIError struct { // type APIError struct {
Code int `json:"code"` // Code int `json:"code"`
Message string `json:"message"` // Message string `json:"message"`
Data APIErrorData `json:"data"` // Data APIErrorData `json:"data"`
} // }
type APIErrorData struct { // type APIErrorData struct {
Link APIErrorLink `json:"link"` // Link APIErrorLink `json:"link"`
} // }
type APIErrorLink struct { // type APIErrorLink struct {
Code string `json:"code"` // Code string `json:"code"`
Message string `json:"message"` // Message string `json:"message"`
} // }
func SetDownloaded(item PBEvent) (err error) { // func SetDownloaded(item PBEvent) (err error) {
req, err := http.NewRequestWithContext(context.Background(), "PATCH", FULL_URL+"/"+item.Record.Id, nil) // req, err := http.NewRequestWithContext(context.Background(), "PATCH", FULL_URL+"/"+item.Record.Id, nil)
if err != nil { // if err != nil {
log.Printf("Error creating PATCH request: %++v", err) // log.Printf("Error creating PATCH request: %++v", err)
return err // return err
} // }
req.Header.Set("Content-Type", "application/json") // req.Header.Set("Content-Type", "application/json")
partialItem := new(PBEvent) // partialItem := new(PBEvent)
partialItem.Record = item.Record // partialItem.Record = item.Record
partialItem.Record.Downloaded = true // partialItem.Record.Downloaded = true
body, err := json.Marshal(partialItem.Record) // body, err := json.Marshal(partialItem.Record)
if err != nil { // if err != nil {
log.Printf("Error marshalling subscription body: %++v", err) // log.Printf("Error marshalling subscription body: %++v", err)
return err // return err
} // }
req.Body = io.NopCloser(bytes.NewReader(body)) // req.Body = io.NopCloser(bytes.NewReader(body))
client := http.Client{} // client := http.Client{}
res, err := client.Do(req) // res, err := client.Do(req)
if err != nil { // if err != nil {
log.Printf("Error sending PATCH request: %++v", err) // log.Printf("Error sending PATCH request: %++v", err)
return err // return err
} // }
defer res.Body.Close() // defer res.Body.Close()
if res.StatusCode != http.StatusOK { // if res.StatusCode != http.StatusOK {
log.Printf("Non-OK HTTP status: %d", res.StatusCode) // log.Printf("Non-OK HTTP status: %d", res.StatusCode)
body, err = io.ReadAll(res.Body) // body, err = io.ReadAll(res.Body)
if err != nil { // if err != nil {
log.Printf("Error reading response body: %++v", err) // log.Printf("Error reading response body: %++v", err)
return err // return err
} // }
var data APIError // var data APIError
err = json.Unmarshal(body, &data) // err = json.Unmarshal(body, &data)
if err != nil { // if err != nil {
log.Printf("Error unmarshaling JSON: %++v", err) // log.Printf("Error unmarshaling JSON: %++v", err)
return err // return err
} // }
log.Printf("API error: %++v", data) // log.Printf("API error: %++v", data)
return fmt.Errorf("Non-OK HTTP status, err: %++v", data) // return fmt.Errorf("Non-OK HTTP status, err: %++v", data)
} // }
return nil // return nil
} // }

View File

@@ -20,16 +20,16 @@ var dl = ytdlp.New().
RecodeVideo("mp4"). RecodeVideo("mp4").
ConcurrentFragments(4) ConcurrentFragments(4)
func Download(event PBEvent, status chan error) { // func Download(event PBEvent, status chan error) {
_, err := dl.Run(context.TODO(), event.Record.Link) // _, err := dl.Run(context.TODO(), event.Record.Link)
if err != nil { // if err != nil {
status <- err // status <- err
return // return
} // }
log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link) // log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
SetDownloaded(event) // SetDownloaded(event)
} // }
func DownloadURL(url string, status chan error) { func DownloadURL(url string, status chan error) {
log.Printf("Downloading %s", url) log.Printf("Downloading %s", url)

View File

@@ -5,16 +5,11 @@ import (
"time" "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 = "ws://youtube-download-ws-server.site.quack-lab.dev/ws"
const WEBSOCKET_SERVER_ALT = "ws://localhost:8080/ws" const WEBSOCKET_SERVER_ALT = "ws://localhost:8080/ws"
func main() { func main() {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
log.Println(FULL_URL)
// res, err := http.Get(FULL_URL) // res, err := http.Get(FULL_URL)
// if err != nil { // if err != nil {

View File

@@ -1,89 +1,89 @@
package main package main
import ( // import (
"bytes" // "bytes"
"encoding/json" // "encoding/json"
"log" // "log"
"net/http" // "net/http"
"github.com/r3labs/sse" // "github.com/r3labs/sse"
) // )
type RealtimeListener struct { // type RealtimeListener struct {
Url string // Url string
Collections []string // Collections []string
Create chan PBEvent // Create chan PBEvent
Update chan PBEvent // Update chan PBEvent
Delete chan PBEvent // Delete chan PBEvent
client *sse.Client // client *sse.Client
} // }
type Subscription struct { // type Subscription struct {
ClientId string `json:"clientId"` // ClientId string `json:"clientId"`
Subscriptions []string `json:"subscriptions"` // Subscriptions []string `json:"subscriptions"`
} // }
func (listener RealtimeListener) handlePbEvent(msg *sse.Event) { // func (listener RealtimeListener) handlePbEvent(msg *sse.Event) {
pbEvent := new(PBEvent) // pbEvent := new(PBEvent)
err := json.Unmarshal(msg.Data, &pbEvent) // err := json.Unmarshal(msg.Data, &pbEvent)
if err != nil { // if err != nil {
log.Printf("Error unmarshalling event: %v\n", err) // log.Printf("Error unmarshalling event: %v\n", err)
return // return
} // }
log.Printf("Received event: %++v", pbEvent) // log.Printf("Received event: %++v", pbEvent)
if pbEvent.ClientId != "" { // if pbEvent.ClientId != "" {
listener.doSubscribe(pbEvent.ClientId) // listener.doSubscribe(pbEvent.ClientId)
} // }
if pbEvent.Action != "" { // if pbEvent.Action != "" {
go listener.shipEvent(*pbEvent) // go listener.shipEvent(*pbEvent)
} // }
} // }
func (listener RealtimeListener) shipEvent(event PBEvent) { // func (listener RealtimeListener) shipEvent(event PBEvent) {
switch event.Action { // switch event.Action {
case "create": // case "create":
listener.Create <- event // listener.Create <- event
case "update": // case "update":
listener.Update <- event // listener.Update <- event
case "delete": // case "delete":
listener.Delete <- event // listener.Delete <- event
default: // default:
log.Printf("Unknown action: %v\n", event.Action) // log.Printf("Unknown action: %v\n", event.Action)
} // }
} // }
func (listener RealtimeListener) doSubscribe(clientId string) { // func (listener RealtimeListener) doSubscribe(clientId string) {
subscription := Subscription{ // subscription := Subscription{
ClientId: clientId, // ClientId: clientId,
Subscriptions: listener.Collections, // Subscriptions: listener.Collections,
} // }
log.Printf("Subscribing client: %v to %++v", clientId, subscription) // log.Printf("Subscribing client: %v to %++v", clientId, subscription)
body, err := json.Marshal(subscription) // body, err := json.Marshal(subscription)
if err != nil { // if err != nil {
log.Printf("Error marshalling subscription body: %v\n", err) // log.Printf("Error marshalling subscription body: %v\n", err)
return // return
} // }
resp, err := http.Post(POCKETBASE_REALTIME, "application/json", bytes.NewBuffer(body)) // resp, err := http.Post(POCKETBASE_REALTIME, "application/json", bytes.NewBuffer(body))
if err != nil { // if err != nil {
log.Printf("Error posting subscription: %v\n", err) // log.Printf("Error posting subscription: %v\n", err)
return // return
} // }
defer resp.Body.Close() // defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent { // if resp.StatusCode != http.StatusNoContent {
log.Printf("Subscription request failed with status: %v\n", resp.Status) // log.Printf("Subscription request failed with status: %v\n", resp.Status)
} // }
} // }
func (listener *RealtimeListener) initialize() { // func (listener *RealtimeListener) initialize() {
listener.Update = make(chan PBEvent, 32) // listener.Update = make(chan PBEvent, 32)
listener.Create = make(chan PBEvent, 32) // listener.Create = make(chan PBEvent, 32)
listener.Delete = make(chan PBEvent, 32) // listener.Delete = make(chan PBEvent, 32)
log.Print("Initialized") // log.Print("Initialized")
listener.client = sse.NewClient(listener.Url) // listener.client = sse.NewClient(listener.Url)
go listener.client.Subscribe("", listener.handlePbEvent) // go listener.client.Subscribe("", listener.handlePbEvent)
} // }