Repository: mbertolacci/lorem-rss Branch: master Commit: a575fdc92972 Files: 7 Total size: 19.9 KB Directory structure: gitextract_vjehonqu/ ├── .gitignore ├── Dockerfile ├── Procfile ├── README.md ├── package.json ├── web.coffee └── web.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules ================================================ FILE: Dockerfile ================================================ FROM node:12-alpine COPY package* ./ RUN npm install -g foreman && npm install COPY . . EXPOSE 5000 CMD ["nf", "start"] ================================================ FILE: Procfile ================================================ web: node web.js ================================================ FILE: README.md ================================================ # Lorem RSS Generates RSS feeds with content updated at regular intervals. I wrote this to answer a [question I asked on Stack Overflow](http://stackoverflow.com/questions/18202048/are-there-any-constantly-updating-rss-feed-services-to-use-for-testing-or-just). ## API Visit [http://lorem-rss.herokuapp.com/feed](http://lorem-rss.herokuapp.com/feed), with the following optional parameters: * _unit_: one of second, minute, day, month, or year * _interval_: 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. * _length_: 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. ## Examples * The default, updates once a minute, with 10 entries: [/feed](http://lorem-rss.herokuapp.com/feed) * Update every second instead of minute: [/feed?unit=second](http://lorem-rss.herokuapp.com/feed?unit=second) * Update every 30 seconds: [/feed?unit=second&interval=30](http://lorem-rss.herokuapp.com/feed?unit=second&interval=30) * Update once a day: [/feed?unit=day](http://lorem-rss.herokuapp.com/feed?unit=day) * Update every 6 months: [/feed?unit=month&interval=6](http://lorem-rss.herokuapp.com/feed?unit=month&interval=6) * Update once a year: [/feed?unit=year](http://lorem-rss.herokuapp.com/feed?unit=year) * Default feed with 42 entries: [/feed?length=42"](http://lorem-rss.herokuapp.com/feed?length=42) * **Invalid example:** update every 7 minutes (does not evenly divide 60): [/feed?unit=minute&interval=7](http://lorem-rss.herokuapp.com/feed?unit=minute&interval=7) ## Running locally The project contains a Dockerfile that can be used to run Lorem RSS locally. Build via: ``` docker build . -t lorem-rss ``` Run via: ``` docker run --rm -it -p 5000:5000 lorem-rss ``` With thanks given to [eelkevdbos](https://github.com/eelkevdbos), who contributed the Dockerfile. ## Copyright ### The feed and documentation Licensed by Michael Bertolacci under a Creative Commons Attribution 3.0 Unported License. ### The code 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. ================================================ FILE: package.json ================================================ { "name": "lorem-rss", "version": "0.0.1", "description": "", "main": "web.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "BSD", "dependencies": { "crypto": "~0.0.3", "express": "^4.18.2", "lodash": "^4.17.21", "lorem-ipsum": "~0.1.1", "moment": "^2.29.4", "morgan": "^1.9.1", "rss": "^1.2.2", "seed-random": "~1.0.1" } } ================================================ FILE: web.coffee ================================================ ### 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 """ Lorem RSS

Lorem RSS

Generates RSS feeds with content updated at regular intervals. I wrote this to answer a question I asked on Stack Overflow.

The code for this service is available on GitHub.

API

Visit /feed, with the following optional parameters:

Examples


""" 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); ================================================ FILE: web.js ================================================ // Generated by CoffeeScript 2.5.1 (function() { /* 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. */ var RSS, _, app, crypto, express, getNearest, loremIpsum, moment, morgan, port, seedRandom, units; 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 = function(interval, unit) { var now, returnDate, unitOptions; 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('/', function(request, response) { return response.send(` Lorem RSS

Lorem RSS

Generates RSS feeds with content updated at regular intervals. I wrote this to answer a question I asked on Stack Overflow.

The code for this service is available on GitHub.

API

Visit /feed, with the following optional parameters:

Examples


`); }); app.get('/feed', function(request, response) { var etagString, feed, i, interval, j, length, pubDate, ref, unit; if (request.query.interval != null) { interval = parseInt(request.query.interval); } else { interval = 1; } if (!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 (!units[unit]) { response.send(500, `Unit must be one of ${_.keys(units).join(', ')}`); return; } length = request.query.length || 10; if (length < 0 || 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 = j = 0, ref = length; (0 <= ref ? j < ref : j > ref); i = 0 <= ref ? ++j : --j) { 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")}\"`); return response.send(feed.xml()); }); port = process.env.PORT || 5000; app.listen(port, function() { return console.log("Listening on " + port); }); }).call(this);