|
|
|
|
@ -3,58 +3,15 @@ package main
@@ -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 {
@@ -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) {
@@ -106,13 +40,9 @@ func NewRSS(rssPath string) (*Rss2, error) {
|
|
|
|
|
return rss, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type SendItems struct { |
|
|
|
|
ItemList []Item |
|
|
|
|
} |
|
|
|
|
var senditems models.SendItems |
|
|
|
|
|
|
|
|
|
var senditems 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) {
@@ -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) {
@@ -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) {
@@ -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 := "<b>" + string(v.Title) + "</b>\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() {
@@ -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() {
@@ -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) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|