From 92dddc7ebef0188589300a79d94d673b7b1a2014 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 14 Aug 2024 14:40:31 +0300 Subject: [PATCH] cmd: testing commands Signed-off-by: HeshamTB --- cmd/googleurltest/main.go | 57 ++++++++++++++++++++++++++++++++++++++ cmd/watchfile/test | 10 +++++++ cmd/watchfile/watchfile.go | 35 +++++++++++++++++++++++ cmd/ytdlpurldesc/main.go | 32 +++++++++++++++++++++ cmd/ytfeed/main.go | 57 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 cmd/googleurltest/main.go create mode 100644 cmd/watchfile/test create mode 100644 cmd/watchfile/watchfile.go create mode 100644 cmd/ytdlpurldesc/main.go create mode 100644 cmd/ytfeed/main.go diff --git a/cmd/googleurltest/main.go b/cmd/googleurltest/main.go new file mode 100644 index 0000000..adb596f --- /dev/null +++ b/cmd/googleurltest/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "net/http" + "net/url" + "os" + "strconv" + "time" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println(os.Args[0], " ") + os.Exit(1) + } + + url, err := url.Parse(os.Args[1]) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + fmt.Printf("url.Query(): %+v\n", url.Query()) + + resp, err := http.Get(url.String()) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + if resp.StatusCode != 200 { + fmt.Printf("http: got %d\n", resp.StatusCode) + } + + if url != resp.Request.URL { + fmt.Printf("resp.Request.URL: %+v\n", resp.Request.URL) + fmt.Printf("resp.Request.URL.Query(): %+v\n", resp.Request.URL.Query()) + } + + time_s := url.Query().Get("expire") + if time_s == "" { + fmt.Println("url: expire key missing") + os.Exit(1) + } + + time_ss, err := strconv.ParseInt(time_s, 10, 64) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + unix := time.Unix(time_ss, 0) + duration := unix.Sub(time.Now()) + fmt.Printf("duration.String(): %v\n", duration.String()) + +} diff --git a/cmd/watchfile/test b/cmd/watchfile/test new file mode 100644 index 0000000..91c4333 --- /dev/null +++ b/cmd/watchfile/test @@ -0,0 +1,10 @@ +test +a +test +awdeafwqe +test +awd +test +test +awdwd +test diff --git a/cmd/watchfile/watchfile.go b/cmd/watchfile/watchfile.go new file mode 100644 index 0000000..b8a0c33 --- /dev/null +++ b/cmd/watchfile/watchfile.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "log" + + "github.com/gohugoio/hugo/watcher/filenotify" +) + +func main() { + + w, err := filenotify.NewEventWatcher() + if err != nil { + log.Fatalln(err.Error()) + } + defer w.Close() + + err = w.Add("test") + if err != nil { + log.Fatalln(err.Error()) + } + + for { + select { + case e, ok:= <- w.Events(): + if !ok { + log.Println("channel closed") + return + } + fmt.Printf("e: %v\n", e) + } + } + + +} diff --git a/cmd/ytdlpurldesc/main.go b/cmd/ytdlpurldesc/main.go new file mode 100644 index 0000000..8d3fa60 --- /dev/null +++ b/cmd/ytdlpurldesc/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "context" + "fmt" + "strings" + + "github.com/lrstanley/go-ytdlp" +) + +func main() { + cmd := ytdlp.New().ExtractAudio().GetURL().GetDescription() + res, err := cmd.Run( + context.Background(), + "https://www.youtube.com/watch?v=JD9IQRlQyh0", + "https://www.youtube.com/watch?v=ZwkNTwWJP5k", + ) + + if err != nil { + fmt.Printf("err: %v\n", err) + return + } + + lines := strings.Split(res.Stdout, "\n") + for _, line := range lines { + fmt.Printf("line: %v\n", line) + if strings.HasPrefix(line, "https://") { + fmt.Println("probably link") + } + } + +} diff --git a/cmd/ytfeed/main.go b/cmd/ytfeed/main.go new file mode 100644 index 0000000..c710fa6 --- /dev/null +++ b/cmd/ytfeed/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "bufio" + "errors" + "flag" + "fmt" + "io" + "net/http" + "os" + + "gitea.hbanafa.com/hesham/yttopodcast/feed" +) + +var CHAN_ID *string = flag.String("id", "", "youtube channel id") + +func main() { + + flag.Parse() + + if *CHAN_ID == "" { + perr("provide channel id with -id \n") + os.Exit(1) + } + + url := fmt.Sprintf(feed.YT_FEED_URL, *CHAN_ID) + perr(url + "\n") + resp, err := http.Get(url) + + if err != nil { + perr(err.Error() + "\n") + os.Exit(1) + } + + if resp.StatusCode != http.StatusOK { + perr("http: endpoint returned %s\n", resp.Status) + os.Exit(1) + } + + sc := bufio.NewReader(resp.Body) + for { + newl, err := sc.ReadString('\n') + if err != nil { + + if errors.Is(err, io.EOF) { + break + } + perr(err.Error() + "\n") + os.Exit(1) + } + fmt.Print(newl) + } +} + +func perr(f string, args... any) { + fmt.Fprintf(os.Stderr, f, args...) +}