Files
home/homegrown/yt-email/main.go

77 lines
1.5 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"text/template"
"time"
"github.com/joho/godotenv"
)
type channel struct {
Id string `json:"id"`
Name string `json:"name"`
Kind string `json:"kind"`
}
type videos struct {
Id string
Name string
}
type videourl struct {
Id string
Part string
Order string
Date string
Key string
}
func main() {
channellist := "https://raw.githubusercontent.com/bvanroll/home/refs/heads/master/static/yters.json"
err := godotenv.Load()
if err != nil {
}
apikey := os.Getenv("APIKEY")
resp, err := http.Get(channellist)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
output := []channel{}
json.Unmarshal(body, &output)
week, _ := time.ParseDuration("168h")
date := time.Now().Add(-week)
//bro wtf is up with go here man this is ass :D
datestr := date.Format("2006-01-02T15:04:05Z")
videosstr := "https://www.googleapis.com/youtube/v3/search/?channelId={{.Id}}&part={{.Part}},id&order={{.Order}}&publishedAfter={{.Date}}&key={{.Key}}"
templ, err := template.New("test").Parse(videosstr)
if err != nil {
fmt.Println(err)
return
}
for _, val := range output {
temp := videourl{val.Id, "snippet", "date", datestr, apikey}
var output bytes.Buffer
templ.Execute(&output, temp)
resp, err := http.Get(output.String())
if err != nil {
fmt.Println(err)
continue
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
continue
}
if resp.StatusCode != 200 {
fmt.Println(resp)
} else {
fmt.Println(string(body))
}
}
}