diff options
| -rw-r--r-- | app/index.js | 7 | ||||
| -rw-r--r-- | app/node_modules/okservices/okinstagram/index.js | 83 | ||||
| -rw-r--r-- | app/node_modules/okservices/okinstagram/package.json | 14 | ||||
| -rw-r--r-- | examples/lib/okpush/db.js | 1 | ||||
| -rw-r--r-- | examples/lib/okpush/index.js | 6 | ||||
| -rw-r--r-- | package.json | 2 |
6 files changed, 109 insertions, 4 deletions
diff --git a/app/index.js b/app/index.js index 31cbf10..f59f760 100644 --- a/app/index.js +++ b/app/index.js @@ -16,6 +16,7 @@ var OKSchema = require('okschema'); var OKS3Service = require('okservices/oks3'); var OKTwitterService = require('okservices/oktwitter') var OKWebhookService = require('okservices/okwebhook') +var OKInstagramService = require('okservices/okinstagram') require('dotenv').load(); @@ -123,6 +124,12 @@ function OKCMS(options) { config: config, }); break + case 'instagram': + services.instagram = OKInstagramService({ + express: express, + config: config, + }); + break default: services[key] = config.lib({ express: express, diff --git a/app/node_modules/okservices/okinstagram/index.js b/app/node_modules/okservices/okinstagram/index.js new file mode 100644 index 0000000..cbb1ebc --- /dev/null +++ b/app/node_modules/okservices/okinstagram/index.js @@ -0,0 +1,83 @@ +/** + * Service scrapes Instagram for pictures since they've ruined their API. + * Set it up in services config with the following options: + instagram: { + username: 'annapurnapics', + frequency: 60 * 60 * 1000, + }, + */ + +var request = require('request') + +function OKInstagram (options) { + if (!(this instanceof OKInstagram)) return new OKInstagram(options) + options = options || {} + if (!options.express) + throw new Error('Express not provided to OKInstagram'); + if (!options.config || !options.config.username) + throw new Error('Username not provided to OKInstagram'); + + var express = options.express + var router = express.Router() + + var username = options.config.username + var frequency = options.config.frequency || 60 * 60 * 1000 // hourly + + var posts = [] + + router.get('/', function (req, res) { + res.set('Content-Type', 'application/json; charset=utf-8') + res.send(posts) + }) + router.get('/fetch', function (req, res) { + res.send('Fetching now') + setTimeout(fetch) + }) + + function go () { + fetch(function(){ + setTimeout(go, frequency) + }) + } + function fetch (cb) { + request('https://www.instagram.com/' + username + '/', function (err, response, body) { + if (err || response.statusCode !== 200) { + console.error("error fetching instagrams") + cb && cb() + return + } + if (! body.match(/"nodes": \[/)) { + console.error("instagram format has changed") + cb && cb() + return + } + var node_first = body.split(/"nodes": \[/)[1] + var node_list = node_first.split(/\]/)[0] + var nodes = JSON.parse("[" + node_list + "]") + posts = nodes.map(function(node){ + var post = { + url: "https://www.instagram.com/p/" + node.code, + img: node.thumbnail_src, + caption: node.caption || "", + } + return post + }) + cb && cb() + }) + } + + router.post('*', function (req, res) { + throw new Error('OKInstagram POST requests not implemented') + }) + + if (frequency) + go() + + this._router = router +} + +OKInstagram.prototype.middleware = function () { + return this._router +} + +module.exports = OKInstagram diff --git a/app/node_modules/okservices/okinstagram/package.json b/app/node_modules/okservices/okinstagram/package.json new file mode 100644 index 0000000..a5b9d2b --- /dev/null +++ b/app/node_modules/okservices/okinstagram/package.json @@ -0,0 +1,14 @@ +{ + "name": "okinstagram", + "version": "1.0.0", + "description": "instagram", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "okfocus <frontdesk@okfoc.us>", + "license": "LNT", + "dependencies": { + "request": "^2.71.0" + } +} diff --git a/examples/lib/okpush/db.js b/examples/lib/okpush/db.js index 83a6ebc..b926ee9 100644 --- a/examples/lib/okpush/db.js +++ b/examples/lib/okpush/db.js @@ -80,7 +80,6 @@ function updateNotification (key, cb) { note.last_push = now note.save() } - console.log(note, created) cb(null, note) }) } diff --git a/examples/lib/okpush/index.js b/examples/lib/okpush/index.js index 04cf33a..6ef83c6 100644 --- a/examples/lib/okpush/index.js +++ b/examples/lib/okpush/index.js @@ -1,4 +1,5 @@ /** + * OKPush - Handles basic broadcast push notifications, as well as keeping track of tokens. */ var path = require('path') @@ -28,6 +29,8 @@ function OKPush (options) { throw new Error('Notifications not provided to OKPush') if (!options.config.bundleId) throw new Error('bundleId not provided to OKPush') + if (!options.config.mongodbUrl) + throw new Error('mongodbUrl not provided to OKPush') var express = options.express var router = express.Router() @@ -50,9 +53,8 @@ function OKPush (options) { router.use('/admin/', passport.initialize()) router.use('/public/', express.static(path.join(__dirname, './public'))); - // monkeypatch because of this app.use(router) shit.. obnoxious + // monkeypatch because of app.use(router) .. obnoxious router.all('/admin/(:path*)?', function (req, res, next) { - // req.url = "/_services/push" + req.url req.newUrl = req.url req.url = req.originalUrl next() diff --git a/package.json b/package.json index 019cfcb..7d0647a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "okcms", - "version": "0.2.1", + "version": "0.2.2", "description": "The dopest CMS on the planet.", "main": "app/index.js", "scripts": { |
