diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-10-07 19:48:04 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-10-07 19:48:04 -0400 |
| commit | bdcc83dae3742bf1c88da43a23f7278127d6251c (patch) | |
| tree | a769f00862270da42633b85958505a74dfe935d9 /app/node_modules/okservices | |
| parent | d2299d24a23c9d0d835631b72e4f1d1974958f94 (diff) | |
some stuff with okpush, add okinstagram to servicesv0.2.2
Diffstat (limited to 'app/node_modules/okservices')
| -rw-r--r-- | app/node_modules/okservices/okinstagram/index.js | 83 | ||||
| -rw-r--r-- | app/node_modules/okservices/okinstagram/package.json | 14 |
2 files changed, 97 insertions, 0 deletions
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" + } +} |
