ref: 78e3bc4908c7b6c2d7b5e39ba485fb0f8e0113f7
dir: /mastodon.go/
package main
import (
"bytes"
"context"
"fmt"
"github.com/chzyer/readline"
"github.com/k3a/html2text"
"github.com/mattn/go-mastodon"
"golang.org/x/net/html"
"log"
"net/url"
"strings"
)
func ConfigureClient() *mastodon.Client {
appConfig := &mastodon.AppConfig{
Server: "https://eldritch.cafe",
ClientName: "hellclient",
Scopes: "read write follow",
Website: "https://github.com/mattn/go-mastodon",
RedirectURIs: "urn:ietf:wg:oauth:2.0:oob",
}
app, err := mastodon.RegisterApp(context.Background(), appConfig)
if err != nil {
log.Fatal(err)
}
// Have the user manually get the token and send it back to us
u, err := url.Parse(app.AuthURI)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Open your browser to \n%s\n and copy/paste the given authroization code\n", u)
var userAuthorizationCode string
fmt.Print("Paste the code here:")
fmt.Scanln(&userAuthorizationCode)
config := &mastodon.Config{
Server: "https://eldritch.cafe",
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
}
// Create the client
c := mastodon.NewClient(config)
// Exchange the User authentication code with an access token, that can be used to interact with the api on behalf of the user
err = c.GetUserAccessToken(context.Background(), userAuthorizationCode, app.RedirectURI)
if err != nil {
log.Fatal(err)
}
return c
}
func initClient(account *account) *mastodon.Client {
clientID := account.MASTODON_CLIENT_ID
clientSecret := account.MASTODON_CLIENT_ID
accessToken := account.MASTODON_ACCESS_TOKEN
url := account.URL
config := &mastodon.Config{
Server: url,
ClientID: clientID,
ClientSecret: clientSecret,
AccessToken: accessToken,
}
c := mastodon.NewClient(config)
return c
}
func renderStatus(content string) string {
doc, err := html.Parse(strings.NewReader(content))
if err != nil {
log.Fatal(err)
}
for node := range doc.Descendants() {
if node.Data == "a" {
for attr := range node.Attr {
if node.Attr[attr].Key == "class" && strings.Contains(node.Attr[attr].Val, "mention") {
node.Data = "span"
}
}
}
}
//Rip off the HTML body the parser made for us
for node := range doc.Descendants() {
if node.Data == "body" {
node.Type = html.DocumentNode
doc = node
}
}
var rendered bytes.Buffer
err = html.Render(&rendered, doc)
if err != nil {
log.Fatal(err)
}
return rendered.String()
}
func postReply(posttext string, account *account, client mastodon.Client, visibility string, replyto mastodon.ID) (status *mastodon.Status, err error){
toot := mastodon.Toot{
Status: posttext,
Visibility: visibility,
InReplyToID: replyto,
}
status, err = postStatusDetailed(posttext, account, client, visibility, toot)
return
}
func postStatus(posttext string, account *account, client mastodon.Client, visibility string) (status *mastodon.Status, err error) {
// Post a toot
toot := mastodon.Toot{
Status: posttext,
Visibility: visibility,
}
status, err = postStatusDetailed(posttext, account, client, visibility, toot)
return
}
func postStatusDetailed(posttext string, account *account, client mastodon.Client, visibility string, toot mastodon.Toot) (status *mastodon.Status, err error){
status, err = client.PostStatus(context.Background(), &toot)
if err != nil {
printMastodonErr(err)
return
}
return
}
func getUserString(post *mastodon.Status) string {
return post.Account.Acct
}
// Spaces before prefixes....
func formatReblog(post *mastodon.Status, index string) string {
reblogString := fmt.Sprintf(" <%v> Reblogged", post.Account.Username)
return formatStatusDetailed(post.Reblog, index, reblogString)
}
func formatFavorite(post *mastodon.Status, index string) string {
return fmt.Sprintf("\rFavorited: %v <%v> %v", index, post.Account.Username, html2text.HTML2Text(post.Content))
}
func formatStatus(post *mastodon.Status, index string) string {
return formatStatusDetailed(post, index, " ")
}
func formatStatusDetailed(post *mastodon.Status, index string, prefix string) string {
renderedPost := renderStatus(post.Content)
return fmt.Sprintf("%v%v <%v> %v", index, prefix, post.Account.Username, html2text.HTML2Text(renderedPost))
}
func formatEdit(post *mastodon.Status, index string) string {
return fmt.Sprintf("\r%v <%v> EDITED: %v", index, post.Account.Username, html2text.HTML2Text(post.Content))
}
func printMastodonErr(err error) {
fmt.Printf("\r%w\n", err)
}
func printPost(postref string, post *mastodon.Status) *mastodon.Status {
return printPostDetailed(postref, post, "")
}
func printPostDetailed(postref string, post *mastodon.Status, prefix string) *mastodon.Status {
post, plaintext := RenderPostPlaintext(post, postref, prefix)
fmt.Println(plaintext)
return post
}
func RenderPostPlaintext(post *mastodon.Status, postref string, prefix string) (selectedPost *mastodon.Status, plaintext string) {
poststring := ""
postfix := ""
var media []mastodon.Attachment
if post.Reblog != nil {
poststring = formatReblog(post, postref)
selectedPost = post.Reblog
media = post.Reblog.MediaAttachments
} else {
poststring = formatStatusDetailed(post, postref, prefix)
selectedPost = post
media = post.MediaAttachments
}
for _, _ = range media {
postfix = postfix + "🖼️"
}
plaintext = fmt.Sprintf("%v %v", poststring, postfix)
return
}
func StreamHomeTimeline(client *mastodon.Client, rl *readline.Instance, postMap map[string]*mastodon.Status) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
eventCh, err := client.StreamingUser(ctx)
if err != nil {
log.Fatalf("Error starting user stream: %v", err)
}
initReferenceSystem()
postref := "a"
plaintext := ""
idmap := make(map[mastodon.ID]*mastodon.Status)
// Enter a loop to continuously listen for events from the event channel.
for {
select {
case event, ok := <-eventCh: // Read from the event channel, checking 'ok' for closure
if !ok {
// The channel was closed, which indicates the stream has ended.
fmt.Println("Stream closed.")
return // Exit the function
}
switch post := event.(type) {
case *mastodon.UpdateEvent:
post.Status = printPost(postref, post.Status)
saveRef(postMap, post.Status, postref)
idmap[post.Status.ID] = post.Status
postref = IncrementString(postref)
case *mastodon.UpdateEditEvent:
saveRef(postMap, post.Status, postref)
fmt.Println(formatEdit(post.Status, postref))
postref = IncrementString(postref)
idmap[post.Status.ID] = post.Status
case *mastodon.DeleteEvent:
deleted, ok := idmap[post.ID]
//didn't have this in the cache
if(!ok) {
fmt.Printf("Deleted: ID %v", post.ID)
continue
}
printPostDetailed("", deleted, "Deleted:")
continue
case *mastodon.NotificationEvent:
if post.Notification.Status == nil {
fmt.Printf("Notification [%v] from <%v>\n", post.Notification.Type, post.Notification.Account.Acct)
continue
}
_, plaintext = RenderPostPlaintext(post.Notification.Status, postref, "")
fmt.Printf("Notification [%v] from <%v>: %v\n", post.Notification.Type, post.Notification.Account.Acct, plaintext)
saveRef(postMap, post.Notification.Status, postref)
postref = IncrementString(postref)
default:
// Catch any other unexpected event types.
fmt.Printf("Unhandled event: %T\n", post)
}
}
}
}