Browse Source

Refactoring

master
bn4t 8 months ago
parent
commit
bc8b352043
2 changed files with 7 additions and 11 deletions
  1. +6
    -9
      main.go
  2. +1
    -2
      utils.go

+ 6
- 9
main.go View File

@@ -13,7 +13,7 @@ import (

var MastoClient *mastodon.Client

func main() {
func main() {
if FileExists(".env") {
err := godotenv.Load()
if err != nil {
@@ -21,7 +21,6 @@ func main() {
}
}


clientId, clientSecret := initMastoApp()

MastoClient = mastodon.NewClient(&mastodon.Config{
@@ -30,38 +29,36 @@ func main() {
ClientSecret: clientSecret,
})


err := MastoClient.Authenticate(context.Background(), os.Getenv("USERNAME"), os.Getenv("PASSWORD"))
if err != nil {
log.Fatal(err)
}


c := cron.New()
// check for new releases every 3 minutes
c.AddFunc("0 */3 * * * *", checkForNewReleases )
c.AddFunc("0 */3 * * * *", checkForNewReleases)
c.Run()
}

func checkForNewReleases() {
fp := gofeed.NewParser()
feed, err := fp.ParseURL("https://git.pleroma.social/pleroma/pleroma/-/tags?format=atom")
if err != nil{
if err != nil {
log.Print(err.Error())
}

// check if new release happened in the last 3 mins (cron runs all 3 mins)
// if not don't post it
// Need to use updatedParsed since Gitlab doesn't set a published value
if feed.Items[0].UpdatedParsed.Unix() + 180 > time.Now().Unix(){
if feed.Items[0].UpdatedParsed.Unix()+180 > time.Now().Unix() {
ctx := context.Background()
toot := mastodon.Toot{
Status: "A new Pleroma version got released: " + feed.Items[0].Title + "\n\n" + feed.Items[0].Link,
}

_, err := MastoClient.PostStatus(ctx, &toot)
if err != nil{
if err != nil {
log.Print(err.Error())
}
}
}
}

+ 1
- 2
utils.go View File

@@ -15,7 +15,6 @@ func FileExists(filename string) bool {
return !info.IsDir()
}


func initMastoApp() (clientId string, clientSecret string) {
app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: os.Getenv("SERVER_URL"),
@@ -28,4 +27,4 @@ func initMastoApp() (clientId string, clientSecret string) {
}

return app.ClientID, app.ClientSecret
}
}

Loading…
Cancel
Save