Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main

import (
    "http";
    "io";
    "json";
    "fmt";
    "regexp"
)

type timeline struct {
    Statuses []tweet;
}

type tweet struct {
    Id int;
    Text string;
    User user;
}

var rep = regexp.MustCompile("\n");
func (t *tweet) Format() string {
    return t.User.Screen_Name + " - " + rep.ReplaceAllString(t.Text, " ");
}

type user struct {
    Screen_Name string;
}

func main() {
    // fetch data from twitter
    response, _, err := http.Get("http://twitter.com/statuses/public_timeline.json");
    if response.StatusCode != http.StatusOK || err != nil { 
        fmt.Println("Can't load public timeline"); 
        return;
    }
    
    // read the response    
    buffer, _ := io.ReadAll(response.Body);
    response.Body.Close();
    jsonstr := string(buffer);
    
    // apparently, top level arrays aren't permitted..
    jsonstr = "{\"statuses\":" + jsonstr + "}";
    
    // turn our json into a struct..
    var public timeline;
    ok, error := json.Unmarshal(jsonstr, &public);
    if !ok {
        fmt.Printf("There's an error with the JSON: %s", error);
        return;
    }
    
    // print the tweets..
    length := len(public.Statuses);
    
    for i := 0; i < length; i++ {
        tweet := public.Statuses[i];
        if "" != tweet.User.Screen_Name {
            fmt.Printf("%s\n", tweet.Format()); 
        }
    }
}