add ErrorEvent

pull/4/head
Yasuhiro Matsumoto 2017-04-14 11:49:52 +09:00
parent 3646db025f
commit 7ef168e3dd
2 changed files with 59 additions and 36 deletions

View File

@ -30,11 +30,7 @@ func extractText(node *html.Node, w *bytes.Buffer) {
if node.Type == html.TextNode { if node.Type == html.TextNode {
data := strings.Trim(node.Data, "\r\n") data := strings.Trim(node.Data, "\r\n")
if data != "" { if data != "" {
w.WriteString(data) w.WriteString(data + "\n")
}
} else if node.Type == html.ElementNode {
if node.Data == "li" {
w.WriteString("\n* ")
} }
} }
for c := node.FirstChild; c != nil; c = c.NextSibling { for c := node.FirstChild; c != nil; c = c.NextSibling {
@ -166,6 +162,10 @@ func main() {
fmt.Println(t.Status.Account.Username) fmt.Println(t.Status.Account.Username)
color.Set(color.Reset) color.Set(color.Reset)
fmt.Println(textContent(t.Status.Content)) fmt.Println(textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Println(t.Error())
color.Set(color.Reset)
} }
} }
return return

View File

@ -5,8 +5,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path" "path"
"strings" "strings"
"time" "time"
@ -231,7 +233,9 @@ func (c *client) GetTimelineHome() ([]*Status, error) {
func (c *client) PostStatus(toot *Toot) (*Status, error) { func (c *client) PostStatus(toot *Toot) (*Status, error) {
params := url.Values{} params := url.Values{}
params.Set("status", toot.Status) params.Set("status", toot.Status)
//params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID)) if toot.InReplyToID > 0 {
params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID))
}
// TODO: media_ids, senstitive, spoiler_text, visibility // TODO: media_ids, senstitive, spoiler_text, visibility
//params.Set("visibility", "public") //params.Set("visibility", "public")
@ -277,6 +281,13 @@ type DeleteEvent struct {
func (e *DeleteEvent) event() {} func (e *DeleteEvent) event() {}
type ErrorEvent struct {
err error
}
func (e *ErrorEvent) Error() string { return e.err.Error() }
func (e *ErrorEvent) event() {}
type Event interface { type Event interface {
event() event()
} }
@ -288,21 +299,21 @@ func (c *client) StreamingPublic(ctx context.Context) (chan Event, error) {
} }
url.Path = path.Join(url.Path, "/api/v1/streaming/public") url.Path = path.Join(url.Path, "/api/v1/streaming/public")
req, err := http.NewRequest("GET", url.String(), nil) var resp *http.Response
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err := c.Do(req)
if err != nil {
return nil, err
}
q := make(chan Event) q := make(chan Event, 10)
go func() { go func() {
defer ctx.Done() defer ctx.Done()
for {
req, err := http.NewRequest("GET", url.String(), nil)
if err == nil {
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err = c.Do(req)
}
if err == nil {
name := "" name := ""
s := bufio.NewScanner(resp.Body) s := bufio.NewScanner(io.TeeReader(resp.Body, os.Stdout))
for s.Scan() { for s.Scan() {
line := s.Text() line := s.Text()
token := strings.SplitN(line, ":", 2) token := strings.SplitN(line, ":", 2)
@ -321,13 +332,25 @@ func (c *client) StreamingPublic(ctx context.Context) (chan Event, error) {
case "notification": case "notification":
case "delete": case "delete":
} }
default:
} }
} }
fmt.Println(s.Err()) resp.Body.Close()
err = ctx.Err()
if err == nil {
break
}
} else {
q <- &ErrorEvent{err}
}
time.Sleep(3 * time.Second)
}
}() }()
go func() { go func() {
<-ctx.Done() <-ctx.Done()
if resp != nil && resp.Body != nil {
resp.Body.Close() resp.Body.Close()
}
}() }()
return q, nil return q, nil
} }