From 6e5dc31856090c401481b5784b20f110d9bb273b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Mon, 17 Jun 2024 21:11:23 +0200 Subject: [PATCH] Add dl module --- dl/dl.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ dl/go.mod | 3 +++ 2 files changed, 79 insertions(+) create mode 100644 dl/dl.go create mode 100644 dl/go.mod diff --git a/dl/dl.go b/dl/dl.go new file mode 100644 index 0000000..11a55c6 --- /dev/null +++ b/dl/dl.go @@ -0,0 +1,76 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "os" +) + +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" + +type Item struct { + Link string `json:"link"` +} + +func main() { + log.SetFlags(log.Lmicroseconds) + + for _, url := range os.Args[1:] { + log.Printf("Downloading %s", url) + go Download(url) + } +} + +func Download(url string) { + req, err := http.NewRequestWithContext(context.Background(), "POST", FULL_URL, nil) + if err != nil { + log.Printf("Error creating PATCH request: %++v", err) + return + } + req.Header.Set("Content-Type", "application/json") + + item := new(Item) + item.Link = url + + body, err := json.Marshal(item) + if err != nil { + log.Printf("Error marshalling subscription body: %++v", err) + return + } + 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 + } + defer res.Body.Close() + + 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 + } + var data APIError + err = json.Unmarshal(body, &data) + if err != nil { + log.Printf("Error unmarshaling JSON: %++v", err) + return + } + + log.Printf("API error: %++v", data) + return + } +} diff --git a/dl/go.mod b/dl/go.mod new file mode 100644 index 0000000..9a9693e --- /dev/null +++ b/dl/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.22.4