intial state

This commit is contained in:
Balakrishnan Balasubramanian 2022-04-26 09:55:59 -04:00
commit a2085625aa
8 changed files with 263 additions and 0 deletions

20
app/app.go Normal file
View File

@ -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)
}

34
app/config.go Normal file
View File

@ -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
}

20
exp/cron/cron.go Normal file
View File

@ -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()))
}

19
exp/exec/exec.go Normal file
View File

@ -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)
}
}

88
exp/xml/xml.go Normal file
View File

@ -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 := `
<foo>
<bar><blue>slkdfjdslk</blue></bar>
</foo>
`
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)
}

8
go.mod Normal file
View File

@ -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
)

6
go.sum Normal file
View File

@ -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=

68
main.go Normal file
View File

@ -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()
}