From 48fe506f8d143a409cd7ce8d24f6673015444754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Fri, 26 Dec 2025 12:38:08 +0500 Subject: [PATCH] Refactoring --- go.mod | 15 +++-- go.sum | 6 ++ internal/config/config.go | 30 +++++++++ internal/models/config.go | 68 ++++++++++++++++++++ internal/models/rss.go | 33 ++++++++++ main.go | 131 +++----------------------------------- 6 files changed, 156 insertions(+), 127 deletions(-) create mode 100644 internal/config/config.go create mode 100644 internal/models/config.go create mode 100644 internal/models/rss.go diff --git a/go.mod b/go.mod index bb68279..522df80 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,19 @@ module ssender -go 1.17 +go 1.25 + +require ( + github.com/SevereCloud/vksdk/v2 v2.13.1 + github.com/boltdb/bolt v1.3.1 + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/jessevdk/go-flags v1.5.0 + gopkg.in/yaml.v2 v2.4.0 +) require ( - github.com/SevereCloud/vksdk/v2 v2.13.1 // indirect - github.com/boltdb/bolt v1.3.1 // indirect - github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect - github.com/jessevdk/go-flags v1.5.0 // indirect github.com/klauspost/compress v1.14.2 // indirect github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect golang.org/x/text v0.3.7 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 791d484..2971020 100644 --- a/go.sum +++ b/go.sum @@ -2,18 +2,22 @@ github.com/SevereCloud/vksdk/v2 v2.13.1 h1:D11NaP275mW01v2hRF0ycDHdJaIyZEvasZV4M github.com/SevereCloud/vksdk/v2 v2.13.1/go.mod h1:UyOgSj/CYt2dByu3Fyf/y1yT1NoahVi4zECvvrbtPU4= github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/klauspost/compress v1.14.2 h1:S0OHlFk/Gbon/yauFJ4FfJJF5V0fc5HbBTJazi28pRw= github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= @@ -24,7 +28,9 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..d9653a8 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,30 @@ +package config + +import ( + "os" + "ssender/internal/models" + + "gopkg.in/yaml.v2" +) + +func NewConfig(configPath string) (*models.Config, error) { + // Create config structure + config := &models.Config{} + + // Open config file + file, err := os.Open(configPath) + if err != nil { + return nil, err + } + defer file.Close() + + // Init new YAML decode + d := yaml.NewDecoder(file) + + // Start YAML decoding from file + if err := d.Decode(&config); err != nil { + return nil, err + } + + return config, nil +} diff --git a/internal/models/config.go b/internal/models/config.go new file mode 100644 index 0000000..c3688d5 --- /dev/null +++ b/internal/models/config.go @@ -0,0 +1,68 @@ +package models + +import ( + "log" + + "github.com/SevereCloud/vksdk/v2/api" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +type Config struct { + Dbpath string `yaml:"dbpath"` + Telegram struct { + Send bool `yaml:"send"` + SendDebug bool `yaml:"senddebug"` + ChatId int64 `yaml:"chatid"` + Token string `yaml:"token"` + } `yaml:"telegram"` + VK struct { + Send bool `yaml:"send"` + Token string `yaml:"token"` + OwnerId int64 `yaml:"ownerid"` + } `yaml:"vk"` + Facebook struct { + Send bool `yaml:"send"` + Token string `yaml:"token"` + } `yaml:"facebook"` +} + +func (config Config) RunSend(senditems SendItems) { + for _, v := range senditems.ItemList { + if config.Telegram.Send { + log.Println("Send to telegram") + bot, err := tgbotapi.NewBotAPI(config.Telegram.Token) + if err != nil { + log.Panic(err) + } + bot.Debug = config.Telegram.SendDebug + + // s := "" + string(v.Title) + "\n" + html.UnescapeString(string(v.Description)) + + // "\nhttps://t.me/iv?url=" + v.Link + "&rhash=da76512d0ff2a2" + + // "\n\nСсылка на пост: " + v.Link + msg := tgbotapi.NewMessage(config.Telegram.ChatId, v.Link) + msg.ParseMode = "Html" + _, err = bot.Send(msg) + if err != nil { + log.Panic(err) + + } + log.Println("Sended to telegram") + } + if config.VK.Send { + log.Println("Send to VK") + vk := api.NewVK(config.VK.Token) + _, err := vk.WallPost(api.Params{ + "owner_id": config.VK.OwnerId, + "attachments": v.Link, + }) + if err != nil { + log.Fatal(err) + } + log.Println("Sended to VK") + } + if config.Facebook.Send { + log.Println("Send to Facebook") + log.Println("Sending to facebook is not implemented yet") + } + } +} diff --git a/internal/models/rss.go b/internal/models/rss.go new file mode 100644 index 0000000..35fd8d6 --- /dev/null +++ b/internal/models/rss.go @@ -0,0 +1,33 @@ +package models + +import ( + "encoding/xml" + "html/template" +) + +type Rss2 struct { + XMLName xml.Name `xml:"rss"` + Version string `xml:"version,attr"` + // Required + Title string `xml:"channel>title"` + Link string `xml:"channel>link"` + Description string `xml:"channel>description"` + // Optional + PubDate string `xml:"channel>pubDate"` + ItemList []Item `xml:"channel>item"` +} + +type Item struct { + // Required + Title string `xml:"title"` + Link string `xml:"link"` + Description template.HTML `xml:"description"` + // Optional + Content template.HTML `xml:"encoded"` + PubDate string `xml:"pubDate"` + Comments string `xml:"comments"` +} + +type SendItems struct { + ItemList []Item +} diff --git a/main.go b/main.go index 33408e1..d1b6998 100644 --- a/main.go +++ b/main.go @@ -3,58 +3,15 @@ package main import ( "encoding/json" "encoding/xml" - "html/template" "log" "os" + "ssender/internal/config" + "ssender/internal/models" - "github.com/SevereCloud/vksdk/v2/api" "github.com/boltdb/bolt" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/jessevdk/go-flags" - "gopkg.in/yaml.v2" ) -type Config struct { - Dbpath string `yaml:"dbpath"` - Telegram struct { - Send bool `yaml:"send"` - SendDebug bool `yaml:"senddebug"` - ChatId int64 `yaml:"chatid"` - Token string `yaml:"token"` - } `yaml:"telegram"` - VK struct { - Send bool `yaml:"send"` - Token string `yaml:"token"` - OwnerId int64 `yaml:"ownerid"` - } `yaml:"vk"` - Facebook struct { - Send bool `yaml:"send"` - Token string `yaml:"token"` - } `yaml:"facebook"` -} - -func NewConfig(configPath string) (*Config, error) { - // Create config structure - config := &Config{} - - // Open config file - file, err := os.Open(configPath) - if err != nil { - return nil, err - } - defer file.Close() - - // Init new YAML decode - d := yaml.NewDecoder(file) - - // Start YAML decoding from file - if err := d.Decode(&config); err != nil { - return nil, err - } - - return config, nil -} - type Options struct { FileParse string `short:"f" long:"fileparse" description:"File for parse (rss xml)" required:"true"` ConfigPath string `short:"c" long:"configpath" description:"Config file path"` @@ -63,31 +20,8 @@ type Options struct { var ConfigPath = "/etc/ssender/config.yml" -type Rss2 struct { - XMLName xml.Name `xml:"rss"` - Version string `xml:"version,attr"` - // Required - Title string `xml:"channel>title"` - Link string `xml:"channel>link"` - Description string `xml:"channel>description"` - // Optional - PubDate string `xml:"channel>pubDate"` - ItemList []Item `xml:"channel>item"` -} - -type Item struct { - // Required - Title string `xml:"title"` - Link string `xml:"link"` - Description template.HTML `xml:"description"` - // Optional - Content template.HTML `xml:"encoded"` - PubDate string `xml:"pubDate"` - Comments string `xml:"comments"` -} - -func NewRSS(rssPath string) (*Rss2, error) { - rss := &Rss2{} +func NewRSS(rssPath string) (*models.Rss2, error) { + rss := &models.Rss2{} // Open rss2 file file, err := os.Open(rssPath) @@ -106,13 +40,9 @@ func NewRSS(rssPath string) (*Rss2, error) { return rss, nil } -type SendItems struct { - ItemList []Item -} - -var senditems SendItems +var senditems models.SendItems -func FindItems(rss Rss2, dbpath string) { +func FindItems(rss models.Rss2, dbpath string) { db, err := bolt.Open(dbpath, 0600, nil) if err != nil { log.Fatal(err) @@ -131,7 +61,7 @@ func FindItems(rss Rss2, dbpath string) { break } } - if flag != true { + if flag { senditems.ItemList = append(senditems.ItemList, v) } return nil @@ -139,7 +69,7 @@ func FindItems(rss Rss2, dbpath string) { } } -func InitDb(rss Rss2, dbpath string) { +func InitDb(rss models.Rss2, dbpath string) { log.Println("Initialize DB") db, err := bolt.Open(dbpath, 0600, nil) if err != nil { @@ -166,47 +96,6 @@ func InitDb(rss Rss2, dbpath string) { }) } -func (config Config) RunSend() { - for _, v := range senditems.ItemList { - if config.Telegram.Send { - log.Println("Send to telegram") - bot, err := tgbotapi.NewBotAPI(config.Telegram.Token) - if err != nil { - log.Panic(err) - } - bot.Debug = config.Telegram.SendDebug - - // s := "" + string(v.Title) + "\n" + html.UnescapeString(string(v.Description)) + - // "\nhttps://t.me/iv?url=" + v.Link + "&rhash=da76512d0ff2a2" + - // "\n\nСсылка на пост: " + v.Link - msg := tgbotapi.NewMessage(config.Telegram.ChatId, v.Link) - msg.ParseMode = "Html" - _, err = bot.Send(msg) - if err != nil { - log.Panic(err) - - } - log.Println("Sended to telegram") - } - if config.VK.Send { - log.Println("Send to VK") - vk := api.NewVK(config.VK.Token) - _, err := vk.WallPost(api.Params{ - "owner_id": config.VK.OwnerId, - "attachments": v.Link, - }) - if err != nil { - log.Fatal(err) - } - log.Println("Sended to VK") - } - if config.Facebook.Send { - log.Println("Send to Facebook") - log.Println("Sending to facebook is not implemented yet") - } - } -} - func UpdateDb(dbpath string) { log.Println("Update DB") db, err := bolt.Open(dbpath, 0600, nil) @@ -256,7 +145,7 @@ func main() { } // Get config - cfg, err := NewConfig(ConfigPath) + cfg, err := config.NewConfig(ConfigPath) if err != nil { log.Fatal(err) } @@ -277,7 +166,7 @@ func main() { if len(senditems.ItemList) > 0 { // Run send data depended on configuration options log.Println("Run send process") - cfg.RunSend() + cfg.RunSend(senditems) UpdateDb(cfg.Dbpath) } }