fix stream types

close #25
pull/28/head
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 (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
@ -27,7 +29,22 @@ func cmdStream(c *cli.Context) error {
defer cancel()
sc := make(chan os.Signal, 1)
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 {
return err
}

View File

@ -190,6 +190,10 @@ func makeApp() *cli.App {
Name: "json",
Usage: "output JSON",
},
cli.StringFlag{
Name: "type",
Usage: "straem type (public,public/local,user:NAME,hashtag:TAG)",
},
cli.BoolFlag{
Name: "simplejson",
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()
}
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)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, "/api/v1/streaming/"+p)
params := url.Values{}
params.Set("tag", tag)
var resp *http.Response
q := make(chan Event, 10)
@ -86,7 +84,7 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even
for {
var in io.Reader
if tag != "" {
if params != nil {
in = strings.NewReader(params.Encode())
}
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.
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.
func (c *Client) StreamingHome(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "home", "")
// StreamingPublicLocal return channel to read events on public.
func (c *Client) StreamingPublicLocal(ctx context.Context) (chan Event, error) {
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.
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)
}