diff --git a/README.md b/README.md index 5907c12..b6cb97b 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ Usage example: Adds https://howlongtobeat.com/ game length (`MainGame`) and an ID to your front matter (keys `howlongtobeat_id` and `howlongtobeat_hrs`), provided you first added a property called `game_name`. (This gets substituted). +It also downloads a thumbnail of the cover image if you provided the dir as an option. + So, Frontmatter like this: ``` @@ -145,7 +147,9 @@ In your Hugo template, add a link to `https://howlongtobeat.com/game?id={howlong Usage example: ```js - await howlong(`${__dirname}/content/articles`) + await howlong({ + postDir: `${__dirname}/content/articles`, + downloadDir: `${__dirname}/static/img/howlongtobeat_thumbs`) ``` It will print out games and metadata it found. Uses the cool [howlongtobeat npm package](https://www.npmjs.com/package/howlongtobeat) to do its dirty work. diff --git a/package.json b/package.json index 0aa7e9e..5449a6f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jam-my-stack", - "version": "1.0.19", + "version": "1.0.20", "repository": { "url": "https://github.com/wgroeneveld/jam-my-stack", "type": "git" diff --git a/src/howlongtobeat/howlong.js b/src/howlongtobeat/howlong.js index b1638de..435e909 100644 --- a/src/howlongtobeat/howlong.js +++ b/src/howlongtobeat/howlong.js @@ -3,12 +3,15 @@ const hltbService = new hltb.HowLongToBeatService() const { getFiles } = require('./../file-utils'); const fs = require('fs').promises; - +const got = require("got"); const {promisify} = require('util'); const frontMatterParser = require('parser-front-matter'); const parse = promisify(frontMatterParser.parse.bind(frontMatterParser)); +const stream = require('stream'); +const pipeline = promisify(stream.pipeline); +const { createWriteStream } = require("fs"); async function loadPostsWithFrontMatter(postsDirectoryPath) { const postNames = await getFiles(postsDirectoryPath); @@ -27,22 +30,37 @@ async function loadPostsWithFrontMatter(postsDirectoryPath) { return posts; } -async function fillInHowLongToBeat(posts) { +async function downloadThumbnail(url, id, dir) { + console.log(` --- downloading thumbnail ${url} of id ${id}...`) + await pipeline( + got.stream(url), + createWriteStream(`${dir}/${id}.jpg`) + ) +} + +async function fillInHowLongToBeat(posts, downloadDir) { for(post of posts) { const results = await hltbService.search(post.game) if(results.length > 0) { - post.howlongtobeat = results[0].gameplayMain - post.howlongtobeat_id = results[0].id - } + const game = results[0] + post.howlongtobeat = game.gameplayMain + post.howlongtobeat_id = game.id + + if(downloadDir) { + await downloadThumbnail(game.imageUrl, game.id, downloadDir) + } + } } } -async function run(dir) { - console.log(`-- SCANNING not yet processed articles in ${dir} for game_name --`) - let posts = await loadPostsWithFrontMatter(dir) +async function run(options) { + const { postDir, downloadDir } = options + + console.log(`-- SCANNING not yet processed articles in ${postDir} for game_name --`) + let posts = await loadPostsWithFrontMatter(postDir) posts = posts.filter(post => post.game && !post.howlongtobeat_id) - await fillInHowLongToBeat(posts) + await fillInHowLongToBeat(posts, downloadDir) for(post of posts) { let data = await fs.readFile(post.file, 'utf8') diff --git a/test/howlongtobeat/__mocks__/howlongtobeat.js b/test/howlongtobeat/__mocks__/howlongtobeat.js index 72b694b..54997ce 100644 --- a/test/howlongtobeat/__mocks__/howlongtobeat.js +++ b/test/howlongtobeat/__mocks__/howlongtobeat.js @@ -5,7 +5,8 @@ class HowLongToBeatService { async search(game) { return [{ gameplayMain: 93, - id: 11228 + id: 11228, + imageUrl: "https://jefklakscodex.com/img/logo.png" }] } } diff --git a/test/howlongtobeat/howlong.test.js b/test/howlongtobeat/howlong.test.js index 7936ad9..5fbc782 100644 --- a/test/howlongtobeat/howlong.test.js +++ b/test/howlongtobeat/howlong.test.js @@ -13,6 +13,9 @@ game_name: "Wizardry 8" blabla nice one 9/10 GG! ` +jest.disableAutomock() +jest.unmock('got') + const dumpdir = `${__dirname}/howlong-stub` beforeEach(async () => { if(fs.existsSync(dumpdir)) { @@ -23,9 +26,22 @@ beforeEach(async () => { await fsp.writeFile(`${dumpdir}/howlongtobeat-sample.md`, mdsample, 'utf-8') }) -test('howlong adds howlong to beat id and hours to frontmatter', async () => { - await howlong(dumpdir) +test('howlong adds howlong to beat id and hours to frontmatter', async() => { + await howlong({ + postDir: dumpdir, + downloadDir: dumpdir + }) const actualmd = await fsp.readFile(`${dumpdir}/howlongtobeat-sample.md`, 'utf-8') expect(actualmd).toMatchSnapshot() }) + +test('howlong downloads the thumbnail as ID.jpg into downloaddir', async() => { + await howlong({ + postDir: dumpdir, + downloadDir: dumpdir + }) + + const actualmd = await fsp.readFile(`${dumpdir}/11228.jpg`, 'utf-8') + expect(actualmd).not.toBeUndefined() +})