-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharss.go
More file actions
258 lines (227 loc) · 8.68 KB
/
arss.go
File metadata and controls
258 lines (227 loc) · 8.68 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/3ft9/GoOse"
zmq "github.com/pebbe/zmq4"
"html/template"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
)
var DEBUG bool
var STDOUT_OUTPUT bool
var ZMQ_PUB_ADDRESS string
var HTTP_PORT int
var STATIC_PATH string
var RECENT_ITEM_COUNT int
var STATE_FILENAME string
var STATE_SAVE_FREQUENCY int
var CONNECT_API_KEY string
var CONNECT_PROJECT_ID string
var CONNECT_COLLECTION string
var state *State
var urlCh chan string
var stdoutEmitterCh chan *goose.Article
var zmqEmitterCh chan *goose.Article
type TemplateData struct {
Message string
State *State
}
func displayTemplate(w http.ResponseWriter, data *TemplateData, tableonly bool) {
w.Header().Add("Content-Type", "text/html")
if !tableonly {
fmt.Fprintf(w, "<!DOCTYPE html>\n")
fmt.Fprintf(w, "<html>\n")
fmt.Fprintf(w, " <head>\n")
fmt.Fprintf(w, " <title>ARSS</title>\n")
fmt.Fprintf(w, " <link href=\"https://cdn.getconnect.io/connect-js/0.3.1/standalone/connect-all.min.css\" rel=\"stylesheet\" />\n")
fmt.Fprintf(w, " <link href=\"/static/style.css\" rel=\"stylesheet\" />\n")
fmt.Fprintf(w, " </head>\n")
fmt.Fprintf(w, " <body>\n")
fmt.Fprintf(w, " <a href=\"https://github.com/3ft9/arss\"><img style=\"position: absolute; top: 0; right: 0; border: 0;\" src=\"https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67\" alt=\"Fork me on GitHub\" data-canonical-src=\"https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png\"></a>")
fmt.Fprintf(w, " <h1>ARSS</h1>\n")
fmt.Fprintf(w, " <p>%s</p>\n", template.HTMLEscapeString(data.Message))
fmt.Fprintf(w, " <form method=\"post\" action=\"/\">\n")
fmt.Fprintf(w, " <input type=\"text\" name=\"url\" size=\"60\" />\n")
fmt.Fprintf(w, " <input type=\"submit\" name=\"op\" value=\"Subscribe\" />\n")
fmt.Fprintf(w, " </form>\n")
fmt.Fprintf(w, " <h2>Feeds</h2>\n")
fmt.Fprintf(w, " <div id=\"feedstable\">\n")
}
if len(data.State.Feeds) == 0 {
fmt.Fprintf(w, " <p>None, yet.</p>\n")
} else {
fmt.Fprintf(w, " <table>\n")
fmt.Fprintf(w, " <tr>\n")
fmt.Fprintf(w, " <th>Name</th>\n")
fmt.Fprintf(w, " <th>URL</th>\n")
fmt.Fprintf(w, " <th>Count</th>\n")
fmt.Fprintf(w, " <th align=\"right\">Check due</th>\n")
fmt.Fprintf(w, " </tr>\n")
now := time.Now().Unix()
var keys []string
data.State.Lock()
for k, v := range data.State.Feeds {
key := ""
if len(v.Object.Channels) > 0 {
key += v.Object.Channels[0].Title
}
key += "&|&|&|&|&" + k
keys = append(keys, key)
}
data.State.Unlock()
sort.Strings(keys)
var totalcount int64 = 0
for _, url := range keys {
url = strings.Split(url, "&|&|&|&|&")[1]
totalcount += data.State.Feeds[url].ArticleCount
fmt.Fprintf(w, " <tr>\n")
if len(data.State.Feeds[url].Object.Channels) > 0 {
fmt.Fprintf(w, " <td>%s</td>\n", template.HTMLEscapeString(data.State.Feeds[url].Object.Channels[0].Title))
} else {
fmt.Fprintf(w, " <td>-</td>\n")
}
fmt.Fprintf(w, " <td>%s</td>\n", template.HTMLEscapeString(url))
fmt.Fprintf(w, " <td align=\"right\">%d</td>\n", data.State.Feeds[url].ArticleCount)
due := data.State.Feeds[url].CheckDueAt - now
// if due < 5 {
// fmt.Fprintf(w, " <td align=\"right\">due now</td>\n")
// } else {
fmt.Fprintf(w, " <td align=\"right\">%d secs</td>\n", due)
// }
fmt.Fprintf(w, " <td><form method=\"post\" action=\"/\"><input type=\"hidden\" name=\"url\" value=\"%s\" /><input type=\"submit\" name=\"op\" value=\"Unsubscribe\" /></form></td>\n", template.HTMLEscapeString(url))
fmt.Fprintf(w, " </tr>\n")
}
fmt.Fprintf(w, " <tr>\n")
fmt.Fprintf(w, " <td colspan=\"2\" style=\"border-left: none; border-bottom: none; font-weight: bold;\" align=\"right\">Total:</td>\n")
fmt.Fprintf(w, " <td align=\"right\">%d</td>\n", totalcount)
fmt.Fprintf(w, " </tr>\n")
fmt.Fprintf(w, " </table>\n")
}
fmt.Fprintf(w, " <h2>Recent Items</h2>")
fmt.Fprintf(w, " <ul>")
items := []string{}
data.State.Lock()
data.State.Recent.Do(func(ringitem interface{}) {
if ringitem != nil {
item := ringitem.(RecentItem)
items = append(items, fmt.Sprintf(" <li><a href=\"%s\" target=\"_blank\"\">%s: %s</a></li>", template.HTMLEscapeString(item.Url), template.HTMLEscapeString(item.Source), template.HTMLEscapeString(item.Title)))
}
})
data.State.Unlock()
if len(items) == 0 {
fmt.Fprintf(w, " <li>None, yet.</li>")
} else {
for idx := len(items) - 1; idx >= 0; idx-- {
fmt.Fprintf(w, items[idx])
}
}
fmt.Fprintf(w, " </ul>")
if !tableonly {
fmt.Fprintf(w, " </div>\n")
fmt.Fprintf(w, " <script src=\"https://code.jquery.com/jquery-1.11.1.min.js\"></script>\n")
fmt.Fprintf(w, " <script src=\"https://cdn.getconnect.io/connect-js/0.3.1/standalone/connect-all.min.js\"></script>\n")
fmt.Fprintf(w, " <script src=\"/static/script.js\"></script>\n")
fmt.Fprintf(w, " </body>\n")
fmt.Fprintf(w, "</html>\n")
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
msg := "Welcome to the Automated RSS service."
if r.Method == "POST" {
url := r.PostFormValue("url")
op := r.PostFormValue("op")
if len(url) == 0 {
msg = "Please specify a URL!"
} else {
if op == "Subscribe" {
state.Subscribe(url)
http.Redirect(w, r, "/", 302)
} else if op == "Unsubscribe" {
state.Unsubscribe(url)
http.Redirect(w, r, "/", 302)
} else {
msg = "Unknown operation: [" + op + "]"
}
}
}
displayTemplate(w, &TemplateData{Message: msg, State: state}, r.URL.Query().Get("ajax") == "1")
}
func articleProcessor() {
g := goose.New()
for url := range urlCh {
article := g.ExtractFromUrl(url)
article.RawHtml = ""
if STDOUT_OUTPUT {
stdoutEmitterCh <- article
}
if len(ZMQ_PUB_ADDRESS) > 0 {
zmqEmitterCh <- article
}
}
}
func stdoutEmitter() {
e := json.NewEncoder(os.Stdout)
for article := range stdoutEmitterCh {
e.Encode(article)
}
}
func zmqEmitter() {
publisher, _ := zmq.NewSocket(zmq.PUB)
defer publisher.Close()
publisher.Bind(ZMQ_PUB_ADDRESS)
for article := range zmqEmitterCh {
output, err := json.Marshal(article)
if err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
} else {
publisher.Send(string(output), 0)
}
}
}
func main() {
flag.BoolVar(&DEBUG, "debug", false, "Enable debugging output")
flag.BoolVar(&STDOUT_OUTPUT, "stdout", false, "Output to stdout")
flag.StringVar(&ZMQ_PUB_ADDRESS, "zmq_pub_address", "", "ZMQ publish address, default is none (disabled)")
flag.IntVar(&HTTP_PORT, "port", 8080, "HTTP server port")
flag.StringVar(&STATIC_PATH, "static_path", "static", "Path to the static file folder")
flag.IntVar(&RECENT_ITEM_COUNT, "recent", 20, "Number of recent items to display")
flag.StringVar(&STATE_FILENAME, "state_filename", "arss.state", "Filename in which to store the state")
flag.IntVar(&STATE_SAVE_FREQUENCY, "state_frequency", 60, "Frequency with which the state is saved (seconds)")
flag.StringVar(&CONNECT_API_KEY, "connect_api_key", "", "API key for the Connect stats collection service, default is none (disabled)")
flag.StringVar(&CONNECT_PROJECT_ID, "connect_project_id", "", "Project ID for the Connect stats collection service")
flag.StringVar(&CONNECT_COLLECTION, "connect_collection", "", "Collection for the Connect stats collection service")
flag.Parse()
// Single stdoutEmitter to control access.
if STDOUT_OUTPUT {
stdoutEmitterCh = make(chan *goose.Article, 1000)
go stdoutEmitter()
}
// Single ZMQ to control access.
if len(ZMQ_PUB_ADDRESS) > 0 {
zmqEmitterCh = make(chan *goose.Article, 1000)
go zmqEmitter()
}
urlCh = make(chan string, 1000)
for i := 0; i < 4; i++ {
go articleProcessor()
}
state = NewState(RECENT_ITEM_COUNT, STATE_FILENAME, STATE_SAVE_FREQUENCY)
// State.Subscribe("http://feeds.bbci.co.uk/news/rss.xml")
// State.Subscribe("http://feeds.mashable.com/Mashable")
// State.Subscribe("http://feeds2.feedburner.com/techradar/allnews")
// State.Subscribe("http://feeds.feedburner.com/TechCrunch/")
// State.Subscribe("http://www.cnet.com/rss/news/")
// State.Subscribe("http://www.computerweekly.com/rss/All-Computer-Weekly-content.xml")
// State.Subscribe("http://www.zdnet.com/news/rss.xml")
// State.Subscribe("http://feeds.wired.com/wired/index")
go feedDispatcher(state, 4, 10)
http.HandleFunc("/", indexHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(STATIC_PATH))))
http.ListenAndServe(":"+strconv.Itoa(HTTP_PORT), nil)
}