add -t flag to toot!

pull/2/head
Yasuhiro Matsumoto 2017-04-14 02:17:05 +09:00
parent 3de719ac60
commit a55b0a3d31
1 changed files with 22 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -17,7 +18,9 @@ import (
"golang.org/x/net/html" "golang.org/x/net/html"
) )
var blockTags = []string{"div", "br", "p", "blockquote", "pre", "h1", "h2", "h3", "h4", "h5", "h6"} var (
toot = flag.String("t", "", "toot text")
)
func extractText(node *html.Node, w *bytes.Buffer) { func extractText(node *html.Node, w *bytes.Buffer) {
if node.Type == html.TextNode { if node.Type == html.TextNode {
@ -33,14 +36,6 @@ func extractText(node *html.Node, w *bytes.Buffer) {
for c := node.FirstChild; c != nil; c = c.NextSibling { for c := node.FirstChild; c != nil; c = c.NextSibling {
extractText(c, w) extractText(c, w)
} }
if node.Type == html.ElementNode {
for _, bt := range blockTags {
if strings.ToLower(node.Data) == bt {
w.WriteString("\n")
break
}
}
}
} }
func prompt() (string, string, error) { func prompt() (string, string, error) {
@ -67,7 +62,7 @@ func prompt() (string, string, error) {
func getConfig() (string, *mastodon.Config, error) { func getConfig() (string, *mastodon.Config, error) {
dir := os.Getenv("HOME") dir := os.Getenv("HOME")
if dir == "" && runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
dir = os.Getenv("APPDATA") dir = os.Getenv("APPDATA")
if dir == "" { if dir == "" {
dir = filepath.Join(os.Getenv("USERPROFILE"), "Application Data", "mstdn") dir = filepath.Join(os.Getenv("USERPROFILE"), "Application Data", "mstdn")
@ -86,8 +81,8 @@ func getConfig() (string, *mastodon.Config, error) {
} }
config := &mastodon.Config{ config := &mastodon.Config{
Server: "https://mstdn.jp", Server: "https://mstdn.jp",
ClientID: "654a15390204e70d74f8d9264526e017e26d323e20e3f983409c157115009862", ClientID: "7d1873f3940af3e9128c81d5a2ddb3f235ccfa1cd11761efd3b8426f40898fe8",
ClientSecret: "17274242a0846ebadcdda77727666c9d475f1989b56ad9bd959021f62f92a84c", ClientSecret: "3c8ea997c580f196453e97c1c58f6f5c131f668456bbe1ed37aaccac719397db",
} }
if err == nil { if err == nil {
err = json.Unmarshal(b, &config) err = json.Unmarshal(b, &config)
@ -99,19 +94,21 @@ func getConfig() (string, *mastodon.Config, error) {
} }
func main() { func main() {
flag.Parse()
file, config, err := getConfig() file, config, err := getConfig()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
c := mastodon.NewClient(config) client := mastodon.NewClient(config)
if config.AccessToken == "" { if config.AccessToken == "" {
email, password, err := prompt() email, password, err := prompt()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = c.Authenticate(email, password) err = client.Authenticate(email, password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -126,7 +123,17 @@ func main() {
return return
} }
timeline, err := c.GetTimeline("/api/v1/timelines/home") if *toot != "" {
_, err = client.PostStatus(&mastodon.Toot{
Status: *toot,
})
if err != nil {
log.Fatal(err)
}
return
}
timeline, err := client.GetTimelineHome()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }