Skip to content

Instantly share code, notes, and snippets.

@yanbe
Created July 20, 2010 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanbe/482798 to your computer and use it in GitHub Desktop.
Save yanbe/482798 to your computer and use it in GitHub Desktop.
Reads ChirpUserStreams continuously, format chunks, then put them into standard output.
/*
userstreams.go: Reads ChirpUserStreams continuously, format tjem, then put them
into standard output. For detail, read:http://apiwiki.twitter.com/ChirpUserStreams .
Author: Yusuke Yanbe (y.yanbe@gmail.com)
*/
package main
import (
"bufio"
"fmt"
"http"
"json"
"strings"
)
const UserName = ""
const Password = ""
type Chunk struct {
Text, Event string
User, Source, Target struct { Screen_name string }
Target_object struct { Text, Full_name string }
Delete struct { Status struct { Id, User_id int } }
}
func main() {
var EndpointURL = fmt.Sprintf(
"http://%s:%s@betastream.twitter.com/2b/user.json", UserName, Password)
r, _, err := http.Get(EndpointURL)
if err==nil {
if r.StatusCode==http.StatusOK {
buf := bufio.NewReader(r.Body)
buf.ReadBytes('\n') // skip friendship graph data
buf.ReadBytes('\n') // and blank line
for {
b, _ := buf.ReadBytes('\n')
var chunk Chunk
ok, errtok := json.Unmarshal(string(b), &chunk)
if ok {
if len(chunk.Text)>0 {
fmt.Printf("%s: %s\n", chunk.User.Screen_name,
chunk.Text)
} else if len(chunk.Event)>0 {
fmt.Printf("%s %s %s", chunk.Source.Screen_name,
chunk.Event, chunk.Target.Screen_name)
if len(chunk.Target_object.Text)>0 {
fmt.Printf(" \"%s\"", chunk.Target_object.Text)
} else if (strings.HasPrefix(chunk.Event, "list_")) {
fmt.Printf("on %s", chunk.Target_object.Full_name)
}
fmt.Println()
} else if chunk.Delete.Status.Id>0 {
fmt.Printf("user %d deleted status %d\n",
chunk.Delete.Status.User_id, chunk.Delete.Status.Id)
} else {
fmt.Printf("unknown chunk: %s\n", string(b))
}
} else {
fmt.Printf("%s errtok: %s", string(b), errtok)
}
}
} else {
fmt.Println("%d", r.StatusCode)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment