goodreads widgetify and tests
This commit is contained in:
parent
90f1a72fe3
commit
c2020e20e3
|
@ -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
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
const parseMastoFeed = require('./masto-feed-parser')
|
const { parseMastoFeed } = require('./masto-feed-parser')
|
||||||
|
const { widgetify } = require('./goodreads-widgetify.js')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
parseMastoFeed
|
parseMastoFeed,
|
||||||
|
goodreadsWidgetify: widgetify
|
||||||
};
|
};
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
jest.mock('https')
|
||||||
|
const { widgetify } = require('../src/goodreads-widgetify')
|
||||||
|
|
||||||
|
test("goodreads-widgetify changes from lowres to hires images", async () => {
|
||||||
|
const result = await widgetify("fake")
|
||||||
|
|
||||||
|
expect(result).toMatch(/_S400_.jpg/)
|
||||||
|
expect(result).not.toMatch(/_SX/)
|
||||||
|
})
|
Loading…
Reference in New Issue