first commit
This commit is contained in:
203
main.go
Normal file
203
main.go
Normal file
@@ -0,0 +1,203 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/chromedp/chromedp"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/lunny/html2md"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
// GlobalApp holds a pointer for the App
|
||||
cronJob *cron.Cron
|
||||
)
|
||||
|
||||
func init() {
|
||||
viper.SetDefault("development_mode", true)
|
||||
viper.SetDefault("cronjob_interval", "@every 5m")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("config") // name of config file (without extension)
|
||||
viper.AddConfigPath("/etc/fbBot/") // path to look for the config file in
|
||||
viper.AddConfigPath("$HOME/.fbBot") // call multiple times to add many search paths
|
||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
// any approach to require this configuration into your program.
|
||||
var yamlExample = []byte(`
|
||||
development_mode: true
|
||||
cronjob_interval: "@every 5m"
|
||||
no_run_on_start: false
|
||||
http_useragent: Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19
|
||||
(KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19
|
||||
webhooks:
|
||||
live:
|
||||
- https://discordapp.com/api/webhooks/...
|
||||
- https://discordapp.com/api/webhooks/...
|
||||
devel:
|
||||
- https://discordapp.com/api/webhooks/...
|
||||
comment_on_posts:
|
||||
- TY
|
||||
- ty!
|
||||
- danke!
|
||||
- I could use it!
|
||||
- thx!
|
||||
- Thank you!
|
||||
- Merci
|
||||
- Yeah!
|
||||
- Vielen Dank!
|
||||
login:
|
||||
email: example@example.com
|
||||
passwd: thefbpassword
|
||||
`)
|
||||
|
||||
viper.ReadConfig(bytes.NewBuffer(yamlExample))
|
||||
viper.WriteConfig()
|
||||
} else {
|
||||
// Config file was found but another error was produced
|
||||
}
|
||||
}
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("Config file changed:", e.Name)
|
||||
})
|
||||
|
||||
html2md.AddRule("span", &html2md.Rule{
|
||||
Patterns: []string{"span"},
|
||||
Tp: html2md.Void,
|
||||
Replacement: func(innerHTML string, attrs []string) string {
|
||||
return innerHTML
|
||||
},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
SetupCloseHandler()
|
||||
if viper.GetBool("no_run_on_start") {
|
||||
go cronTask()
|
||||
}
|
||||
|
||||
cronJob = cron.New()
|
||||
cronJob.AddFunc(viper.GetString("cronjob_interval"), func() {
|
||||
cronTask()
|
||||
})
|
||||
cronJob.Start()
|
||||
|
||||
for {
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func postRequest(content *Webhook) {
|
||||
|
||||
webhooks := viper.GetStringSlice("webhooks.live")
|
||||
if viper.GetBool("development_mode") {
|
||||
webhooks = viper.GetStringSlice("webhooks.devel")
|
||||
}
|
||||
|
||||
jsonStrBytes, err := content.Marshal()
|
||||
if err != nil {
|
||||
log.Println("postRequest error:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
for _, webhook := range webhooks {
|
||||
//fmt.Println("URL:>", webhook)
|
||||
|
||||
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStrBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("ERROR: " + err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
log.Println("response Status:", resp.Status)
|
||||
//fmt.Println("response Headers:", resp.Header)
|
||||
}
|
||||
}
|
||||
|
||||
func cronTask() error {
|
||||
dir := filepath.Join(".", "content")
|
||||
os.MkdirAll(dir, os.ModePerm)
|
||||
|
||||
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
||||
chromedp.DisableGPU,
|
||||
chromedp.UserAgent(viper.GetString("http_useragent")),
|
||||
chromedp.UserDataDir(dir),
|
||||
)
|
||||
|
||||
allocCtx, cancelAll := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||
defer cancelAll()
|
||||
|
||||
// also set up a custom logger
|
||||
taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf), chromedp.WithErrorf(log.Printf))
|
||||
defer cancel()
|
||||
|
||||
chromedp.Run(taskCtx)
|
||||
|
||||
page1(taskCtx)
|
||||
page2(taskCtx)
|
||||
page3(taskCtx)
|
||||
page4(taskCtx)
|
||||
page5(taskCtx)
|
||||
page6(taskCtx)
|
||||
page7(taskCtx)
|
||||
|
||||
postID := ""
|
||||
var fbPost *FBPostData
|
||||
var fbPostErr error
|
||||
postID, fbPost, fbPostErr = page8(taskCtx)
|
||||
fmt.Printf("Error: %#v\n", fbPostErr)
|
||||
if fbPost.TimeStamp != lastUpdatePosted {
|
||||
page9(taskCtx, postID)
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
if fbPost.TimeStamp != lastUpdatePosted {
|
||||
timeStamp := fbPost.TimeStamp
|
||||
pTime, err := time.Parse(time.RFC3339, fbPost.TimeStamp)
|
||||
if err == nil {
|
||||
timeStamp = pTime.UTC().Format("2006-01-02 15:04:05 UTC")
|
||||
}
|
||||
embeded := []*Embed{}
|
||||
|
||||
embeded = append(embeded, &Embed{
|
||||
Title: fbPost.Title,
|
||||
Description: fbPost.Content + `
|
||||
|
||||
Date of Post: ` + timeStamp + `
|
||||
Post URL: ` + fbPost.PostURL,
|
||||
Image: Image{
|
||||
URL: fbPost.ImageURL,
|
||||
},
|
||||
URL: fbPost.GiftURL,
|
||||
})
|
||||
|
||||
dcontent := &Webhook{
|
||||
Content: fbPost.Title,
|
||||
Embeds: embeded,
|
||||
}
|
||||
if fbPostErr == nil {
|
||||
postRequest(dcontent)
|
||||
}
|
||||
lastUpdatePosted = fbPost.TimeStamp
|
||||
viper.Set("lastPostTimestamp", fbPost.TimeStamp)
|
||||
viper.WriteConfig()
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user