mirror of
https://github.com/idanoo/NZCovidBot
synced 2025-07-02 03:32:14 +00:00
Pulls from git and posts to discord
This commit is contained in:
parent
d63f121871
commit
b37a425589
12 changed files with 611 additions and 5 deletions
6
.env.example
Normal file
6
.env.example
Normal file
|
@ -0,0 +1,6 @@
|
|||
DISCORD_WEBHOOKS= // Comma Separated!
|
||||
SOURCE_REPO=https://github.com/minhealthnz/nz-covid-data.git // Source repo
|
||||
TWITTER_CONSUMER_KEY=
|
||||
TWITTER_CONSUMER_SECRET=
|
||||
TWITTER_ACCESS_TOKEN=
|
||||
TWITTER_ACCESS_TOKEN_SECRET=
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
.env
|
||||
tmp
|
|
@ -1,2 +0,0 @@
|
|||
# 0.0.1
|
||||
|
14
README.md
14
README.md
|
@ -1,3 +1,13 @@
|
|||
# covid-location-discord-bot
|
||||
# NZCovidBot
|
||||
Pull data from Github and parse new locations and fire them off to any configured endpoints
|
||||
|
||||
Post to discord webhooks when new locations are posted
|
||||
### Run locally
|
||||
```
|
||||
go run cmd/nzcovidbot/*.go
|
||||
```
|
||||
|
||||
### Build
|
||||
```
|
||||
go build -o nzcovidbot cmd/nzcovidbot/*.go
|
||||
./nzcovidbot
|
||||
```
|
||||
|
|
37
cmd/nzcovidbot/main.go
Normal file
37
cmd/nzcovidbot/main.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.m2.nz/idanoo/nzcovidbot/internal/nzcovidbot"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("Starting NZ Covid Bot")
|
||||
|
||||
// Load .env file
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
// Fetch discord webhooks
|
||||
nzcovidbot.DiscordWebhooks = strings.Split(os.Getenv("DISCORD_WEBHOOKS"), ",")
|
||||
|
||||
// Fetch twitter keys
|
||||
nzcovidbot.TwitterCreds = nzcovidbot.TwitterCredentials{
|
||||
ConsumerKey: os.Getenv("TWITTER_CONSUMER_KEY"),
|
||||
ConsumerSecret: os.Getenv("TWITTER_CONSUMER_SECRET"),
|
||||
AccessToken: os.Getenv("TWITTER_ACCESS_TOKEN"),
|
||||
AccessTokenSecret: os.Getenv("TWITTER_ACCESS_TOKEN_SECRET"),
|
||||
}
|
||||
|
||||
// Git repo URL
|
||||
nzcovidbot.Repository = os.Getenv("SOURCE_REPO")
|
||||
|
||||
// Boot up listeners / main loop
|
||||
nzcovidbot.Lesgoooo()
|
||||
}
|
12
go.mod
Normal file
12
go.mod
Normal file
|
@ -0,0 +1,12 @@
|
|||
module git.m2.nz/idanoo/nzcovidbot
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/DisgoOrg/disgohook v1.4.3
|
||||
github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e
|
||||
github.com/dghubble/oauth1 v0.7.0
|
||||
github.com/go-git/go-git/v5 v5.4.2
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d
|
||||
)
|
122
go.sum
Normal file
122
go.sum
Normal file
|
@ -0,0 +1,122 @@
|
|||
github.com/DisgoOrg/disgohook v1.4.3 h1:JtZiV0jAku9NZRYD6wVH7tWY1617rh4tRqn4ihTUJRc=
|
||||
github.com/DisgoOrg/disgohook v1.4.3/go.mod h1:aHNyBHq1pBbdWrkCq3ZCSBeavUoGWZAAT4+609EcrvU=
|
||||
github.com/DisgoOrg/log v1.1.0 h1:a6hLfVSDuTFJc5AKQ8FDYQ5TASnwk3tciUyXThm1CR4=
|
||||
github.com/DisgoOrg/log v1.1.0/go.mod h1:Qihgz6fax3JCfuO7vxVavL0LyHS0sUdQ9OmykQ2fiQs=
|
||||
github.com/DisgoOrg/restclient v1.2.7 h1:A8ltFP+8O2q9ZTk/vRmlQRvH2cbcnaacUV2uXWFcONQ=
|
||||
github.com/DisgoOrg/restclient v1.2.7/go.mod h1:2pc/htya/5kjxvWNYya98sb8B4mexobxmWvhTiWPt94=
|
||||
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
|
||||
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
|
||||
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
|
||||
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
|
||||
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
|
||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
|
||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
|
||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
|
||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
||||
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
|
||||
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e h1:o0sI/cfhAXdtAbiIVeTd4hmwtwgs4cQFwRBhmbx8AKY=
|
||||
github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE=
|
||||
github.com/dghubble/oauth1 v0.7.0 h1:AlpZdbRiJM4XGHIlQ8BuJ/wlpGwFEJNnB4Mc+78tA/w=
|
||||
github.com/dghubble/oauth1 v0.7.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
|
||||
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
|
||||
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
|
||||
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
|
||||
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
|
||||
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
|
||||
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
|
||||
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
|
||||
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
|
||||
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
|
||||
github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
|
||||
github.com/go-git/go-billy/v5 v5.3.1 h1:CPiOUAzKtMRvolEKw+bG1PLRpT7D3LIs3/3ey4Aiu34=
|
||||
github.com/go-git/go-billy/v5 v5.3.1/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
|
||||
github.com/go-git/go-git-fixtures/v4 v4.2.1 h1:n9gGL1Ct/yIw+nfsfr8s4+sbhT+Ncu2SubfXjIWgci8=
|
||||
github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6vMiRR/nnVTBtavnB0=
|
||||
github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4=
|
||||
github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc=
|
||||
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
|
||||
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
|
||||
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d h1:xQcF7b7cZLWZG/+7A4G7un1qmEDYHIvId9qxRS1mZMs=
|
||||
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d/go.mod h1:BzSc3WEF8R+lCaP5iGFRxd5kIXy4JKOZAwNe1w0cdc0=
|
||||
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
|
||||
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
|
||||
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs=
|
||||
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 h1:RX8C8PRZc2hTIod4ds8ij+/4RQX3AqhYj3uOHmyaz4E=
|
||||
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
99
internal/nzcovidbot/csv.go
Normal file
99
internal/nzcovidbot/csv.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Slice of updated located
|
||||
type UpdatedLocations struct {
|
||||
Locations []UpdatedRow
|
||||
}
|
||||
|
||||
// Updated data
|
||||
type UpdatedRow struct {
|
||||
ChangeType string // ADDED, REMOVED, MODIFIED
|
||||
DiscordData string // Formatted Row data
|
||||
TwitterData string // Formatted Row data
|
||||
}
|
||||
|
||||
// Struct of updated locations
|
||||
var updatedLocations UpdatedLocations
|
||||
|
||||
// parseCsvRow Build into struct for output later
|
||||
func parseCsvRow(changeType string, data string) {
|
||||
newRow := UpdatedRow{
|
||||
ChangeType: changeType,
|
||||
DiscordData: formatCsvDiscordRow(data),
|
||||
TwitterData: formatCsvTwitterRow(data),
|
||||
}
|
||||
|
||||
updatedLocations.Locations = append(updatedLocations.Locations, newRow)
|
||||
}
|
||||
|
||||
// formatCsvDiscordRow Format the string to a tidy string for the interwebs
|
||||
func formatCsvDiscordRow(data string) string {
|
||||
// Split string
|
||||
r := csv.NewReader(strings.NewReader(data))
|
||||
r.Comma = ','
|
||||
fields, err := r.Read()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
c := make([]string, 0)
|
||||
c = append(c, fields...)
|
||||
return fmt.Sprintf("**%s** %s on _%s_ - _%s_", c[1], c[2], c[4], c[5])
|
||||
}
|
||||
|
||||
// formatCsvTwitterRow Format the string to a tidy string for the interwebs
|
||||
func formatCsvTwitterRow(data string) string {
|
||||
// Split string
|
||||
r := csv.NewReader(strings.NewReader(data))
|
||||
r.Comma = ','
|
||||
fields, err := r.Read()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
c := make([]string, 0)
|
||||
c = append(c, fields...)
|
||||
return fmt.Sprintf("**%s** - %s\n_%s_ - _%s_", c[1], c[2], c[4], c[5])
|
||||
}
|
||||
|
||||
func getPostableDiscordData() string {
|
||||
if len(updatedLocations.Locations) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
rows := make([]string, 0)
|
||||
for _, location := range updatedLocations.Locations {
|
||||
if location.ChangeType == "REMOVED" {
|
||||
rows = append(rows, fmt.Sprintf("REMOVED: %s", location.DiscordData))
|
||||
} else {
|
||||
rows = append(rows, location.DiscordData)
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(rows, "\n\n")
|
||||
}
|
||||
|
||||
func getPostableTwitterData() string {
|
||||
if len(updatedLocations.Locations) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
rows := make([]string, 0)
|
||||
for _, location := range updatedLocations.Locations {
|
||||
if location.ChangeType == "REMOVED" {
|
||||
rows = append(rows, fmt.Sprintf("REMOVED: %s", location.TwitterData))
|
||||
} else {
|
||||
rows = append(rows, location.TwitterData)
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(rows, "\n\n")
|
||||
}
|
48
internal/nzcovidbot/discord.go
Normal file
48
internal/nzcovidbot/discord.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/DisgoOrg/disgohook"
|
||||
"github.com/DisgoOrg/disgohook/api"
|
||||
)
|
||||
|
||||
var (
|
||||
// Slice of discord webhooks
|
||||
DiscordWebhooks []string
|
||||
)
|
||||
|
||||
func postToDiscord(webhookString string, msg string) {
|
||||
tokenParts := strings.Split(webhookString, "/")
|
||||
len := len(tokenParts)
|
||||
webhook, err := disgohook.NewWebhookClientByToken(nil, nil, tokenParts[len-2]+"/"+tokenParts[len-1])
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
// message, err := webhook.SendContent(msg)
|
||||
// if err != nil {
|
||||
// log.Print(err)
|
||||
// return
|
||||
// }
|
||||
|
||||
// // message, err = webhook.SendEmbeds(api.NewEmbedBuilder().
|
||||
// // SetDescription(msg).
|
||||
// // Build(),
|
||||
// // )
|
||||
// if err != nil {
|
||||
// log.Print(err)
|
||||
// return
|
||||
// }
|
||||
|
||||
_, err = webhook.SendMessage(api.NewWebhookMessageCreateBuilder().
|
||||
SetContent(msg).
|
||||
Build(),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
165
internal/nzcovidbot/git.go
Normal file
165
internal/nzcovidbot/git.go
Normal file
|
@ -0,0 +1,165 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/waigani/diffparser"
|
||||
)
|
||||
|
||||
// Repo URL
|
||||
var gitRepo *git.Repository
|
||||
|
||||
// Current hash we have parsed
|
||||
var commitHash string
|
||||
|
||||
// Location to store tmp data
|
||||
var tmpDirectory = "./tmp"
|
||||
|
||||
func loadRepo(repository string) {
|
||||
r, err := git.PlainOpen(tmpDirectory + "/repo")
|
||||
if err != nil {
|
||||
if err.Error() == "repository does not exist" {
|
||||
r = cloneRepo(repository)
|
||||
} else {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
commitHash := getCommitHash()
|
||||
log.Printf("Last reported hash: %s", commitHash)
|
||||
gitRepo = r
|
||||
}
|
||||
|
||||
func cloneRepo(repository string) *git.Repository {
|
||||
if _, err := os.Stat(tmpDirectory); os.IsNotExist(err) {
|
||||
err = os.Mkdir(tmpDirectory, 0755)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
r, err := git.PlainClone(tmpDirectory+"/repo", false, &git.CloneOptions{
|
||||
URL: repository,
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ref, err := r.Head()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
setCommitHash(ref.Hash().String())
|
||||
|
||||
log.Println("Succesfully cloned repo")
|
||||
return r
|
||||
}
|
||||
|
||||
// Set it in memory + write to disk
|
||||
func setCommitHash(hash string) error {
|
||||
log.Printf("Update hash to: %s", hash)
|
||||
commitHash = hash
|
||||
return os.WriteFile(tmpDirectory+"/commithash", []byte(hash), 0644)
|
||||
}
|
||||
|
||||
// Read from memory, or disk
|
||||
func getCommitHash() string {
|
||||
if commitHash != "" {
|
||||
return commitHash
|
||||
}
|
||||
|
||||
hash, err := ioutil.ReadFile(tmpDirectory + "/commithash")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
commitHash = string(hash)
|
||||
return commitHash
|
||||
}
|
||||
|
||||
func checkForUpdates() {
|
||||
// Fetch updates from remote
|
||||
pullOptions := git.PullOptions{
|
||||
RemoteName: "origin",
|
||||
}
|
||||
|
||||
// Pull latest changes if exist
|
||||
worktree, err := gitRepo.Worktree()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = worktree.Pull(&pullOptions)
|
||||
if err != nil {
|
||||
if err == git.NoErrAlreadyUpToDate {
|
||||
log.Println("No updates")
|
||||
return
|
||||
} else {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Get current commit hash
|
||||
head, err := gitRepo.Head()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get latest commit
|
||||
latestCommit, err := gitRepo.CommitObject(head.Hash())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get current commit
|
||||
currentCommit, err := gitRepo.CommitObject(plumbing.NewHash(getCommitHash()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get patch of changes
|
||||
patch, err := currentCommit.Patch(latestCommit)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Parse git diff
|
||||
diff, err := diffparser.Parse(patch.String())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Loop through file changes
|
||||
for _, file := range diff.Files {
|
||||
if strings.HasSuffix(file.NewName, ".csv") {
|
||||
for _, hunk := range file.Hunks {
|
||||
newRange := hunk.WholeRange
|
||||
for _, line := range newRange.Lines {
|
||||
if line.Mode == diffparser.ADDED {
|
||||
parseCsvRow("ADDED", line.Content)
|
||||
}
|
||||
// switch changeType := line.Mode; changeType {
|
||||
// case diffparser.UNCHANGED:
|
||||
// continue
|
||||
// case diffparser.ADDED:
|
||||
// parseCsvRow("ADDED", line.Content)
|
||||
// case diffparser.REMOVED:
|
||||
// continue
|
||||
// // To re-add in future?
|
||||
// // parseCsvRow("REMOVED", line.Content)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setCommitHash(head.Hash().String())
|
||||
postTheUpdates()
|
||||
}
|
60
internal/nzcovidbot/nzcovidbot.go
Normal file
60
internal/nzcovidbot/nzcovidbot.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Repository string
|
||||
|
||||
func Lesgoooo() {
|
||||
// Setup repo stuff
|
||||
loadRepo(Repository)
|
||||
|
||||
// Create chan to end timer
|
||||
endTicker := make(chan bool)
|
||||
|
||||
// Timer to run every minute
|
||||
minuteTicker := time.NewTicker(time.Duration(15) * time.Second)
|
||||
|
||||
// Initial check on load
|
||||
go checkForUpdates()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-endTicker:
|
||||
fmt.Println("Stopping background workers")
|
||||
return
|
||||
case <-minuteTicker.C:
|
||||
// Check for updates
|
||||
go checkForUpdates()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func postTheUpdates() {
|
||||
// Discord
|
||||
postableDiscordData := getPostableDiscordData()
|
||||
|
||||
if postableDiscordData == "" {
|
||||
return
|
||||
}
|
||||
|
||||
for _, discordWebhook := range DiscordWebhooks {
|
||||
postToDiscord(discordWebhook, postableDiscordData)
|
||||
}
|
||||
|
||||
// Twitter
|
||||
// postableTwitterData := getPostableTwitterData()
|
||||
// if postableTwitterData == "" {
|
||||
// return
|
||||
// }
|
||||
|
||||
// for _, discordWebhook := range DiscordWebhooks {
|
||||
// postToTwitter(discordWebhook, postableTwitterData)
|
||||
// }
|
||||
|
||||
// Clear out posted data!
|
||||
updatedLocations = UpdatedLocations{}
|
||||
}
|
48
internal/nzcovidbot/twitter.go
Normal file
48
internal/nzcovidbot/twitter.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/dghubble/go-twitter/twitter"
|
||||
"github.com/dghubble/oauth1"
|
||||
)
|
||||
|
||||
type TwitterCredentials struct {
|
||||
ConsumerKey string
|
||||
ConsumerSecret string
|
||||
AccessToken string
|
||||
AccessTokenSecret string
|
||||
}
|
||||
|
||||
var TwitterCreds TwitterCredentials
|
||||
|
||||
// getClient is a helper function that will return a twitter client
|
||||
// that we can subsequently use to send tweets, or to stream new tweets
|
||||
// this will take in a pointer to a Credential struct which will contain
|
||||
// everything needed to authenticate and return a pointer to a twitter Client
|
||||
// or an error
|
||||
func getClient(creds *TwitterCredentials) (*twitter.Client, error) {
|
||||
// Pass in your consumer key (API Key) and your Consumer Secret (API Secret)
|
||||
config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
|
||||
// Pass in your Access Token and your Access Token Secret
|
||||
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
|
||||
|
||||
httpClient := config.Client(oauth1.NoContext, token)
|
||||
client := twitter.NewClient(httpClient)
|
||||
|
||||
// Verify Credentials
|
||||
verifyParams := &twitter.AccountVerifyParams{
|
||||
SkipStatus: twitter.Bool(true),
|
||||
IncludeEmail: twitter.Bool(true),
|
||||
}
|
||||
|
||||
// we can retrieve the user and verify if the credentials
|
||||
// we have used successfully allow us to log in!
|
||||
user, _, err := client.Accounts.VerifyCredentials(verifyParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Printf("User's ACCOUNT:\n%+v\n", user)
|
||||
return client, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue