diff --git a/src/goodreads-widgetify.js b/src/goodreads-widgetify.js new file mode 100644 index 0000000..f275444 --- /dev/null +++ b/src/goodreads-widgetify.js @@ -0,0 +1,28 @@ +const https = require('https') + + +// WHY? Because including this thing comes with free cookies... + +const replaceLowresWithHiresImages = (data) => { + return data.replace(/_SX[0-9]+_(SY[0-9]+_)*.jpg/g, "_S400_.jpg") +} + +function widgetify(url) { + return new Promise(resolve => { + https.get(url, (resp) => { + let data = ''; + + resp.on('data', (chunk) => { + data += chunk; + }); + + resp.on('end', () => { + resolve(replaceLowresWithHiresImages(data)) + }); + }) + }) +} + +module.exports = { + widgetify +} diff --git a/src/index.js b/src/index.js index c956539..2d55fd2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,7 @@ -const parseMastoFeed = require('./masto-feed-parser') +const { parseMastoFeed } = require('./masto-feed-parser') +const { widgetify } = require('./goodreads-widgetify.js') module.exports = { - parseMastoFeed + parseMastoFeed, + goodreadsWidgetify: widgetify }; diff --git a/test/__mocks__/https.js b/test/__mocks__/https.js new file mode 100644 index 0000000..744b403 --- /dev/null +++ b/test/__mocks__/https.js @@ -0,0 +1,16 @@ +const fs = require('fs') + +const fakedata = fs.readFileSync('./test/expected-goodreads-content.js') + +function getmock(url, callback) { + callback({ + on: function(id, callback) { + if(id === "data") callback(fakedata) + if(id === "end") callback() + } + }) +} + +module.exports = { + "get": getmock +} diff --git a/test/expected-goodreads-content.js b/test/expected-goodreads-content.js new file mode 100644 index 0000000..9078d6f --- /dev/null +++ b/test/expected-goodreads-content.js @@ -0,0 +1,9 @@ + var widget_code = '

\n Wouter's bookshelf: read<\/a>\n <\/h2>\n
\n