-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (80 loc) · 2.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/silentsokolov/go-vimeo/vimeo"
"github.com/spf13/viper"
"golang.org/x/oauth2"
)
// BOT_NAME is the Bot Name
const BOT_NAME = "plebiscitobot"
const DateFormat = "2006-01-02 15:04:05"
func init() {
viper.SetConfigName(fmt.Sprintf("%s-config", BOT_NAME))
viper.AddConfigPath("/etc/tg-bots/")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("error reading config file: %s \n", err))
}
}
func main() {
var config Config
viper.Unmarshal(&config)
// Last video date
latestDate, err := time.Parse(DateFormat, config.Vimeo.LatestDate)
if err != nil {
latestDate, _ = time.Parse(DateFormat, "2020-09-25 00:00:00")
}
// Telegram
tgBot, err := tgbotapi.NewBotAPI(config.Telegram.Token)
if err != nil {
panic(err)
}
tgBot.Debug = true
// Vimeo
if config.Vimeo.Active {
tc := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Vimeo.Token},
))
vimeoClient := vimeo.NewClient(tc, nil)
for {
log.Printf("Sleeping %d minutes...", config.Vimeo.CheckInterval)
time.Sleep(time.Duration(config.Vimeo.CheckInterval) * time.Minute)
log.Printf("Looking for new videos...")
videos, _, err := vimeoClient.Users.ListVideo(config.Vimeo.UserID, vimeo.OptSort("date"), vimeo.OptDirection("desc"))
if err != nil {
log.Printf("error: %s", err)
continue
}
toPublish := make([]*vimeo.Video, 0)
for _, video := range videos {
if video.ReleaseTime.After(latestDate) {
toPublish = append(toPublish, video)
continue
}
break
}
log.Printf("%d videos found! Publishing...", len(toPublish))
correct := 0
for i := len(toPublish) - 1; i >= 0; i-- {
msg := tgbotapi.NewMessageToChannel(config.Telegram.Channel, toPublish[i].Link)
msg.DisableNotification = true
m, err := tgBot.Send(msg)
if err != nil {
log.Printf("cannot send message: %s", err)
log.Printf("Response: %+v", m)
continue
}
latestDate = toPublish[i].ReleaseTime
correct++
viper.Set("vimeo.latestDate", latestDate.Format(DateFormat))
viper.WriteConfig()
}
log.Printf("Published %d videos", correct)
log.Printf("New latest date: %s", latestDate.Format(DateFormat))
}
}
}