fix stream types

close #25
This commit is contained in:
Yasuhiro Matsumoto 2017-04-19 16:13:07 +09:00
parent e47dd5a618
commit b5541c9e38
3 changed files with 40 additions and 10 deletions

View File

@ -3,9 +3,11 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/mattn/go-mastodon" "github.com/mattn/go-mastodon"
@ -27,7 +29,22 @@ func cmdStream(c *cli.Context) error {
defer cancel() defer cancel()
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt) signal.Notify(sc, os.Interrupt)
q, err := client.StreamingPublic(ctx)
var q chan mastodon.Event
var err error
t := c.String("type")
if t == "public" {
q, err = client.StreamingPublic(ctx)
} else if t == "" || t == "public/local" {
q, err = client.StreamingPublicLocal(ctx)
} else if strings.HasPrefix(t, "user:") {
q, err = client.StreamingUser(ctx, t[5:])
} else if strings.HasPrefix(t, "hashtag:") {
q, err = client.StreamingHashtag(ctx, t[8:])
} else {
return errors.New("invalid type")
}
if err != nil { if err != nil {
return err return err
} }

View File

@ -190,6 +190,10 @@ func makeApp() *cli.App {
Name: "json", Name: "json",
Usage: "output JSON", Usage: "output JSON",
}, },
cli.StringFlag{
Name: "type",
Usage: "straem type (public,public/local,user:NAME,hashtag:TAG)",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "simplejson", Name: "simplejson",
Usage: "output simple JSON", Usage: "output simple JSON",

View File

@ -69,15 +69,13 @@ func handleReader(ctx context.Context, q chan Event, r io.Reader) error {
return ctx.Err() return ctx.Err()
} }
func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Event, error) { func (c *Client) streaming(ctx context.Context, p string, params url.Values) (chan Event, error) {
u, err := url.Parse(c.config.Server) u, err := url.Parse(c.config.Server)
if err != nil { if err != nil {
return nil, err return nil, err
} }
u.Path = path.Join(u.Path, "/api/v1/streaming/"+p) u.Path = path.Join(u.Path, "/api/v1/streaming/"+p)
params := url.Values{}
params.Set("tag", tag)
var resp *http.Response var resp *http.Response
q := make(chan Event, 10) q := make(chan Event, 10)
@ -86,7 +84,7 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even
for { for {
var in io.Reader var in io.Reader
if tag != "" { if params != nil {
in = strings.NewReader(params.Encode()) in = strings.NewReader(params.Encode())
} }
req, err := http.NewRequest(http.MethodGet, u.String(), in) req, err := http.NewRequest(http.MethodGet, u.String(), in)
@ -121,15 +119,26 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even
// StreamingPublic return channel to read events on public. // StreamingPublic return channel to read events on public.
func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) { func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "public", "") params := url.Values{}
return c.streaming(ctx, "public", params)
} }
// StreamingHome return channel to read events on home. // StreamingPublicLocal return channel to read events on public.
func (c *Client) StreamingHome(ctx context.Context) (chan Event, error) { func (c *Client) StreamingPublicLocal(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "home", "") params := url.Values{}
return c.streaming(ctx, "public/local", params)
}
// StreamingUser return channel to read events on home.
func (c *Client) StreamingUser(ctx context.Context, user string) (chan Event, error) {
params := url.Values{}
params.Set("user", user)
return c.streaming(ctx, "user", params)
} }
// StreamingHashtag return channel to read events on tagged timeline. // StreamingHashtag return channel to read events on tagged timeline.
func (c *Client) StreamingHashtag(ctx context.Context, tag string) (chan Event, error) { func (c *Client) StreamingHashtag(ctx context.Context, tag string) (chan Event, error) {
return c.streaming(ctx, "hashtag", tag) params := url.Values{}
params.Set("tag", tag)
return c.streaming(ctx, "hashtag", params)
} }