Skip to content

Commit

Permalink
Rewrite to use the new twitter-service and mastodon-service
Browse files Browse the repository at this point in the history
Closes: #1
  • Loading branch information
HelixSpiral committed Oct 5, 2023
1 parent 8503310 commit ce0db0c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 129 deletions.
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ go 1.19

require github.com/helixspiral/ndbc v0.0.0-20230724003245-218f2bd95180

require github.com/dghubble/oauth1 v0.7.2
require (
github.com/eclipse/paho.mqtt.golang v1.4.3
golang.org/x/exp v0.0.0-20230724220655-d98519c11495
)

require golang.org/x/exp v0.0.0-20230724220655-d98519c11495
require (
github.com/gorilla/websocket v1.5.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dghubble/oauth1 v0.7.2 h1:pwcinOZy8z6XkNxvPmUDY52M7RDPxt0Xw1zgZ6Cl5JA=
github.com/dghubble/oauth1 v0.7.2/go.mod h1:9erQdIhqhOHG/7K9s/tgh9Ks/AfoyrO5mW/43Lu2+kE=
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
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/helixspiral/ndbc v0.0.0-20230724003245-218f2bd95180 h1:S6U5Szn4IVFaCIuIWBx/er6tH914vV5/ivD0yAhoaQs=
github.com/helixspiral/ndbc v0.0.0-20230724003245-218f2bd95180/go.mod h1:Tx7b+MULDxlvuKnxYDHT+jf9vjIiRY7ZFCWpMpiTFhY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/exp v0.0.0-20230724220655-d98519c11495 h1:zKGKw2WlGb8oPoRGqQ2PT8g2YoCN1w/YbbQjHXCdUWE=
golang.org/x/exp v0.0.0-20230724220655-d98519c11495/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
88 changes: 68 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
package main

import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"time"
"unicode"

mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/helixspiral/ndbc"
"golang.org/x/exp/slices"
)

func main() {
// Some initial Twitter setup
consumerKey := os.Getenv("CONSUMER_KEY")
consumerSecret := os.Getenv("CONSUMER_SECRET")
accessToken := os.Getenv("ACCESS_TOKEN")
accessSecret := os.Getenv("ACCESS_SECRET")

// Some initial Mastodon setup
mastodonServer := os.Getenv("MASTODON_SERVER")
mastodonClientID := os.Getenv("MASTODON_CLIENT_ID")
mastodonClientSecret := os.Getenv("MASTODON_CLIENT_SECRET")
mastodonUser := os.Getenv("MASTODON_USERNAME")
mastodonPass := os.Getenv("MASTODON_PASSWORD")

// Some initial MQTT setup
mqttBroker := os.Getenv("MQTT_BROKER")
mqttClientId := os.Getenv("MQTT_CLIENT_ID")
mqttTopic := os.Getenv("MQTT_TOPIC")

options := mqtt.NewClientOptions().AddBroker(mqttBroker).SetClientID(mqttClientId)
options.WriteTimeout = 20 * time.Second
mqttClient := mqtt.NewClient(options)

// Some initial setup
buoyIDString := os.Getenv("BUOY_ID")
buoyLocation := os.Getenv("BUOY_LOCATION") // There's no clean and easy way to get this from the API, since these buoys don't move we'll just throw it in this way.
Expand All @@ -33,51 +58,74 @@ func main() {
log.Fatal(err)
}

// Upload image to Twitter
mediaResp, err := uploadImage(buoyPicture)
if err != nil {
log.Fatal(err)
}

// Send tweet
var tweetMessage string
// Setup the MQTT message
var message string
if buoyLocation != "" {
tweetMessage += fmt.Sprintf("%s: ", buoyLocation)
message += fmt.Sprintf("%s: ", buoyLocation)
}

// Figure out wind direction
if slices.Contains([]string{
"North", "Northeast",
"East", "Southeast",
"South", "Southwest",
"West", "Northwest",
}, buoyInfo.WindDirection) {
tweetMessage += fmt.Sprintf("Winds coming in from the %s, ", buoyInfo.WindDirection)
message += fmt.Sprintf("Winds coming in from the %s, ", buoyInfo.WindDirection)
}

// Add wind speed
if buoyInfo.WindSpeed > 0 {
tweetMessage += fmt.Sprintf("sustained winds of %.1f m/s", buoyInfo.WindSpeed)
message += fmt.Sprintf("sustained winds of %.1f m/s", buoyInfo.WindSpeed)
if buoyInfo.GustSpeed > 0 && buoyInfo.GustSpeed != buoyInfo.WindSpeed {
tweetMessage += fmt.Sprintf(", and gusting up to %.1f m/s!", buoyInfo.GustSpeed)
message += fmt.Sprintf(", and gusting up to %.1f m/s!", buoyInfo.GustSpeed)
} else {
tweetMessage += "!"
message += "!"
}
}

if tweetMessage != "" {
tweetMessage += "\\r\\n\\r\\n"
// Some newlines and then hashtags
if message != "" {
message += "\\r\\n\\r\\n"
}

tweetMessage += "#noaa #ndbc #buoy #Maine #coast #weather #ocean"
message += "#noaa #ndbc #buoy #Maine #coast #weather #ocean"

// I'm sure there's a better way to do this, but it's simple and it works.
// We use this to ensure the first letter is capital, there's a change it wont be if there's no
// wind direction information
tweetMessageRunes := []rune(tweetMessage)
tweetMessageRunes[0] = unicode.ToUpper(tweetMessageRunes[0])
tweetMessage = string(tweetMessageRunes)
messageRunes := []rune(message)
messageRunes[0] = unicode.ToUpper(messageRunes[0])
message = string(messageRunes)

jsonMsg, err := json.Marshal(&MqttMessage{
MastodonServer: mastodonServer,
MastodonClientID: mastodonClientID,
MastodonClientSecret: mastodonClientSecret,
MastodonUser: mastodonUser,
MastodonPass: mastodonPass,

err = sendMessage(tweetMessage, mediaResp.MediaIDString)
TwitterConsumerKey: consumerKey,
TwitterConsumerSecret: consumerSecret,
TwitterAccessToken: accessToken,
TwitterAccessSecret: accessSecret,

Message: message,
Image: buoyPicture,
})
if err != nil {
log.Fatal(err)
}

// Connect to the MQTT broker
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}

log.Println("Sending message:", message)
token := mqttClient.Publish(mqttTopic, 2, false, jsonMsg)
_ = token.Wait()
if token.Error() != nil {
panic(err)
}
}
25 changes: 13 additions & 12 deletions struct.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

type twitterMediaResponse struct {
MediaID int `json:"media_id"`
MediaIDString string `json:"media_id_string"`
MediaKey string `json:"media_key"`
Size int `json:"size"`
ExpiresAfter int `json:"expires_after_secs"`
Image twitterImageInfo `json:"image"`
}
type MqttMessage struct {
MastodonServer string
MastodonClientID string
MastodonClientSecret string
MastodonUser string
MastodonPass string

TwitterConsumerKey string
TwitterConsumerSecret string
TwitterAccessToken string
TwitterAccessSecret string

type twitterImageInfo struct {
Type string `json:"image_type"`
Width int `json:"w"`
Height int `json:"h"`
Message string
Image []byte
}
89 changes: 0 additions & 89 deletions twitter.go

This file was deleted.

0 comments on commit ce0db0c

Please sign in to comment.