main: working stream of hashtag and timeline

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-02-13 15:52:25 -05:00
parent 4319cd2e98
commit e2e1a5c92f
Signed by: vbatts
GPG Key ID: 10937E57733F1362
4 changed files with 142 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.json

18
go.mod Normal file
View File

@ -0,0 +1,18 @@
module tmp.tAT0gR1Esl
go 1.19
require (
github.com/mattn/go-mastodon v0.0.6
github.com/sirupsen/logrus v1.9.0
github.com/urfave/cli/v2 v2.24.3
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

29
go.sum Normal file
View File

@ -0,0 +1,29 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
github.com/urfave/cli/v2 v2.24.3 h1:7Q1w8VN8yE0MJEHP06bv89PjYsN4IHWED2s1v/Zlfm0=
github.com/urfave/cli/v2 v2.24.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

94
main.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/mattn/go-mastodon"
"github.com/sirupsen/logrus"
cli "github.com/urfave/cli/v2" // imports as package "cli"
)
func main() {
app := &cli.App{
Name: "mst",
Usage: "simple mastodon toy",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "default config file (for keys and tokens)",
Value: "./config.json",
},
&cli.BoolFlag{
Name: "timeline",
Usage: "output the users timeline feed",
},
&cli.StringFlag{
Name: "hash",
Usage: "stream the search of the provided hashtag",
},
},
Action: func(ctx *cli.Context) error {
config := mastodon.Config{
Server: "https://mastodon.social",
ClientID: "asdfasdfafd",
ClientSecret: "asfdasdfasfd",
AccessToken: "asdfasdfadfasfd",
}
buf, err := ioutil.ReadFile(ctx.String("config"))
if err == nil {
tmpconfig := mastodon.Config{}
err := json.Unmarshal(buf, &tmpconfig)
if err != nil {
logrus.Fatal(err)
}
config = tmpconfig
} else {
logrus.Warn(err)
}
// https://pkg.go.dev/github.com/mattn/go-mastodon#Config
c := mastodon.NewClient(&config)
if ctx.Bool("timeline") {
timeline, err := c.GetTimelineHome(context.Background(), nil)
if err != nil {
logrus.Fatal(err)
}
for i := len(timeline) - 1; i >= 0; i-- {
fmt.Println(timeline[i])
}
}
if ctx.String("hash") != "" {
// https://pkg.go.dev/github.com/mattn/go-mastodon#Client.StreamingHashtag
evnt, err := c.StreamingHashtag(context.Background(), ctx.String("hash"), false)
if err != nil {
logrus.Fatal(err)
}
for {
select {
case e := <-evnt:
ue := e.(*mastodon.UpdateEvent)
// https://pkg.go.dev/github.com/mattn/go-mastodon#Status
buf, err := json.MarshalIndent(ue.Status, "", " ")
if err != nil {
logrus.Error(err)
}
fmt.Println(string(buf))
}
}
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}