Help us understand the problem. What is going on with this article?

go-twitterでUserStreamingを取得する

More than 1 year has passed since last update.

go-twitterでUserStreamingを取得してみる

go-twitter: https://github.com/dghubble/go-twitter

package main

import (
    "fmt"
    "log"
    "reflect"
    "time"

    "github.com/dghubble/go-twitter/twitter"
    "github.com/dghubble/oauth1"
)

func main() {
    config := oauth1.NewConfig(
        "consumerKey",
        "consumerSecret",
    )
    token := oauth1.NewToken(
        "accessToken",
        "accessSecret",
    )


    httpClient := config.Client(oauth1.NoContext, token)

    client := twitter.NewClient(httpClient)

    params := &twitter.StreamUserParams{
        With:          "followings",
        StallWarnings: twitter.Bool(true),
    }

    stream, err := client.Streams.User(params)

    if err != nil {
        log.Fatal("Failed to start streaming.")
        return
    }

    for message := range stream.Messages {
        switch message := message.(type) {
        case *twitter.FriendsList:
            go funcFriendsList(message)
        case *twitter.Tweet:
            go printTweet(message)
        case *twitter.StatusDeletion:
            go funcStatusDeletion(message)
        case *twitter.Event:
            go funcEvent(message)
        default:
            log.Println("unknown type: ", reflect.TypeOf(message))
        }
    }

}

func funcFriendsList(friendsList *twitter.FriendsList) {
    // do something
    fmt.Println("FriendsList")
}

func printTweet(tweet *twitter.Tweet) {
    if tweet.RetweetedStatus != nil {
        tweet = tweet.RetweetedStatus
    }
    created_at, err := tweet.CreatedAtTime()
    if err != nil {
        log.Println("tweet.CreatedAtTime() retruns error.")
    }

    created_at = created_at.In(time.FixedZone("Asia/Tokyo", 9*60*60))
    fmt.Printf("%s % 6dRT % 6dFav %s\n", created_at.Format("2006-01-02 15:04:05"), tweet.RetweetCount, tweet.FavoriteCount, tweet.Text)
}

func funcEvent(event *twitter.Event) {
    // do something
    fmt.Println("Event")
}

func funcStatusDeletion(deletion *twitter.StatusDeletion) {
    // do something
    fmt.Println("StatusDeletion")
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away