mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-02 22:32:19 +00:00
Test mastodon posting
This commit is contained in:
parent
aec56e7352
commit
3aeff25285
8 changed files with 78 additions and 2 deletions
|
@ -13,3 +13,7 @@ PEERTUBE_DB_SCHEMA=peertube_prod
|
||||||
MATRIX_WEBHOOK_URL=""
|
MATRIX_WEBHOOK_URL=""
|
||||||
MATRIX_WEBHOOK_API_KEY=""
|
MATRIX_WEBHOOK_API_KEY=""
|
||||||
MATRIX_WEBHOOK_CHANNEL=""
|
MATRIX_WEBHOOK_CHANNEL=""
|
||||||
|
|
||||||
|
MASTODON_INSTANCE_URL=""
|
||||||
|
MASTODON_USERNAME=""
|
||||||
|
MASTODON_PASSWORD=""
|
|
@ -49,6 +49,19 @@ func main() {
|
||||||
gms.MATRIX_WEBHOOK_URL = ""
|
gms.MATRIX_WEBHOOK_URL = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load mastodon data if exists
|
||||||
|
gms.MASTODON_INSTANCE_URL = os.Getenv("MASTODON_INSTANCE_URL")
|
||||||
|
gms.MASTODON_USERNAME = os.Getenv("MASTODON_USERNAME")
|
||||||
|
gms.MASTODON_PASSWORD = os.Getenv("MASTODON_PASSWORD")
|
||||||
|
if gms.MASTODON_INSTANCE_URL == "" ||
|
||||||
|
gms.MASTODON_USERNAME == "" ||
|
||||||
|
gms.MASTODON_PASSWORD == "" {
|
||||||
|
log.Println("MASTODON_INSTANCE_URL info incompelete. Skipping")
|
||||||
|
|
||||||
|
// Set URL empty so we can check this later on
|
||||||
|
gms.MASTODON_INSTANCE_URL = ""
|
||||||
|
}
|
||||||
|
|
||||||
// Load schemas if set
|
// Load schemas if set
|
||||||
gms.PIXELFED_DB_SCHEMA = os.Getenv("PIXELFED_DB_SCHEMA")
|
gms.PIXELFED_DB_SCHEMA = os.Getenv("PIXELFED_DB_SCHEMA")
|
||||||
gms.MATRIX_DB_SCHEMA = os.Getenv("MATRIX_DB_SCHEMA")
|
gms.MATRIX_DB_SCHEMA = os.Getenv("MATRIX_DB_SCHEMA")
|
||||||
|
|
3
go.mod
3
go.mod
|
@ -3,6 +3,9 @@ module gomastodonstats
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/joho/godotenv v1.4.0 // indirect
|
github.com/joho/godotenv v1.4.0 // indirect
|
||||||
github.com/lib/pq v1.10.7 // indirect
|
github.com/lib/pq v1.10.7 // indirect
|
||||||
|
github.com/mattn/go-mastodon v0.0.6 // indirect
|
||||||
|
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
|
||||||
)
|
)
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -1,4 +1,10 @@
|
||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||||
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/mattn/go-mastodon v0.0.6 h1:lqU1sOeeIapaDsDUL6udDZIzMb2Wqapo347VZlaOzf0=
|
||||||
|
github.com/mattn/go-mastodon v0.0.6/go.mod h1:cg7RFk2pcUfHZw/IvKe1FUzmlq5KnLFqs7eV2PHplV8=
|
||||||
|
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
|
||||||
|
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE=
|
||||||
|
|
|
@ -14,6 +14,10 @@ var (
|
||||||
MATRIX_WEBHOOK_API_KEY string
|
MATRIX_WEBHOOK_API_KEY string
|
||||||
MATRIX_WEBHOOK_CHANNEL string
|
MATRIX_WEBHOOK_CHANNEL string
|
||||||
|
|
||||||
|
MASTODON_INSTANCE_URL string
|
||||||
|
MASTODON_USERNAME string
|
||||||
|
MASTODON_PASSWORD string
|
||||||
|
|
||||||
// UserCount metric name
|
// UserCount metric name
|
||||||
METRICNAME_USERCOUNT = "userCount"
|
METRICNAME_USERCOUNT = "userCount"
|
||||||
|
|
||||||
|
|
|
@ -15,4 +15,6 @@ func Run() {
|
||||||
updatedMetrics := persistMetrics(metrics)
|
updatedMetrics := persistMetrics(metrics)
|
||||||
|
|
||||||
sendToMatrix(updatedMetrics)
|
sendToMatrix(updatedMetrics)
|
||||||
|
|
||||||
|
postToMastodon(updatedMetrics)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,43 @@
|
||||||
package gomastodonstats
|
package gomastodonstats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/mattn/go-mastodon"
|
||||||
|
)
|
||||||
|
|
||||||
|
func postToMastodon(metrics []metric) {
|
||||||
|
if MASTODON_INSTANCE_URL == "" {
|
||||||
|
log.Printf("Skipping posting to mastodon. Missing configuration")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c := mastodon.NewClient(&mastodon.Config{
|
||||||
|
Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL),
|
||||||
|
ClientID: "client-id",
|
||||||
|
ClientSecret: "client-secret",
|
||||||
|
})
|
||||||
|
err := c.Authenticate(context.Background(), MASTODON_USERNAME, MASTODON_PASSWORD)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Invalid mastodon credentials")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build status
|
||||||
|
startOfDay := getStartofDay()
|
||||||
|
msg := fmt.Sprintf("Stats for %s\n\n", startOfDay.String())
|
||||||
|
for _, m := range metrics {
|
||||||
|
msg = msg + getPrintableString(m) + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
toot := mastodon.Toot{
|
||||||
|
Status: msg,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = c.PostStatus(context.Background(), &toot)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/text/cases"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Stores out metric/row data
|
// Stores out metric/row data
|
||||||
|
@ -114,5 +116,5 @@ func getUserCounts() ([]metric, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPrintableString(m metric) string {
|
func getPrintableString(m metric) string {
|
||||||
return fmt.Sprintf("%s: %d", m.Service, m.MetricValue)
|
return fmt.Sprintf("%s: %d", cases.Title(m.Service), m.MetricValue)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue