remove duplicates for howlongtobeat_id just in case

This commit is contained in:
Wouter Groeneveld 2021-07-01 14:22:33 +02:00
parent cb36a4d666
commit dac72b2e92
2 changed files with 34 additions and 1 deletions

23
src/e2e-test.js Normal file
View File

@ -0,0 +1,23 @@
const jam = require('./index.js')
const dir = "/Users/wgroeneveld/development/jefklakscodex"
console.log('e2e test...');
jam.howlongtobeat.howlong({
postDir: `${dir}/content/games`,
downloadDir: `${dir}/static/img/hltb`
})
/*
const hltb = require('howlongtobeat');
(async function() {
const hltbService = new hltb.HowLongToBeatService()
const results = await hltbService.search("Arcanum")
console.log(results)
})()
*/

View File

@ -31,6 +31,9 @@ async function loadPostsWithFrontMatter(postsDirectoryPath) {
}
async function downloadThumbnail(url, id, dir) {
if(url.startsWith('/')) {
url = `https://howlongtobeat.com${url}`
}
console.log(` --- downloading thumbnail ${url} of id ${id}...`)
await pipeline(
got.stream(url),
@ -57,13 +60,20 @@ async function fillInHowLongToBeat(posts, downloadDir) {
async function run(options) {
const { postDir, downloadDir } = options
console.log(`-- SCANNING not yet processed articles in ${postDir} for game_name --`)
console.log(`-- SCANNING posts in ${postDir} for "game_name" key... --`)
let posts = await loadPostsWithFrontMatter(postDir)
console.log(` >> Found ${posts.length}`)
posts = posts.filter(post => post.game && !post.howlongtobeat_id)
console.log(` >> ToProcess: ${posts.length}`)
await fillInHowLongToBeat(posts, downloadDir)
for(post of posts) {
let data = await fs.readFile(post.file, 'utf8')
// just in case it's there, do not duplicate keys!
data = data.replace(/howlongtobeat_id:(.*)\n/, '')
data = data.replace(/nhowlongtobeat_hrs:(.*)\n/, '')
data = data.replace(/game_name:/, `howlongtobeat_id: ${post.howlongtobeat_id}\nhowlongtobeat_hrs: ${post.howlongtobeat}\ngame_name:`)
console.log(`\tFound game ${post.game}, how long filling in: ${post.howlongtobeat} (id #${post.howlongtobeat_id})`)
await fs.writeFile(post.file, data, 'utf8')