hltb: download thumbnails as cover.jpg in respective static dir instead of {id}.jpg in one dir

This commit is contained in:
Wouter Groeneveld 2022-11-06 09:52:39 +01:00
parent b819cf3787
commit 8a386eb12d
6 changed files with 35 additions and 22 deletions

View File

@ -6,9 +6,9 @@ Published at https://www.npmjs.com/package/jam-my-stack
[![npm version](https://badge.fury.io/js/jam-my-stack.svg)](https://badge.fury.io/js/jam-my-stack)
These simple scripts **enrich your Jamstack-site** by adding/manipulating/whatever (meta)data, such as extra posts, indexing, and so forth. A primary example of these tools in action is my own site https://brainbaking.com - inspect how it's used at https://github.com/wgroeneveld/brainbaking
These simple scripts **enrich your Jamstack-site** by adding/manipulating/whatever (meta)data, such as extra posts, indexing, and so forth. A primary example of these tools in action is my own site https://brainbaking.com - inspect how it's used at https://git.brainbaking.com/wgroeneveld/brainbaking
**Are you looking for a way to receive webmentions?** See https://github.com/wgroeneveld/serve-my-jams !
**Are you looking for a way to receive webmentions?** See https://git.brainbaking.com/wgroeneveld/go-jamming !
## The tools
@ -113,7 +113,7 @@ With Pagefind, there's no need to integrate it into jam-my-stack, greatly simpli
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. The downloaded thumbnail is automatically optimized for the web using `mogrify` (this will emit a warning if you do not have ImageMagick installed locally).
It also downloads a thumbnail of the cover image as `cover.jpg` in **the same relative directory as the source article** if you provided the dir as an option. The downloaded thumbnail is automatically optimized for the web using `mogrify` (this will emit a warning if you do not have ImageMagick installed locally).
So, Frontmatter like this:
@ -140,17 +140,19 @@ Usage example:
```js
await howlong({
postDir: `${__dirname}/content/articles`,
downloadDir: `${__dirname}/static/img/howlongtobeat_thumbs`)
postDir: `${__dirname}/content`,
downloadDir: `${__dirname}/static`)
```
It will print out games and metadata it found.
Working example: https://jefklakscodex.com/articles/reviews/diablo-3/ (on the left side). Check out the Hugo template to use the properties at https://github.com/wgroeneveld/jefklakscodex .
Suppose the above `diablo-3.md` lives in `content/games/switch/diablo-3`, then a `cover.jpg` will be automatically downloaded in `static/games/switch/diablo-3/` and that directory will be created if not yet existing.
Working example: https://jefklakscodex.com/games/diablo-3/ (on the left side). Check out the Hugo template to use the properties at https://git.brainbaking.com/wgroeneveld/jefklakscodex.
### 5. Webmentions
In cooperation with https://github.com/wgroeneveld/go-jamming
In cooperation with https://git.brainbaking.com/wgroeneveld/go-jamming
#### 5.1 `getWebmentions`
@ -167,7 +169,7 @@ await getWebmentions("brainbaking.com", {
#### 5.1 `send`
Calls the set webmention endpoint using a `PUT`. Based on the RSS feed, see the [go-jamming](github.com/wgroeneveld/go-jamming) README.
Calls the set webmention endpoint using a `PUT`. Based on the RSS feed, see the [go-jamming](git.brainbaking.com/wgroeneveld/go-jamming) README.
Same as `getWebmentions`.

View File

@ -1,6 +1,6 @@
{
"name": "jam-my-stack",
"version": "1.0.33",
"version": "1.0.34",
"repository": {
"url": "https://github.com/wgroeneveld/jam-my-stack",
"type": "git"

View File

@ -1,5 +1,6 @@
const fs = require('fs').promises;
const { existsSync, mkdirSync } = require("fs");
const { resolve } = require('path');
const fs = require('fs').promises;
// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
async function getFiles(dir) {
@ -11,6 +12,13 @@ async function getFiles(dir) {
return Array.prototype.concat(...files);
}
module.exports = {
getFiles
function createIfNotExists(dir) {
if(!existsSync(dir)) {
mkdirSync(dir, { recursive: true })
}
}
module.exports = {
getFiles,
createIfNotExists
}

View File

@ -1,4 +1,4 @@
const { getFiles } = require('./../file-utils');
const { getFiles, createIfNotExists } = require('./../file-utils');
const fs = require('fs').promises;
const got = require("got");
const {promisify} = require('util');
@ -35,7 +35,7 @@ async function downloadThumbnail(url, id, dir) {
console.log(` --- downloading thumbnail ${url} of id ${id}...`)
await pipeline(
got.stream(url),
createWriteStream(`${dir}/${id}.jpg`)
createWriteStream(`${dir}/cover.jpg`)
)
}
@ -91,7 +91,7 @@ async function howLongToBeatSearch(game) {
})
}
async function fillInHowLongToBeat(posts, downloadDir) {
async function fillInHowLongToBeat(posts, postRootDir, downloadRootDir) {
for(post of posts) {
const results = await howLongToBeatSearch(post.game)
@ -100,9 +100,13 @@ async function fillInHowLongToBeat(posts, downloadDir) {
post.howlongtobeat = game.howlong
post.howlongtobeat_id = game.id
if(downloadDir) {
if(downloadRootDir) {
// this assumes postDir is something like ".../content/blah" and dlDir like ".../static/blah" to mirror its structure
const downloadDir = post.file.replace(postRootDir, downloadRootDir).replace('.md', '')
createIfNotExists(downloadDir)
await downloadThumbnail(game.imageUrl, game.id, downloadDir)
const { stdout, stderr } = await exec(`mogrify -sampling-factor 4:2:0 -strip -quality 80 -interlace JPEG -resize '>300x' -format jpg -colorspace sRGB ${downloadDir}/${game.id}.jpg`)
const { stdout, stderr } = await exec(`mogrify -sampling-factor 4:2:0 -strip -quality 80 -interlace JPEG -resize '>300x' -format jpg -colorspace sRGB ${downloadDir}/cover.jpg`)
if(stderr) {
console.log(`-- WARN: unable to mogrify downloaded JPG: ${stderr}`)
}
@ -119,7 +123,7 @@ async function run(options) {
console.log(` >> Found ${posts.length}`)
posts = posts.filter(post => post.game && !post.howlongtobeat_id)
console.log(` >> ToProcess: ${posts.length}`)
await fillInHowLongToBeat(posts, downloadDir)
await fillInHowLongToBeat(posts, postDir, downloadDir)
for(post of posts) {
let data = await fs.readFile(post.file, 'utf8')

View File

@ -1,7 +1,6 @@
const got = require("got");
const { createWriteStream } = require("fs");
const { createWriteStream, existsSync } = require("fs");
const { pipeline } = require('stream/promises');
const { existsSync } = require('fs');
const fsp = require('fs').promises;
const { dirname } = require('path');

View File

@ -34,12 +34,12 @@ test('howlong adds howlong to beat id and hours to frontmatter', async() => {
expect(actualmd).toMatchSnapshot()
})
test('howlong downloads the thumbnail as ID.jpg into downloaddir', async() => {
test('howlong downloads the thumbnail as cover.jpg into respective downloaddir', async() => {
await howlong({
postDir: dumpdir,
downloadDir: dumpdir
})
const actualmd = await fsp.readFile(`${dumpdir}/11228.jpg`, 'utf-8')
const actualmd = await fsp.readFile(`${dumpdir}/howlongtobeat-sample/cover.jpg`, 'utf-8')
expect(actualmd).not.toBeUndefined()
})