Implement writing urls to nsq for downloading
This commit is contained in:
50
youtubeWatcher/nsqWriter.go
Normal file
50
youtubeWatcher/nsqWriter.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user