also download thumbnails from howlongtobeat

This commit is contained in:
Wouter Groeneveld 2021-07-01 13:49:30 +02:00
parent 1bd64eed52
commit cb36a4d666
5 changed files with 53 additions and 14 deletions

View File

@ -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.

View File

@ -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"

View File

@ -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')

View File

@ -5,7 +5,8 @@ class HowLongToBeatService {
async search(game) {
return [{
gameplayMain: 93,
id: 11228
id: 11228,
imageUrl: "https://jefklakscodex.com/img/logo.png"
}]
}
}

View File

@ -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()
})