Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple poll end date formats #122

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-numbers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"osrs-web-scraper": patch
---

Support multiple end date formats for polls
15 changes: 12 additions & 3 deletions src/scrapers/polls/sections/pollHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { HTMLElement } from "node-html-parser";

import { PollSection } from "./types";

const END_DATE_FORMATS = ["EEEE, MMMM do", "EEEE MMMM do", "EEEE do MMMM"];

const pollHeader: PollSection = {
format: async (htmlElement, url) => {
const title = htmlElement.querySelector("h2").textContent;
Expand All @@ -35,9 +37,16 @@ const pollHeader: PollSection = {
.replaceAll(/(\()*(\))*/g, "");

const endDate = description.match(/This poll will close on (.*)\./)?.[1];
let endDateFormatted = parse(endDate, "EEEE, MMMM do", new Date());
if (!endDateFormatted) {
endDateFormatted = parse(endDate, "EEEE do MMMM", new Date());
let endDateFormatted;
let endDateFormatIndex = 0;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore It is a valid type
while (!endDateFormatted || isNaN(endDateFormatted)) {
endDateFormatted = parse(
endDate,
END_DATE_FORMATS[endDateFormatIndex++],
new Date()
);
}

content.push(
Expand Down
Loading