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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package main
import (
"code.google.com/p/go-html-transform/h5"
"code.google.com/p/go-html-transform/html/transform"
"code.google.com/p/go.net/html"
"code.google.com/p/go.tools/blog/atom"
"encoding/xml"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
)
func getattr(attrs []html.Attribute, name string) (val string, found bool) {
for _, a := range attrs {
if a.Key == name {
val = a.Val
found = true
return
}
}
return
}
func Textify(node *html.Node) string {
switch node.Type {
case html.TextNode:
return node.Data
case html.ElementNode:
for _, att := range node.Attr {
if att.Key == "alt" {
return att.Val
}
}
fallthrough
case html.DocumentNode:
text := ""
for n := node.FirstChild; n != nil; n = n.NextSibling {
text += Textify(n)
}
return text
default:
return ""
}
}
type Tweet struct {
Content string
From string
ID string
Date time.Time
}
func ScrapeTweets(r io.Reader) ([]Tweet, error) {
t, err := transform.NewFromReader(r)
if err != nil {
return nil, fmt.Errorf("Could not scrape profile: %s", err)
}
tweets := make([]Tweet, 0)
t.Apply(func(node *html.Node) {
var tweet Tweet
tweet.From, _ = getattr(node.Attr, "data-screen-name")
tweet.ID, _ = getattr(node.Attr, "data-item-id")
time_ok := false
tree := h5.NewTree(node)
t2 := transform.New(&tree)
t2.Apply(func(node *html.Node) {
if ts, ok := getattr(node.Attr, "data-time"); ok {
if ts_int, err := strconv.ParseInt(ts, 10, 64); err == nil {
tweet.Date = time.Unix(ts_int, 0)
time_ok = true
}
}
}, "a.ProfileTweet-timestamp span")
if !time_ok {
return
}
t2.Apply(func(node *html.Node) {
tweet.Content = Textify(node)
}, ".ProfileTweet-text")
tweets = append(tweets, tweet)
}, "div.GridTimeline .ProfileTweet")
return tweets, nil
}
const titlelimit = 80
func (t Tweet) Atomify() *atom.Entry {
entry := new(atom.Entry)
entry.Title = "@" + t.From + ": " + t.Content
if len([]rune(entry.Title)) > titlelimit {
entry.Title = string([]rune(entry.Title)[:titlelimit-2]) + " …"
}
url := "https://twitter.com/" + t.From + "/status/" + t.ID
entry.ID = url
entry.Link = []atom.Link{atom.Link{
Rel: "alternate",
Href: url,
}}
entry.Summary = &atom.Text{Type: "text", Body: t.Content}
entry.Content = &atom.Text{Type: "text", Body: t.Content}
entry.Author = &atom.Person{
Name: "@" + t.From,
URI: "https://twitter.com/" + t.From,
}
entry.Published = atom.Time(t.Date)
entry.Updated = atom.Time(t.Date)
return entry
}
func main() {
os.Exit(Main())
}
func Main() int {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Need one argument (twitter user name, without the '@')")
return 1
}
user := os.Args[1]
resp, err := http.Get("https://twitter.com/" + user)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't download @%s's stream: %s\n", user, err)
return 1
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "Couldn't download @%s's stream: HTTP Status %d %s\n", user, resp.StatusCode, resp.Status)
return 1
}
tweets, err := ScrapeTweets(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
feed := atom.Feed{
Title: "Tweets from @" + user,
ID: "https://twitter.com/" + user,
Link: []atom.Link{
atom.Link{
Rel: "alternate",
Href: "http://twitter.com/" + user,
},
},
Author: &atom.Person{
Name: "@" + user,
URI: "https://twitter.com/" + user,
},
}
var latest time.Time
for _, tweet := range tweets {
feed.Entry = append(feed.Entry, tweet.Atomify())
if tweet.Date.After(latest) {
latest = tweet.Date
}
}
feed.Updated = atom.Time(latest)
enc := xml.NewEncoder(os.Stdout)
if err := enc.Encode(feed); err != nil {
fmt.Fprintf(os.Stderr, "Could not encode feed: %s\n", err)
return 1
}
return 0
}
|