-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathweb.coffee
230 lines (198 loc) · 8.53 KB
/
web.coffee
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
###
The MIT License (MIT)
Copyright (c) 2013 Michael Bertolacci
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
###
express = require 'express'
RSS = require 'rss'
moment = require 'moment'
_ = require 'lodash'
morgan = require 'morgan'
loremIpsum = require 'lorem-ipsum'
seedRandom = require 'seed-random'
crypto = require 'crypto'
app = express()
app.use morgan('combined')
units = {
second: {
nextUp: 'minute',
mustDivide: 60
}
minute: {
nextUp: 'hour'
mustDivide: 60
}
hour: {
nextUp: 'day'
mustDivide: 24
}
day: {
nextUp: 'year'
mustDivide: 1
}
month: {
nextUp: 'year'
mustDivide: 12
}
year: {
mustDivide: 1
}
}
getNearest = (interval, unit) ->
if interval == 1
return moment().utc().startOf(unit)
else
unitOptions = units[unit]
if unitOptions.mustDivide % interval != 0
throw "When using #{unit}s the interval must divide #{unitOptions.mustDivide}"
now = moment().utc()
returnDate = now.clone().startOf(unitOptions.nextUp || unit)
returnDate[unit](now[unit]() - now[unit]() % interval)
return returnDate
app.get '/', (request, response) ->
response.send """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Lorem RSS</title>
<meta name="description" content="Web service that generates lorem ipsum RSS feeds">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/4.2.3/css/normalize.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/4.2.3/css/foundation.min.css">
<style type="text/css">
ul.indent {
position: relative;
left: 20px;
}
</style>
</head>
<body>
<div class="row">
<div class="large-12 columns">
<h1>Lorem RSS</h1>
<p>
Generates RSS feeds with content updated at regular intervals. I wrote this to
answer a <a href="http://stackoverflow.com/questions/18202048/are-there-any-constantly-updating-rss-feed-services-to-use-for-testing-or-just">question I asked on Stack Overflow</a>.
</p>
<p>
The code for this service is <a href="https://github.com/mbertolacci/lorem-rss">available on GitHub</a>.
</p>
<h2>API</h2>
<p>
Visit <a href="/feed">/feed</a>, with the following optional parameters:
</p>
<ul class="disc indent">
<li>
<em>unit</em>: one of second, minute, day, month, or year
</li>
<li>
<em>interval</em>: an integer to repeat the units at.
For seconds and minutes this interval must evenly divide 60,
for month it must evenly divide 12, and for day and year it
can only be 1.
</li>
<li>
<em>length</em>: an integer that determines the number of items in the feed.
Must be greater or equal to 0 and smaller or equal to 1000. Defaults to 10 items.
</li>
</ul>
<h2>Examples</h2>
<ul class="disc indent">
<li>
The default, updates once a minute, with 10 entries: <a href="/feed">/feed</a>
</li>
<li>
Update every second instead of minute: <a href="/feed?unit=second">/feed?unit=second</a>
</li>
<li>
Update every 30 seconds: <a href="/feed?unit=second&interval=30">/feed?unit=second&interval=30</a>
</li>
<li>
Update once a day: <a href="/feed?unit=day">/feed?unit=day</a>
</li>
<li>
Update every 6 months: <a href="/feed?unit=month&interval=6">/feed?unit=month&interval=6</a>
</li>
<li>
Update once a year: <a href="/feed?unit=year">/feed?unit=year</a>
</li>
<li>
Default feed with 42 entries: <a href="/feed?length=42">/feed?length=42</a>
</li>
<li>
<strong>Invalid example:</strong>
update every 7 minutes (does not evenly divide 60):
<a href="/feed?unit=minute&interval=7">/feed?unit=minute&interval=7</a>
</li>
</ul>
<hr/>
<p class="copyright">
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">Lorem RSS</span> (this page and the feeds generated) by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Michael Bertolacci</span> are licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US">Creative Commons Attribution 3.0 Unported License</a>.
</p>
</div>
</div>
</body>
</html>
"""
app.get '/feed', (request, response) ->
if request.query.interval?
interval = parseInt request.query.interval
else
interval = 1
if not interval
response.send(500, "Interval must be an integer")
return
if interval <= 0
response.send(500, "Interval must be greater than 0")
return
unit = request.query.unit || 'minute'
if not units[unit]
response.send(500, "Unit must be one of #{_.keys(units).join(', ')}")
return
length = request.query.length || 10
if length < 0 or length > 1000
response.send(500, "Length must be greater or equal to 0 and smaller or equal to 1000")
return
pubDate = getNearest(interval, unit)
feed = new RSS({
title: "Lorem ipsum feed for an interval of #{interval} #{unit}s with #{length} item(s)",
description: 'This is a constantly updating lorem ipsum feed'
site_url: 'http://example.com/',
copyright: 'Michael Bertolacci, licensed under a Creative Commons Attribution 3.0 Unported License.',
ttl: Math.ceil(moment.duration(interval, unit).asMinutes()),
pubDate: pubDate.clone().toDate()
})
pubDate = getNearest(interval, unit)
for i in [0...length]
feed.item {
title: "Lorem ipsum #{pubDate.format()}",
description: loremIpsum(
random: seedRandom(pubDate.unix())
)
url: "http://example.com/test/#{pubDate.format('X')}"
author: 'John Smith',
date: pubDate.clone().toDate()
}
pubDate = pubDate.subtract(interval, unit)
etagString = feed.pubDate + interval + unit
response.set 'Content-Type', 'application/rss+xml'
response.set 'ETag', "\"#{crypto.createHash('md5').update(etagString).digest("hex");}\""
response.send feed.xml()
port = process.env.PORT || 5000;
app.listen port, () ->
console.log("Listening on " + port);