Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@tiechel

go-twitterでUserStreamingを取得する

More than 3 years have 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")
}

2
Help us understand the problem. What is going on with this article?
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
tiechel
社内アプリばかりつくっている。
qiitadon
Qiitadon(β)から生まれた Qiita ユーザー・コミュニティです。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?