A bot that toots out new Pleroma releases. https://social.bn4t.me/PleromaReleases
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5KB

  1. package main
  2. import (
  3. "context"
  4. "github.com/joho/godotenv"
  5. "github.com/mattn/go-mastodon"
  6. "github.com/mmcdole/gofeed"
  7. "github.com/robfig/cron"
  8. "log"
  9. "os"
  10. "time"
  11. )
  12. var MastoClient *mastodon.Client
  13. func main() {
  14. if FileExists(".env") {
  15. err := godotenv.Load()
  16. if err != nil {
  17. log.Fatal("Error loading .env file")
  18. }
  19. }
  20. clientId, clientSecret := initMastoApp()
  21. MastoClient = mastodon.NewClient(&mastodon.Config{
  22. Server: os.Getenv("SERVER_URL"),
  23. ClientID: clientId,
  24. ClientSecret: clientSecret,
  25. })
  26. err := MastoClient.Authenticate(context.Background(), os.Getenv("USERNAME"), os.Getenv("PASSWORD"))
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. c := cron.New()
  31. // check for new releases every 3 minutes
  32. c.AddFunc("0 */3 * * * *", checkForNewReleases)
  33. c.Run()
  34. }
  35. func checkForNewReleases() {
  36. fp := gofeed.NewParser()
  37. feed, err := fp.ParseURL("https://git.pleroma.social/pleroma/pleroma/-/tags?format=atom")
  38. if err != nil {
  39. log.Print(err.Error())
  40. }
  41. // check if new release happened in the last 3 mins (cron runs all 3 mins)
  42. // if not don't post it
  43. // Need to use updatedParsed since Gitlab doesn't set a published value
  44. if feed.Items[0].UpdatedParsed.Unix()+180 > time.Now().Unix() {
  45. ctx := context.Background()
  46. toot := mastodon.Toot{
  47. Status: "A new Pleroma version got released: " + feed.Items[0].Title + "\n\n" + feed.Items[0].Link,
  48. }
  49. _, err := MastoClient.PostStatus(ctx, &toot)
  50. if err != nil {
  51. log.Print(err.Error())
  52. }
  53. }
  54. }