commit a2085625aa715f204fda824899abadacef963ffb Author: balki <3070606-balki@users.noreply.gitlab.com> Date: Tue Apr 26 09:55:59 2022 -0400 intial state diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..ca39644 --- /dev/null +++ b/app/app.go @@ -0,0 +1,20 @@ +package app + +import ( + "fmt" + "log" + + "go.balki.me/tss/config" +) + +func Run(configPath string) { + cfg, err := config.Parse(configPath) + if err != nil { + log.Fatal(err) + } + + for _, feed := range c.Feeds { + log.Println("Processing feed", feed.Name) + } + fmt.Println(configPath) +} diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..6608ab1 --- /dev/null +++ b/app/config.go @@ -0,0 +1,34 @@ +package config + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Feeds []struct { + Name string `yaml:"name"` + Channel string `yaml:"channel"` + Rhash string `yaml:"rhash"` + Url string `yaml:"url"` + Cron string `yaml:"cron"` + Proxy string `yaml:"proxy"` + } `yaml:"feeds"` +} + +func Parse(configPath string) (*Config, error) { + cfg, err := os.ReadFile("tss.yaml") + if err != nil { + return nil, err + } + + c := Config{} + err = yaml.Unmarshal(cfg, &c) + + if err != nil { + return nil, err + } + + return &c, nil +} diff --git a/exp/cron/cron.go b/exp/cron/cron.go new file mode 100644 index 0000000..f972e24 --- /dev/null +++ b/exp/cron/cron.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "log" + "time" + + "github.com/robfig/cron/v3" +) + +func main() { + fmt.Println("vim-go") + + s, err := cron.ParseStandard("@daily") + if err != nil { + log.Fatal(err) + } + + fmt.Println(s.Next(time.Now())) +} diff --git a/exp/exec/exec.go b/exp/exec/exec.go new file mode 100644 index 0000000..15c859a --- /dev/null +++ b/exp/exec/exec.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "os/exec" +) + +func main() { + fmt.Println("vim-go") + c := exec.Command("./script.sh") + op, err := c.Output() + if err != nil { + fmt.Println(err) + exitErr := err.(*exec.ExitError) + fmt.Printf("hello %s\n", exitErr.Stderr) + } else { + fmt.Printf("%s", op) + } +} diff --git a/exp/xml/xml.go b/exp/xml/xml.go new file mode 100644 index 0000000..3c8d252 --- /dev/null +++ b/exp/xml/xml.go @@ -0,0 +1,88 @@ +package main + +import ( + "encoding/csv" + "encoding/xml" + "fmt" + "log" + "os" + "strings" + "time" +) + +/* + XMLName xml.Name `xml:"person"` + FirstName string `xml:"firstname"` + Parents interface{} `xml:"parent"` +*/ + +type Item struct { + Title string `xml:"title"` + Link string `xml:"link"` + Author string `xml:"author"` + Guid string `xml:"guid"` + Description string `xml:"description"` + Content string `xml:",innerxml"` +} + +func main() { + fmt.Println("hello go") + data, err := os.ReadFile("./ounapuu.xml") + if err != nil { + log.Fatal(err) + } + + v := struct { + Items []Item `xml:"channel>item"` + }{} + + err = xml.Unmarshal(data, &v) + if err != nil { + log.Fatal(err) + } + + f, err := os.OpenFile("./data.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + + defer f.Close() + + if err != nil { + log.Fatal(err) + } + csvw := csv.NewWriter(f) + csvw.Write([]string{ + "date", + "link", + "info", + }) + + for index, item := range v.Items { + fmt.Println(index, item.Title) + //fmt.Println(item.Content) + c := item.Content + csvw.Write([]string{ + time.Now().String(), + item.Link, + strings.Replace(c, "\n", " ", -1), + }) + + } + +} +func foo() { + x := ` + + slkdfjdslk + +` + s := struct { + Bar struct { + Content string `xml:",innerxml"` + } `xml:"bar"` + }{} + + err := xml.Unmarshal([]byte(x), &s) + if err != nil { + log.Fatal(err) + } + fmt.Println(s.Bar.Content) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..46f4248 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module go.balki.me/tss + +go 1.17 + +require ( + github.com/robfig/cron/v3 v3.0.1 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2149a0c --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6f23cde --- /dev/null +++ b/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "log" + "net/url" + "os" +) + +func main() { + var err error + + for _, feed := range c.Feeds { + log.Println("Processing feed", feed.Name) + data, err := download(feed.Url) + if err != nil { + log.Fatal(err) + } + + links, err := parseFeed(data) + if err != nil { + log.Fatal(err) + } + + for _, link := range links { + if alreadySent(link) { + continue + } + err := sendTelegram(link, feed.Channel, feed.Rhash) + if err != nil { + log.Fatal(err) + } + } + } +} + +func download(url string) ([]byte, error) { + log.Println("url", url) + return os.ReadFile("ounapuu.xml") +} + +func parseFeed(data []byte) ([]string, error) { + return []string{"https://blog.link"}, nil +} + +func alreadySent(link string) bool { + return false +} + +func sendTelegram(link string, channel string, rhash string) error { + log.Println("link", link, "channel", channel, "rhash", rhash) + rhash = "ae86262f2de32f" + log.Println("ivurl", genIVLink(link, rhash)) + + return nil +} + +func genIVLink(link, rhash string) string { + query := url.Values{} + query.Set("url", link) + query.Set("rhash", rhash) + u := url.URL{ + Scheme: "https", + Host: "t.me", + Path: "iv", + RawQuery: vs.Encode(), + } + return u.String() +}