From 41861da83e56b544ba75c19600cd555f92e22240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Fr=C3=B6hle?= Date: Wed, 15 Jan 2020 14:48:44 +0100 Subject: [PATCH] add systemd service files collect configiguration together update README --- README.md | 11 ++++++ closehandle.go | 2 ++ configuration.go | 42 +++++++++++++++++++++++ contrib/etc/systemd/system/fbBot.service | 13 +++++++ facebook.go | 43 ------------------------ main.go | 5 --- 6 files changed, 68 insertions(+), 48 deletions(-) create mode 100644 contrib/etc/systemd/system/fbBot.service diff --git a/README.md b/README.md index 4f501a7..1ad1be8 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,17 @@ - libxml2-dev libonig-dev - chrome / chromium browser +# Install + +```sh +go build . +mv fbBot /usr/local/bin/ +cp contrib/etc / +systemctl daemon-reload +systemctl enable fbBot.service +systemctl start fbBot.service +``` + # Config The Config File `config.yaml` should be located in /etc/fbbot. diff --git a/closehandle.go b/closehandle.go index 76dd77a..11f5f2e 100644 --- a/closehandle.go +++ b/closehandle.go @@ -13,6 +13,8 @@ import ( func SetupCloseHandler() { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) + signal.Notify(c, os.Interrupt, syscall.SIGHUP) + signal.Notify(c, os.Interrupt, syscall.SIGKILL) go func() { <-c fmt.Println("\r- Ctrl+C pressed in Terminal") diff --git a/configuration.go b/configuration.go index 2b9b3f1..331640c 100644 --- a/configuration.go +++ b/configuration.go @@ -1,7 +1,10 @@ package main import ( + "regexp" + "github.com/chromedp/cdproto/cdp" + "github.com/robfig/cron/v3" "github.com/spf13/viper" ) @@ -10,6 +13,37 @@ const ( passwordField = `//*[@id="m_login_password"]` ) +// xPath constants +const ( + xPathArticle string = `(//*/article[contains(concat(" ", normalize-space(@class)," "), " _55wo ") and contains(concat(" ", normalize-space(@class)," "), " _5rgr ")])[1]` + xPathArticleContent string = xPathArticle + `/div/div[contains(concat(" ", normalize-space(@class)," "), " _5rgt ") and contains(concat(" ", normalize-space(@class)," "), " _5nk5 ")]/span` + xPathImageURL string = xPathArticle + `/div/div[2]/div[1]/a/div/div/i` + xPathImageURL2 string = xPathArticle + `/div/div[2]/section/div/i` + xPathImageURL3 string = xPathArticle + `/div/div[2]/section/div/div/i` // Video Preview + xPathGiftURL string = xPathArticle + `/div/div[2]/section/a` + xPathPostingURL string = xPathArticle + `/div/header/div[2]/div/div/div[1]/div/a` +) + +// internal URL constants +const ( + fbPageURL string = "https://www.facebook.com/herowarsgame/" + // fbGameURL for search on expaned urls + fbGameURL string = "apps.facebook.com/mobaheroes" +) + +// Constants for all search for title of the Postings +const ( + FreeTitanArtifact string = "FREE Titan Artifact" + FreeSilverCaskets string = "FREE Silver Caskets" + FreeSoulStones string = "Soul Stones" + FreeSkinStones string = "Skin Stones" + ActionKeepTheAmount string = "Keep the amount" + FreeWinterfestBaubles string = "Winterfest Baubles" + FreeTopFanPackage string = "Top Fan" + FreeEnergyForFee string = "ENERGY FOR FREE" + WinterfestRankingRewards string = "Winterfest ranking rewards" +) + var ( headers = map[string]interface{}{ "Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7", @@ -17,4 +51,12 @@ var ( nodes []*cdp.Node randomText = viper.GetStringSlice("comment_on_posts") + + dataFT *DataFT + + lastUpdatePosted string + regexStyleImage = regexp.MustCompile(`(?m)url\((.*)\);`) + + // cronJob the Cronjob data + cronJob *cron.Cron ) diff --git a/contrib/etc/systemd/system/fbBot.service b/contrib/etc/systemd/system/fbBot.service new file mode 100644 index 0000000..aebd297 --- /dev/null +++ b/contrib/etc/systemd/system/fbBot.service @@ -0,0 +1,13 @@ +[Unit] +Description=HW FB Bot service +After=network.target +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=1 +ExecStart=/usr/local/bin/fbBot + +[Install] +WantedBy=multi-user.target diff --git a/facebook.go b/facebook.go index 906518b..40b7f14 100644 --- a/facebook.go +++ b/facebook.go @@ -5,7 +5,6 @@ import ( "errors" "html" "net/url" - "regexp" "strings" "time" @@ -17,48 +16,6 @@ import ( "mvdan.cc/xurls/v2" ) -// xPath constants -const ( - xPathArticle string = `(//*/article[contains(concat(" ", normalize-space(@class)," "), " _55wo ") and contains(concat(" ", normalize-space(@class)," "), " _5rgr ")])[1]` - xPathArticleContent string = xPathArticle + `/div/div[contains(concat(" ", normalize-space(@class)," "), " _5rgt ") and contains(concat(" ", normalize-space(@class)," "), " _5nk5 ")]/span` - xPathImageURL string = xPathArticle + `/div/div[2]/div[1]/a/div/div/i` - xPathImageURL2 string = xPathArticle + `/div/div[2]/section/div/i` - xPathImageURL3 string = xPathArticle + `/div/div[2]/section/div/div/i` // Video Preview - xPathGiftURL string = xPathArticle + `/div/div[2]/section/a` - xPathPostingURL string = xPathArticle + `/div/header/div[2]/div/div/div[1]/div/a` -) - -// internal URL constants -const ( - fbPageURL string = "https://www.facebook.com/herowarsgame/" - webhookLive string = "https://discordapp.com/api/webhooks/..." - webhookLiveExcelsior string = "https://discordapp.com/api/webhooks/..." - webhookDev string = "https://discordapp.com/api/webhooks/..." - - // fbGameURL for search on expaned urls - fbGameURL string = "apps.facebook.com/mobaheroes" -) - -var ( - dataFT *DataFT - - lastUpdatePosted string - regexStyleImage = regexp.MustCompile(`(?m)url\((.*)\);`) -) - -// Constants for all search for title of the Postings -const ( - FreeTitanArtifact string = "FREE Titan Artifact" - FreeSilverCaskets string = "FREE Silver Caskets" - FreeSoulStones string = "Soul Stones" - FreeSkinStones string = "Skin Stones" - ActionKeepTheAmount string = "Keep the amount" - FreeWinterfestBaubles string = "Winterfest Baubles" - FreeTopFanPackage string = "Top Fan" - FreeEnergyForFee string = "ENERGY FOR FREE" - WinterfestRankingRewards string = "Winterfest ranking rewards" -) - //FBPostData FBPostData type FBPostData struct { PostURL string diff --git a/main.go b/main.go index c39d785..1acfc62 100644 --- a/main.go +++ b/main.go @@ -17,11 +17,6 @@ import ( "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")