-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
217 lines (182 loc) · 5.58 KB
/
main.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package bots
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/pkg/errors"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/delay"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
// TelegramAPIBase is the API base of telegram API.
const TelegramAPIBase = `https://api.telegram.org/`
// BatchSize is the number of top stories to fetch from Hacker News.
const BatchSize = 30
// NumCommentsThreshold is the threshold for number of comments. Story with less
// than this threshold will not be posted in the channel.
const NumCommentsThreshold = 5
// ScoreThreshold is the threshold for the score. Story with less than this
// threshold will not be posted in the channel.
const ScoreThreshold = 50
// DefaultTimeout is the default URLFetch timeout.
const DefaultTimeout = 9 * time.Minute
// DefaultChatID is the default chat ID.
const DefaultChatID = `@yahnc`
func loge(ctx context.Context, err error) {
log.Errorf(ctx, "%+v", err)
}
var editMessageFunc = delay.Func("editMessage", func(ctx context.Context, itemID int64, messageID int64) {
log.Infof(ctx, "editing message: id %d, message id %d", itemID, messageID)
story := Story{ID: itemID, MessageID: messageID}
err := story.EditMessage(ctx)
if err != nil {
if errors.Cause(err) != ErrIgnoredItem {
loge(ctx, err)
}
return
}
key := GetKey(ctx, itemID)
if _, err := datastore.Put(ctx, key, &story); err != nil {
loge(ctx, err)
}
})
var sendMessageFunc = delay.Func("sendMessage", func(ctx context.Context, itemID int64) {
log.Infof(ctx, "sending message: id %d", itemID)
story := Story{ID: itemID}
err := story.SendMessage(ctx)
if err != nil {
if errors.Cause(err) != ErrIgnoredItem {
loge(ctx, err)
}
return
}
key := GetKey(ctx, itemID)
if _, err := datastore.Put(ctx, key, &story); err != nil {
loge(ctx, err)
}
})
var deleteMessageFunc = delay.Func("deleteMessage", func(ctx context.Context, itemID int64, messageID int64) {
log.Infof(ctx, "deleting message: id %d, message id %d", itemID, messageID)
story := Story{ID: itemID, MessageID: messageID}
if err := story.DeleteMessage(ctx); err != nil {
loge(ctx, err)
}
})
func init() {
http.HandleFunc("/poll", handler)
http.HandleFunc("/cleanup", cleanUpHandler)
}
// TelegramAPI is a helper function to get the Telegram API endpoint.
func TelegramAPI(method string) string {
return TelegramAPIBase + os.Getenv("BOT_KEY") + "/" + method
}
// NewsURL is a helper function to get the URL to the story's HackerNews page.
func NewsURL(id int64) string {
return `https://news.ycombinator.com/item?id=` + strconv.FormatInt(id, 10)
}
// ItemURL is a helper function to get the API of an item.
func ItemURL(id int64) string {
return fmt.Sprintf(`https://hacker-news.firebaseio.com/v0/item/%d.json`, id)
}
// GetTopStoryURL is a helper function to get the
func GetTopStoryURL() string {
return fmt.Sprintf(`https://hacker-news.firebaseio.com/v0/topstories.json?orderBy="$key"&limitToFirst=%d`, BatchSize)
}
// GetKey get a datastore key for the given item ID.
func GetKey(ctx context.Context, i int64) *datastore.Key {
root := datastore.NewKey(ctx, "TopStory", "Root", 0, nil)
return datastore.NewKey(ctx, "Story", "", i, root)
}
func handler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
topStories, err := getTopStories(ctx, BatchSize)
if err != nil {
loge(ctx, err)
return
}
var keys []*datastore.Key
for _, story := range topStories {
keys = append(keys, GetKey(ctx, story))
}
savedStories := make([]Story, BatchSize, BatchSize)
err = datastore.GetMulti(ctx, keys, savedStories)
var wg sync.WaitGroup
defer wg.Wait()
if err == nil {
log.Infof(ctx, "no unknown news")
wg.Add(len(keys))
for i, key := range keys {
go func(id, messageID int64) {
defer wg.Done()
editMessageFunc.Call(ctx, id, messageID)
}(key.IntID(), savedStories[i].MessageID)
}
return
}
multiErr, ok := err.(appengine.MultiError)
if !ok {
log.Debugf(ctx, "%v", errors.Wrap(err, "in func handler() from datastore.GetMulti()"))
return
}
for i, err := range multiErr {
switch {
case err == nil:
wg.Add(1)
go func(id, messageID int64) {
defer wg.Done()
editMessageFunc.Call(ctx, id, messageID)
}(keys[i].IntID(), savedStories[i].MessageID)
case err == datastore.ErrNoSuchEntity:
wg.Add(1)
go func(id int64) {
defer wg.Done()
sendMessageFunc.Call(ctx, id)
}(keys[i].IntID())
default:
loge(ctx, err)
}
}
}
func getTopStories(ctx context.Context, limit int) ([]int64, error) {
resp, err := myHTTPClient(ctx).Get(GetTopStoryURL())
if err != nil {
return nil, errors.Wrap(err, "getTopStories -> http.Client.Get")
}
defer resp.Body.Close()
var ret []int64
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
return nil, errors.Wrap(err, "in getTopStories from json.Decoder.Decode()")
}
return ret, nil
}
func myHTTPClient(ctx context.Context) *http.Client {
withTimeout, _ := context.WithTimeout(ctx, DefaultTimeout)
return urlfetch.Client(withTimeout)
}
func cleanUpHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
var allStories []Story
now := time.Now()
oneDayAgo := now.Add(-24 * time.Hour)
_, err := datastore.NewQuery("Story").Filter("LastSave <=", oneDayAgo).GetAll(ctx, &allStories)
if err != nil {
loge(ctx, err)
return
}
var wg sync.WaitGroup
defer wg.Wait()
for _, story := range allStories {
wg.Add(1)
go func(id, messageID int64) {
defer wg.Done()
deleteMessageFunc.Call(ctx, id, messageID)
}(story.ID, story.MessageID)
}
}