Remove unused api and realtime
This commit is contained in:
@@ -1,75 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
// import (
|
||||
// "bytes"
|
||||
// "context"
|
||||
// "encoding/json"
|
||||
// "fmt"
|
||||
// "io"
|
||||
// "log"
|
||||
// "net/http"
|
||||
// )
|
||||
|
||||
type APIError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data APIErrorData `json:"data"`
|
||||
}
|
||||
// type APIError struct {
|
||||
// Code int `json:"code"`
|
||||
// Message string `json:"message"`
|
||||
// Data APIErrorData `json:"data"`
|
||||
// }
|
||||
|
||||
type APIErrorData struct {
|
||||
Link APIErrorLink `json:"link"`
|
||||
}
|
||||
// type APIErrorData struct {
|
||||
// Link APIErrorLink `json:"link"`
|
||||
// }
|
||||
|
||||
type APIErrorLink struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
// type APIErrorLink struct {
|
||||
// Code string `json:"code"`
|
||||
// Message string `json:"message"`
|
||||
// }
|
||||
|
||||
func SetDownloaded(item PBEvent) (err error) {
|
||||
req, err := http.NewRequestWithContext(context.Background(), "PATCH", FULL_URL+"/"+item.Record.Id, nil)
|
||||
if err != nil {
|
||||
log.Printf("Error creating PATCH request: %++v", err)
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// func SetDownloaded(item PBEvent) (err error) {
|
||||
// req, err := http.NewRequestWithContext(context.Background(), "PATCH", FULL_URL+"/"+item.Record.Id, nil)
|
||||
// if err != nil {
|
||||
// log.Printf("Error creating PATCH request: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
partialItem := new(PBEvent)
|
||||
partialItem.Record = item.Record
|
||||
partialItem.Record.Downloaded = true
|
||||
// partialItem := new(PBEvent)
|
||||
// partialItem.Record = item.Record
|
||||
// partialItem.Record.Downloaded = true
|
||||
|
||||
body, err := json.Marshal(partialItem.Record)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling subscription body: %++v", err)
|
||||
return err
|
||||
}
|
||||
req.Body = io.NopCloser(bytes.NewReader(body))
|
||||
// body, err := json.Marshal(partialItem.Record)
|
||||
// if err != nil {
|
||||
// log.Printf("Error marshalling subscription body: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// req.Body = io.NopCloser(bytes.NewReader(body))
|
||||
|
||||
client := http.Client{}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("Error sending PATCH request: %++v", err)
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
// client := http.Client{}
|
||||
// res, err := client.Do(req)
|
||||
// if err != nil {
|
||||
// log.Printf("Error sending PATCH request: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
log.Printf("Non-OK HTTP status: %d", res.StatusCode)
|
||||
// if res.StatusCode != http.StatusOK {
|
||||
// log.Printf("Non-OK HTTP status: %d", res.StatusCode)
|
||||
|
||||
body, err = io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading response body: %++v", err)
|
||||
return err
|
||||
}
|
||||
var data APIError
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
log.Printf("Error unmarshaling JSON: %++v", err)
|
||||
return err
|
||||
}
|
||||
// body, err = io.ReadAll(res.Body)
|
||||
// if err != nil {
|
||||
// log.Printf("Error reading response body: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
// var data APIError
|
||||
// err = json.Unmarshal(body, &data)
|
||||
// if err != nil {
|
||||
// log.Printf("Error unmarshaling JSON: %++v", err)
|
||||
// return err
|
||||
// }
|
||||
|
||||
log.Printf("API error: %++v", data)
|
||||
return fmt.Errorf("Non-OK HTTP status, err: %++v", data)
|
||||
}
|
||||
// log.Printf("API error: %++v", data)
|
||||
// return fmt.Errorf("Non-OK HTTP status, err: %++v", data)
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
@@ -20,16 +20,16 @@ var dl = ytdlp.New().
|
||||
RecodeVideo("mp4").
|
||||
ConcurrentFragments(4)
|
||||
|
||||
func Download(event PBEvent, status chan error) {
|
||||
_, err := dl.Run(context.TODO(), event.Record.Link)
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
// func Download(event PBEvent, status chan error) {
|
||||
// _, err := dl.Run(context.TODO(), event.Record.Link)
|
||||
// if err != nil {
|
||||
// status <- err
|
||||
// return
|
||||
// }
|
||||
|
||||
log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
|
||||
SetDownloaded(event)
|
||||
}
|
||||
// log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
|
||||
// SetDownloaded(event)
|
||||
// }
|
||||
|
||||
func DownloadURL(url string, status chan error) {
|
||||
log.Printf("Downloading %s", url)
|
||||
|
@@ -5,16 +5,11 @@ import (
|
||||
"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 {
|
||||
|
@@ -1,89 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
// import (
|
||||
// "bytes"
|
||||
// "encoding/json"
|
||||
// "log"
|
||||
// "net/http"
|
||||
|
||||
"github.com/r3labs/sse"
|
||||
)
|
||||
// "github.com/r3labs/sse"
|
||||
// )
|
||||
|
||||
type RealtimeListener struct {
|
||||
Url string
|
||||
Collections []string
|
||||
Create chan PBEvent
|
||||
Update chan PBEvent
|
||||
Delete chan PBEvent
|
||||
client *sse.Client
|
||||
}
|
||||
// type RealtimeListener struct {
|
||||
// Url string
|
||||
// Collections []string
|
||||
// Create chan PBEvent
|
||||
// Update chan PBEvent
|
||||
// Delete chan PBEvent
|
||||
// client *sse.Client
|
||||
// }
|
||||
|
||||
type Subscription struct {
|
||||
ClientId string `json:"clientId"`
|
||||
Subscriptions []string `json:"subscriptions"`
|
||||
}
|
||||
// type Subscription struct {
|
||||
// ClientId string `json:"clientId"`
|
||||
// Subscriptions []string `json:"subscriptions"`
|
||||
// }
|
||||
|
||||
func (listener RealtimeListener) handlePbEvent(msg *sse.Event) {
|
||||
pbEvent := new(PBEvent)
|
||||
err := json.Unmarshal(msg.Data, &pbEvent)
|
||||
if err != nil {
|
||||
log.Printf("Error unmarshalling event: %v\n", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Received event: %++v", pbEvent)
|
||||
// func (listener RealtimeListener) handlePbEvent(msg *sse.Event) {
|
||||
// pbEvent := new(PBEvent)
|
||||
// err := json.Unmarshal(msg.Data, &pbEvent)
|
||||
// if err != nil {
|
||||
// log.Printf("Error unmarshalling event: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
// log.Printf("Received event: %++v", pbEvent)
|
||||
|
||||
if pbEvent.ClientId != "" {
|
||||
listener.doSubscribe(pbEvent.ClientId)
|
||||
}
|
||||
// if pbEvent.ClientId != "" {
|
||||
// listener.doSubscribe(pbEvent.ClientId)
|
||||
// }
|
||||
|
||||
if pbEvent.Action != "" {
|
||||
go listener.shipEvent(*pbEvent)
|
||||
}
|
||||
}
|
||||
// if pbEvent.Action != "" {
|
||||
// go listener.shipEvent(*pbEvent)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (listener RealtimeListener) shipEvent(event PBEvent) {
|
||||
switch event.Action {
|
||||
case "create":
|
||||
listener.Create <- event
|
||||
case "update":
|
||||
listener.Update <- event
|
||||
case "delete":
|
||||
listener.Delete <- event
|
||||
default:
|
||||
log.Printf("Unknown action: %v\n", event.Action)
|
||||
}
|
||||
}
|
||||
// func (listener RealtimeListener) shipEvent(event PBEvent) {
|
||||
// switch event.Action {
|
||||
// case "create":
|
||||
// listener.Create <- event
|
||||
// case "update":
|
||||
// listener.Update <- event
|
||||
// case "delete":
|
||||
// listener.Delete <- event
|
||||
// default:
|
||||
// log.Printf("Unknown action: %v\n", event.Action)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (listener RealtimeListener) doSubscribe(clientId string) {
|
||||
subscription := Subscription{
|
||||
ClientId: clientId,
|
||||
Subscriptions: listener.Collections,
|
||||
}
|
||||
log.Printf("Subscribing client: %v to %++v", clientId, subscription)
|
||||
// func (listener RealtimeListener) doSubscribe(clientId string) {
|
||||
// subscription := Subscription{
|
||||
// ClientId: clientId,
|
||||
// Subscriptions: listener.Collections,
|
||||
// }
|
||||
// log.Printf("Subscribing client: %v to %++v", clientId, subscription)
|
||||
|
||||
body, err := json.Marshal(subscription)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling subscription body: %v\n", err)
|
||||
return
|
||||
}
|
||||
// body, err := json.Marshal(subscription)
|
||||
// if err != nil {
|
||||
// log.Printf("Error marshalling subscription body: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
resp, err := http.Post(POCKETBASE_REALTIME, "application/json", bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
log.Printf("Error posting subscription: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// resp, err := http.Post(POCKETBASE_REALTIME, "application/json", bytes.NewBuffer(body))
|
||||
// if err != nil {
|
||||
// log.Printf("Error posting subscription: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
// defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
log.Printf("Subscription request failed with status: %v\n", resp.Status)
|
||||
}
|
||||
}
|
||||
// if resp.StatusCode != http.StatusNoContent {
|
||||
// log.Printf("Subscription request failed with status: %v\n", resp.Status)
|
||||
// }
|
||||
// }
|
||||
|
||||
func (listener *RealtimeListener) initialize() {
|
||||
listener.Update = make(chan PBEvent, 32)
|
||||
listener.Create = make(chan PBEvent, 32)
|
||||
listener.Delete = make(chan PBEvent, 32)
|
||||
log.Print("Initialized")
|
||||
listener.client = sse.NewClient(listener.Url)
|
||||
go listener.client.Subscribe("", listener.handlePbEvent)
|
||||
}
|
||||
// func (listener *RealtimeListener) initialize() {
|
||||
// listener.Update = make(chan PBEvent, 32)
|
||||
// listener.Create = make(chan PBEvent, 32)
|
||||
// listener.Delete = make(chan PBEvent, 32)
|
||||
// log.Print("Initialized")
|
||||
// listener.client = sse.NewClient(listener.Url)
|
||||
// go listener.client.Subscribe("", listener.handlePbEvent)
|
||||
// }
|
||||
|
Reference in New Issue
Block a user