Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(telegraph): support multi telegraph token
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Jun 29, 2019
1 parent ab441bc commit b393089
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
7 changes: 4 additions & 3 deletions config/autoload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
BotToken string
Socks5 string
TelegraphToken string
TelegraphToken []string
EnableTelegraph bool
Mysql MysqlConfig
EnableMysql bool
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions tgraph/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tgraph
import (
"fmt"
"log"
"math/rand"
"time"
)

func PublishHtml(sourceTitle string, title string, rawLink string, html string) (string, error) {
Expand All @@ -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
Expand Down
22 changes: 17 additions & 5 deletions tgraph/tgraph.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tgraph

import (
"fmt"
"github.com/indes/flowerss-bot/config"
"github.com/meinside/telegraph-go"
"log"
Expand All @@ -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")

}

}

}

0 comments on commit b393089

Please sign in to comment.