From bb7af20f8388a78dbe1a7e06b4a2b0158fd03464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Mon, 17 Jun 2024 21:19:34 +0200 Subject: [PATCH] Add dl to enqueue downloads --- dl/dl.go | 27 ++++++++++++++++++++++++--- dl/sync | 1 + 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 dl/sync diff --git a/dl/dl.go b/dl/dl.go index 11a55c6..9018e9b 100644 --- a/dl/dl.go +++ b/dl/dl.go @@ -4,11 +4,11 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io" "log" "net/http" "os" + "sync" ) const POCKETBASE_URL = `https://pocketbase-scratch.site.quack-lab.dev/api/collections` @@ -20,19 +20,38 @@ type Item struct { Link string `json:"link"` } +type APIError struct { + Code int `json:"code"` + Message string `json:"message"` + Data APIErrorData `json:"data"` +} +type APIErrorData struct { + Link APIErrorLink `json:"link"` +} +type APIErrorLink struct { + Code string `json:"code"` + Message string `json:"message"` +} + +var wg sync.WaitGroup + func main() { log.SetFlags(log.Lmicroseconds) for _, url := range os.Args[1:] { log.Printf("Downloading %s", url) + wg.Add(1) go Download(url) } + wg.Wait() } func Download(url string) { + defer wg.Done() + req, err := http.NewRequestWithContext(context.Background(), "POST", FULL_URL, nil) if err != nil { - log.Printf("Error creating PATCH request: %++v", err) + log.Printf("Error creating POST request: %++v", err) return } req.Header.Set("Content-Type", "application/json") @@ -50,7 +69,7 @@ func Download(url string) { client := http.Client{} res, err := client.Do(req) if err != nil { - log.Printf("Error sending PATCH request: %++v", err) + log.Printf("Error sending POST request: %++v", err) return } defer res.Body.Close() @@ -72,5 +91,7 @@ func Download(url string) { log.Printf("API error: %++v", data) return + } else { + log.Printf("Enqueued %s", url) } } diff --git a/dl/sync b/dl/sync new file mode 100644 index 0000000..42b139b --- /dev/null +++ b/dl/sync @@ -0,0 +1 @@ +main.exe,"C:\Program Files\Git\usr\bin\dl.exe" \ No newline at end of file