Files
youtube-downloader/youtubeWatcher/nsqWriter.go

51 lines
1.1 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
const URL = `https://nsq.site.quack-lab.dev/pub?topic=ytdqueue`
type Item struct {
Link string `json:"link"`
}
func Download(url string) error {
log.Printf("Starting download for URL: %s", url)
req, err := http.NewRequestWithContext(context.Background(), "POST", URL, nil)
if err != nil {
return fmt.Errorf("error creating POST request: %++v", err)
}
req.Header.Set("Content-Type", "application/json")
item := new(Item)
item.Link = url
body, err := json.Marshal(item)
if err != nil {
return fmt.Errorf("error marshalling subscription body: %++v", err)
}
req.Body = io.NopCloser(bytes.NewReader(body))
client := http.Client{}
log.Printf("Sending POST request to %s", URL)
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending POST request: %++v", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("non-OK HTTP status: %d", res.StatusCode)
}
log.Printf("Successfully enqueued %s", url)
return nil
}