2020-01-11 18:38:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-01-11 19:11:30 +00:00
|
|
|
"flag"
|
2020-01-11 18:38:50 +00:00
|
|
|
|
2020-01-11 20:18:59 +00:00
|
|
|
"github.com/McKael/madon/v2"
|
2020-01-11 18:38:50 +00:00
|
|
|
"github.com/jaytaylor/html2text"
|
|
|
|
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
|
|
|
|
"within.website/ln"
|
|
|
|
"within.website/ln/opname"
|
|
|
|
)
|
|
|
|
|
2020-01-11 19:11:30 +00:00
|
|
|
var (
|
|
|
|
// Mastodon
|
|
|
|
mastodonInstance = flag.String("mastodon-instance", "", "Mastodon instance to connect to")
|
|
|
|
mastodonAppID = flag.String("mastodon-app-id", "", "Mastodon app ID")
|
|
|
|
mastodonAppSecret = flag.String("mastodon-app-secret", "", "Mastodon app secret")
|
|
|
|
mastodonToken = flag.String("mastodon-token", "", "Mastodon API token")
|
|
|
|
mastodonAccount = flag.String("mastodon-account", "", "Mastodon account")
|
|
|
|
)
|
|
|
|
|
2020-01-11 18:38:50 +00:00
|
|
|
func makeMastodon() (*madon.Client, error) {
|
|
|
|
c, err := madon.RestoreApp("mi", *mastodonInstance, *mastodonAppID, *mastodonAppSecret, &madon.UserToken{AccessToken: *mastodonToken})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *Mi) StreamMastodon(ctx context.Context) error {
|
|
|
|
evChan := make(chan madon.StreamEvent, 10)
|
|
|
|
stop := make(chan bool)
|
|
|
|
done := make(chan bool)
|
|
|
|
ctx = opname.With(context.Background(), "user-stream")
|
|
|
|
|
|
|
|
err := mi.mastodonClient.StreamListener("user", "", evChan, stop, done)
|
|
|
|
if err != nil {
|
|
|
|
ln.FatalErr(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ln.Log(ctx, ln.Info("streaming user toots to rethinkdb"))
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
ln.Fatal(ctx, ln.Action("got done?"))
|
|
|
|
case <-ctx.Done():
|
|
|
|
stop <- true
|
|
|
|
case ev := <-evChan:
|
|
|
|
s, ok := ev.Data.(madon.Status)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:18:59 +00:00
|
|
|
if s.Account.Acct != *mastodonAccount {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-11 18:38:50 +00:00
|
|
|
if s.Reblog != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Table("mastodon").Insert(s).Exec(mi.session)
|
|
|
|
if err != nil {
|
|
|
|
ln.Error(ctx, err)
|
|
|
|
stop <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
ln.Log(ctx, ln.Info("got toot"), ln.F{
|
|
|
|
"toot_url": s.URL,
|
|
|
|
"toot_creator": s.Account.Username,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NewStatus struct {
|
2020-01-14 22:37:35 +00:00
|
|
|
NewVal *madon.Status `json:"new_val"`
|
|
|
|
OldVal *madon.Status `json:"old_val"`
|
2020-01-11 18:38:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:18:59 +00:00
|
|
|
func (mi *Mi) PushMastodon(ctx context.Context, p Post) error {
|
|
|
|
_, err := mi.mastodonClient.PostStatus(madon.PostStatusParams{
|
|
|
|
Text: p.Format(),
|
|
|
|
Visibility: "public",
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-11 18:38:50 +00:00
|
|
|
func (mi *Mi) StreamMastodonToTwitter(ctx context.Context) {
|
|
|
|
res, err := r.Table("mastodon").Changes().Run(mi.session)
|
|
|
|
if err != nil {
|
|
|
|
ln.FatalErr(ctx, err)
|
|
|
|
}
|
|
|
|
defer res.Close()
|
|
|
|
|
|
|
|
ln.Log(ctx, ln.Info("streaming mastodon to twitter"))
|
|
|
|
|
|
|
|
for {
|
|
|
|
data, ok := res.NextResponse()
|
|
|
|
if !ok {
|
|
|
|
ln.FatalErr(ctx, res.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var ns NewStatus
|
|
|
|
err := json.Unmarshal(data, &ns)
|
|
|
|
if err != nil {
|
|
|
|
ln.FatalErr(ctx, err)
|
|
|
|
}
|
2020-01-11 22:17:06 +00:00
|
|
|
|
|
|
|
if ns.NewVal == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-11 18:38:50 +00:00
|
|
|
st := ns.NewVal
|
|
|
|
|
2020-01-14 22:37:35 +00:00
|
|
|
if st.Visibility != "public" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-11 22:17:06 +00:00
|
|
|
if st.Application.Name == "mi_irl" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:06:44 +00:00
|
|
|
if st.Sensitive {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-11 18:38:50 +00:00
|
|
|
if st.Account.Acct != *mastodonAccount {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if st.InReplyToID != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err := html2text.FromString(st.Content, html2text.Options{OmitLinks: true})
|
|
|
|
if len(text) > 200 {
|
|
|
|
text = text[:200] + "... read more here: " + st.URL
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ln.Error(ctx, err, ln.F{"url": st.URL})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tweet, _, err := mi.twitterClient.Statuses.Update(text, nil)
|
|
|
|
if err != nil {
|
|
|
|
ln.Error(ctx, err, ln.F{
|
|
|
|
"text": text,
|
|
|
|
"url": st.URL,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ln.Log(ctx, ln.Info("sent tweet from mastodon"), ln.F{
|
|
|
|
"tweet_id": tweet.ID,
|
|
|
|
})
|
2020-01-11 20:18:59 +00:00
|
|
|
|
|
|
|
err = r.Table("tweets").Insert(tweet).Exec(mi.session)
|
|
|
|
if err != nil {
|
|
|
|
ln.Error(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-01-11 18:38:50 +00:00
|
|
|
}
|
|
|
|
}
|