Skip to content

Instantly share code, notes, and snippets.

@letsbreelhere
Created February 9, 2022 06:44
Show Gist options
  • Save letsbreelhere/97d5fd6da58302cbccf194d48176beb8 to your computer and use it in GitHub Desktop.
Save letsbreelhere/97d5fd6da58302cbccf194d48176beb8 to your computer and use it in GitHub Desktop.
J! Archive Parser
import { parse } from 'node-html-parser';
const parseRound = (clueRoot, responseRoot) => {
const categories: Array<string> = clueRoot.querySelectorAll('td.category_name').map(c => c.rawText);
const clues = clueRoot.querySelectorAll('td.clue');
let result: Object<string, object> = {};
categories.forEach(category => {
result[category] = [];
})
const responses = responseRoot.querySelectorAll('td.clue').map(response =>
response.querySelector('.correct_response')?.rawText || 'UNKNOWN'
);
clues.forEach((clue, i: number) => {
const col = Math.floor(i%6);
const category = categories[col];
let value = clue.querySelector('.clue_value');
value = value ? Number(value.rawText.slice(1)) : 'DAILY_DOUBLE';
result[category] = [
...result[category],
{
value,
clue: clue.querySelector('.clue_text').rawText,
response: responses[i],
}
]
})
return result;
}
const parseFinalJeopardy = (clueRoot, responseRoot) => {
const category = clueRoot.querySelector('.category_name').rawText
const clue = clueRoot.querySelector('.clue_text').rawText
const response = responseRoot.querySelector('em.correct_response').rawText
return { category, clue, response }
}
export default async (clueHtml, responseHtml) => {
const responseRoot = await parse(responseHtml);
const clueRoot = await parse(clueHtml);
console.warn(
responseRoot.querySelector('#jeopardy_round'),
responseRoot.querySelector('#double_jeopardy_round'),
responseRoot.querySelector('#final_jeopardy_round'),
)
const firstRound = parseRound(
clueRoot.querySelector('#jeopardy_round'),
responseRoot.querySelector('#jeopardy_round')
);
const secondRound = parseRound(
clueRoot.querySelector('#double_jeopardy_round'),
responseRoot.querySelector('#double_jeopardy_round')
);
const finalJeopardy = parseFinalJeopardy(
clueRoot.querySelector('#final_jeopardy_round'),
responseRoot.querySelector('#final_jeopardy_round'),
);
return {
firstRound,
secondRound,
finalJeopardy,
};
}
// Example with fetch
const date = '2021-01-26';
const clueHtml = await fetch(`http://www.j-archive.com/search.php?search=date%3A2021-01-26');
const gameId = parse(clueHtml).querySelector('img.game_dynamics')._attrs['src'].replace('chartgame.php?game_id=', '');
const responseHtml = await fetch(`http://www.j-archive.com/showgameresponses.php?game_id=${gameId}`);
const parsed = await parseJ(clueHtml, responseHtml);
console.log(parsed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment