go-mastodon/cmd/mstdn/main.go

226 lines
4.5 KiB
Go
Raw Normal View History

2017-04-13 18:00:02 +02:00
package main
import (
"bufio"
"bytes"
2017-04-14 02:32:46 +02:00
"context"
2017-04-13 18:00:02 +02:00
"encoding/json"
2017-04-13 19:17:05 +02:00
"flag"
2017-04-13 18:00:02 +02:00
"fmt"
"io/ioutil"
"log"
"os"
2017-04-14 02:32:46 +02:00
"os/signal"
2017-04-13 18:00:02 +02:00
"path/filepath"
"runtime"
"strings"
2017-04-14 02:32:46 +02:00
"github.com/fatih/color"
2017-04-13 18:00:02 +02:00
"github.com/mattn/go-mastodon"
"github.com/mattn/go-tty"
"golang.org/x/net/html"
)
2017-04-13 19:17:05 +02:00
var (
toot = flag.String("t", "", "toot text")
stream = flag.Bool("S", false, "streaming public")
fromfile = flag.String("ff", "", "post utf-8 string from a file(\"-\" means STDIN)")
2017-04-13 19:17:05 +02:00
)
2017-04-13 18:00:02 +02:00
func readFile(filename string) ([]byte, error) {
if filename == "-" {
return ioutil.ReadAll(os.Stdin)
}
2017-04-14 05:23:45 +02:00
return ioutil.ReadFile(filename)
}
2017-04-14 02:32:46 +02:00
func textContent(s string) string {
doc, err := html.Parse(strings.NewReader(s))
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
2017-04-14 05:30:13 +02:00
var extractText func(node *html.Node, w *bytes.Buffer)
extractText = func(node *html.Node, w *bytes.Buffer) {
if node.Type == html.TextNode {
data := strings.Trim(node.Data, "\r\n")
if data != "" {
2017-04-14 06:53:56 +02:00
w.WriteString(data)
2017-04-14 05:30:13 +02:00
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
extractText(c, w)
}
2017-04-14 06:53:56 +02:00
if node.Type == html.ElementNode {
name := strings.ToLower(node.Data)
if name == "br" {
w.WriteString("\n")
}
}
2017-04-14 05:30:13 +02:00
}
2017-04-14 02:32:46 +02:00
extractText(doc, &buf)
return buf.String()
}
2017-04-13 18:00:02 +02:00
func prompt() (string, string, error) {
t, err := tty.Open()
if err != nil {
return "", "", err
}
defer t.Close()
fmt.Print("E-Mail: ")
b, _, err := bufio.NewReader(os.Stdin).ReadLine()
if err != nil {
return "", "", err
}
email := string(b)
fmt.Print("Password: ")
password, err := t.ReadPassword()
if err != nil {
return "", "", err
}
return email, password, nil
}
func getConfig() (string, *mastodon.Config, error) {
dir := os.Getenv("HOME")
2017-04-13 19:17:05 +02:00
if runtime.GOOS == "windows" {
2017-04-13 18:00:02 +02:00
dir = os.Getenv("APPDATA")
if dir == "" {
dir = filepath.Join(os.Getenv("USERPROFILE"), "Application Data", "mstdn")
}
dir = filepath.Join(dir, "mstdn")
} else {
dir = filepath.Join(dir, ".config", "mstdn")
}
if err := os.MkdirAll(dir, 0700); err != nil {
return "", nil, err
}
file := filepath.Join(dir, "settings.json")
b, err := ioutil.ReadFile(file)
if err != nil && !os.IsNotExist(err) {
return "", nil, err
}
config := &mastodon.Config{
Server: "https://mstdn.jp",
2017-04-13 19:17:05 +02:00
ClientID: "7d1873f3940af3e9128c81d5a2ddb3f235ccfa1cd11761efd3b8426f40898fe8",
ClientSecret: "3c8ea997c580f196453e97c1c58f6f5c131f668456bbe1ed37aaccac719397db",
2017-04-13 18:00:02 +02:00
}
if err == nil {
err = json.Unmarshal(b, &config)
if err != nil {
return "", nil, fmt.Errorf("could not unmarshal %v: %v", file, err)
}
}
return file, config, nil
}
2017-04-14 05:30:13 +02:00
func authenticate(client *mastodon.Client, config *mastodon.Config, file string) {
email, password, err := prompt()
if err != nil {
log.Fatal(err)
}
err = client.Authenticate(email, password)
if err != nil {
log.Fatal(err)
}
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
log.Fatal("failed to store file:", err)
}
err = ioutil.WriteFile(file, b, 0700)
if err != nil {
log.Fatal("failed to store file:", err)
}
}
2017-04-14 05:33:12 +02:00
func streaming(client *mastodon.Client) {
ctx, cancel := context.WithCancel(context.Background())
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
q, err := client.StreamingPublic(ctx)
if err != nil {
log.Fatal(err)
}
go func() {
<-sc
cancel()
close(q)
}()
for e := range q {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Println(t.Status.Account.Username)
color.Set(color.Reset)
fmt.Println(textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Println(t.Error())
color.Set(color.Reset)
}
}
}
func init() {
2017-04-13 19:17:05 +02:00
flag.Parse()
2017-04-14 05:33:12 +02:00
if *fromfile != "" {
text, err := readFile(*fromfile)
if err != nil {
log.Fatal(err)
}
*toot = string(text)
}
}
func post(client *mastodon.Client, text string) {
_, err := client.PostStatus(&mastodon.Toot{
Status: text,
})
if err != nil {
log.Fatal(err)
}
}
2017-04-13 19:17:05 +02:00
2017-04-14 05:34:13 +02:00
func timeline(client *mastodon.Client) {
timeline, err := client.GetTimelineHome()
if err != nil {
log.Fatal(err)
}
for _, t := range timeline {
color.Set(color.FgHiRed)
fmt.Println(t.Account.Username)
color.Set(color.Reset)
fmt.Println(textContent(t.Content))
}
}
2017-04-14 05:33:12 +02:00
func main() {
2017-04-13 18:00:02 +02:00
file, config, err := getConfig()
if err != nil {
log.Fatal(err)
}
2017-04-13 19:17:05 +02:00
client := mastodon.NewClient(config)
2017-04-13 18:00:02 +02:00
if config.AccessToken == "" {
2017-04-14 05:30:13 +02:00
authenticate(client, config, file)
2017-04-13 18:00:02 +02:00
return
}
2017-04-13 19:17:05 +02:00
if *toot != "" {
2017-04-14 05:33:12 +02:00
post(client, *toot)
2017-04-13 19:17:05 +02:00
return
}
2017-04-14 05:33:12 +02:00
2017-04-14 02:32:46 +02:00
if *stream {
2017-04-14 05:33:12 +02:00
streaming(client)
2017-04-14 02:32:46 +02:00
return
}
2017-04-13 19:17:05 +02:00
2017-04-14 05:34:13 +02:00
timeline(client)
2017-04-13 18:00:02 +02:00
}