Add dl to enqueue downloads

This commit is contained in:
2024-06-17 21:19:34 +02:00
parent 6e5dc31856
commit bb7af20f83
2 changed files with 25 additions and 3 deletions

View File

@@ -4,11 +4,11 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
"sync"
) )
const POCKETBASE_URL = `https://pocketbase-scratch.site.quack-lab.dev/api/collections` const POCKETBASE_URL = `https://pocketbase-scratch.site.quack-lab.dev/api/collections`
@@ -20,19 +20,38 @@ type Item struct {
Link string `json:"link"` 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() { func main() {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
for _, url := range os.Args[1:] { for _, url := range os.Args[1:] {
log.Printf("Downloading %s", url) log.Printf("Downloading %s", url)
wg.Add(1)
go Download(url) go Download(url)
} }
wg.Wait()
} }
func Download(url string) { func Download(url string) {
defer wg.Done()
req, err := http.NewRequestWithContext(context.Background(), "POST", FULL_URL, nil) req, err := http.NewRequestWithContext(context.Background(), "POST", FULL_URL, nil)
if err != nil { if err != nil {
log.Printf("Error creating PATCH request: %++v", err) log.Printf("Error creating POST request: %++v", err)
return return
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@@ -50,7 +69,7 @@ func Download(url string) {
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 POST request: %++v", err)
return return
} }
defer res.Body.Close() defer res.Body.Close()
@@ -72,5 +91,7 @@ func Download(url string) {
log.Printf("API error: %++v", data) log.Printf("API error: %++v", data)
return return
} else {
log.Printf("Enqueued %s", url)
} }
} }

1
dl/sync Normal file
View File

@@ -0,0 +1 @@
main.exe,"C:\Program Files\Git\usr\bin\dl.exe"