74 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"bytes"
 | 
						|
	"context"
 | 
						|
	"encoding/json"
 | 
						|
	"io"
 | 
						|
	"log"
 | 
						|
	"net/http"
 | 
						|
	"os"
 | 
						|
	"sync"
 | 
						|
)
 | 
						|
 | 
						|
const URL = `https://youtube-download-ws-server.site.quack-lab.dev/download`
 | 
						|
 | 
						|
type Item struct {
 | 
						|
	Link string `json:"link"`
 | 
						|
}
 | 
						|
 | 
						|
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", URL, nil)
 | 
						|
	if err != nil {
 | 
						|
		log.Printf("Error creating POST 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 POST 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
 | 
						|
		}
 | 
						|
		return
 | 
						|
	} else {
 | 
						|
		log.Printf("Enqueued %s", url)
 | 
						|
	}
 | 
						|
}
 |