os/parserView |
---|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var fs = require('fs') |
| 4 | +fs.writeFileSync('capsule.log', 'node parser was called but did not catch', 'utf8') |
| 5 | + |
| 6 | +var url = require('url') |
| 7 | + |
| 8 | +// parse URL, split up query params |
| 9 | +var uri = process.argv[2] |
| 10 | +var parsedURI = url.parse(uri) |
| 11 | +payloadArray = parsedURI.query.split(/[\=\&]/) |
| 12 | + |
| 13 | +// convert to key-val dict |
| 14 | +var payload = {} |
| 15 | +for (var i = 0, len = payloadArray.length ; i<len ; i+=2) { |
| 16 | + payload[payloadArray[i]] = (payloadArray[i+1]) |
| 17 | +} |
| 18 | + |
| 19 | +// decode body from base64 |
| 20 | +payload.body = unescape(new Buffer(payload.body, 'base64').toString()) |
| 21 | +payload.title = unescape(payload.title) |
| 22 | + |
| 23 | +// ssbify the dang thing, finally |
| 24 | +var ssbifyString = require('ssbify-string') |
| 25 | +var ssbClient = require('ssb-client') |
| 26 | + |
| 27 | +ssbClient(function (err, sbot) { |
| 28 | + if (err) fs.writeFileSync('capsule.log', err, 'utf8') |
| 29 | + |
| 30 | + ssbifyString( |
| 31 | + sbot, payload.body, |
| 32 | + { ignoreBrokenLinks: true, url: payload.src, title: payload.title }, |
| 33 | + function(err, res) { |
| 34 | + if (err) fs.writeFileSync('capsule.log', err, 'utf8') |
| 35 | + |
| 36 | + // publish a message |
| 37 | + sbot.publish({ type: 'post', text: res, channel: 'test' }, |
| 38 | + function (err, msg) { |
| 39 | + if (err) fs.writeFileSync('capsule.log', err, 'utf8') |
| 40 | + }) |
| 41 | + |
| 42 | + }) |
| 43 | +}) |