From b39308991136e632bcc01088abecc43bce5030c4 Mon Sep 17 00:00:00 2001 From: indes Date: Sat, 29 Jun 2019 11:11:52 +0800 Subject: [PATCH] feat(telegraph): support multi telegraph token --- config/autoload.go | 7 ++++--- tgraph/publish.go | 5 +++++ tgraph/tgraph.go | 22 +++++++++++++++++----- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/config/autoload.go b/config/autoload.go index 467880c9..1f9b5581 100644 --- a/config/autoload.go +++ b/config/autoload.go @@ -10,7 +10,7 @@ import ( var ( BotToken string Socks5 string - TelegraphToken string + TelegraphToken []string EnableTelegraph bool Mysql MysqlConfig EnableMysql bool @@ -61,13 +61,14 @@ func init() { if *telegraphTokenCli == "" { if viper.IsSet("telegraph_token") { EnableTelegraph = true - TelegraphToken = viper.GetString("telegraph_token") + //TelegraphToken = viper.GetString("") + TelegraphToken = viper.GetStringSlice("telegraph_token") } else { EnableTelegraph = false } } else { EnableTelegraph = true - TelegraphToken = *telegramTokenCli + TelegraphToken = append(TelegraphToken, *telegramTokenCli) } if *intervalCli == 0 { diff --git a/tgraph/publish.go b/tgraph/publish.go index f3375d67..e8746591 100644 --- a/tgraph/publish.go +++ b/tgraph/publish.go @@ -3,6 +3,8 @@ package tgraph import ( "fmt" "log" + "math/rand" + "time" ) func PublishHtml(sourceTitle string, title string, rawLink string, html string) (string, error) { @@ -21,6 +23,9 @@ func PublishHtml(sourceTitle string, title string, rawLink string, html string) title, sourceTitle, ) + rand.Seed(time.Now().Unix()) // initialize global pseudo random generator + client := clientPool[rand.Intn(len(clientPool))] + if page, err := client.CreatePageWithHTML(title+" - "+sourceTitle, authorName, authorUrl, html, true); err == nil { log.Printf("Created telegraph page url: %s", page.URL) return page.URL, err diff --git a/tgraph/tgraph.go b/tgraph/tgraph.go index 59d1d0f3..5ed5b417 100644 --- a/tgraph/tgraph.go +++ b/tgraph/tgraph.go @@ -1,6 +1,7 @@ package tgraph import ( + "fmt" "github.com/indes/flowerss-bot/config" "github.com/meinside/telegraph-go" "log" @@ -16,18 +17,29 @@ var ( authorUrl = "https://github.com/indes/flowerss-bot" authorName = "flowerss" verbose = false - client *telegraph.Client + //client *telegraph.Client + clientPool []*telegraph.Client ) func init() { if config.EnableTelegraph { log.Println("Telegraph Enable") telegraph.Verbose = verbose - var err error - client, err = telegraph.Load(authToken) - if err != nil { - log.Fatal("* Load error: %s", err) + + for _, t := range authToken { + client, err := telegraph.Load(t) + if err != nil { + log.Println(fmt.Sprintf("Telegraph load error: %s token: %s", err, t)) + } else { + clientPool = append(clientPool, client) + } + } + if len(clientPool) == 0 { + config.EnableTelegraph = false + log.Println("Telegraph token error, disable telegraph") + } + } }