add ErrorEvent

This commit is contained in:
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,46 +299,58 @@ 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()
name := ""
s := bufio.NewScanner(resp.Body) for {
for s.Scan() { req, err := http.NewRequest("GET", url.String(), nil)
line := s.Text() if err == nil {
token := strings.SplitN(line, ":", 2) req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
if len(token) != 2 { resp, err = c.Do(req)
continue
} }
switch strings.TrimSpace(token[0]) { if err == nil {
case "event": name := ""
name = strings.TrimSpace(token[1]) s := bufio.NewScanner(io.TeeReader(resp.Body, os.Stdout))
case "data": for s.Scan() {
switch name { line := s.Text()
case "update": token := strings.SplitN(line, ":", 2)
var status Status if len(token) != 2 {
json.Unmarshal([]byte(token[1]), &status) continue
q <- &UpdateEvent{&status} }
case "notification": switch strings.TrimSpace(token[0]) {
case "delete": case "event":
name = strings.TrimSpace(token[1])
case "data":
switch name {
case "update":
var status Status
json.Unmarshal([]byte(token[1]), &status)
q <- &UpdateEvent{&status}
case "notification":
case "delete":
}
default:
}
} }
resp.Body.Close()
err = ctx.Err()
if err == nil {
break
}
} else {
q <- &ErrorEvent{err}
} }
time.Sleep(3 * time.Second)
} }
fmt.Println(s.Err())
}() }()
go func() { go func() {
<-ctx.Done() <-ctx.Done()
resp.Body.Close() if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}() }()
return q, nil return q, nil
} }