/
favstar.go
143 lines (130 loc) · 2.62 KB
/
favstar.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package favstar
import (
"exp/html"
"io/ioutil"
"net/http"
"strings"
)
type cond map[string]string
func walk(n *html.Node, tag string, attr cond) (l []*html.Node) {
switch n.Type {
case html.ErrorNode:
return
case html.DocumentNode:
for c := n.FirstChild; c != nil; c = c.NextSibling {
l = walk(c, tag, attr)
}
return
case html.CommentNode:
return
case html.TextNode:
if tag == "TEXT" {
l = append(l, n)
} else {
return
}
default:
return
case html.ElementNode:
if strings.ToLower(tag) == strings.ToLower(n.Data) {
for _, e := range l {
if e == n {
return
}
}
if attr != nil && len(attr) > 0 {
for _, a := range n.Attr {
val, found := attr[a.Key]
if found {
for _, as := range strings.Split(a.Val, " ") {
if as == val {
l = append(l, n)
break
}
}
break
}
}
} else {
l = append(l, n)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
for _, f := range walk(c, tag, attr) {
for _, e := range l {
if e == f {
return
}
}
l = append(l, f)
}
}
}
return
}
func text(node *html.Node) (text string) {
for _, t := range walk(node, "TEXT", cond{}) {
text += strings.TrimSpace(t.Data)
}
return
}
func attr(node *html.Node, name string) (attr string) {
for _, a := range node.Attr {
if a.Key == name {
attr = a.Val
}
}
return
}
func isFav(id string) bool {
return strings.Index(id, "faved_by_others_") == 0
}
func isRt(id string) bool {
return strings.Index(id, "rt_by_others_") == 0
}
type Entry struct {
Text string
Fav []string
RT []string
}
type Favstar struct {
Entry []Entry
}
func Get(user_id string) (f Favstar, err error) {
res, err := http.Get("http://favstar.fm/users/" + user_id + "/recent")
if err != nil {
return
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
doc, err := html.Parse(strings.NewReader(string(b)))
if err != nil {
return
}
tweetWithStats := walk(doc, "div", cond{"class": "fs-tweet"})
for _, tweetWithStat := range tweetWithStats {
t := walk(tweetWithStat, "p", cond{"class": "fs-tweet-text"})
if t == nil {
continue
}
var e Entry
e.Text = t[0].FirstChild.Data
favs := walk(tweetWithStat, "div", cond{"data-type": "favs"})
if favs != nil {
for _, aa := range walk(favs[0], "a", nil) {
e.Fav = append(e.Fav, attr(aa, "title"))
}
}
rts := walk(tweetWithStat, "div", cond{"data-type": "rts"})
if rts != nil {
for _, aa := range walk(rts[0], "a", nil) {
e.RT = append(e.RT, attr(aa, "title"))
}
}
f.Entry = append(f.Entry, e)
}
return
}