Skip to content
Open
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
44 changes: 35 additions & 9 deletions src/api/(parsers)/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,45 @@ export default async function parse(url: string) {
}
} catch (error) {
console.error('Fetch error:', error);
throw new Error(
`Failed to fetch html for url ${url}: ${error instanceof Error ? error.message : 'Unknown error'}`
);
// Return fallback data instead of throwing
html = '';
}

for (const [domain, parser] of Object.entries(parsers)) {
if (url.includes(domain)) {
return parser(url, html);
try {
return parser(url, html || '');
} catch (error) {
console.error(`Parser error for ${domain}:`, error);
// Return fallback data based on domain
if (domain === 'usaco.org') {
const id = url.split('=').at(-1)?.trim() || 'unknown';
return {
uniqueId: `usaco-${id}`,
name: 'unknown',
source: 'unknown',
solutionMetadata: {
kind: 'USACO',
usacoId: isNaN(+id) ? 0 : +id,
},
};
}
// For other domains, return a generic fallback
return {
uniqueId: 'unknown',
name: 'unknown',
source: 'unknown',
solutionMetadata: {},
};
}
}
}
throw new Error(`No parser found for this url.
Available parsers:
${Object.keys(parsers)
.map(key => ` - ${key}`)
.join('\n')}`);

// No parser found - return fallback
return {
uniqueId: 'unknown',
name: 'unknown',
source: 'unknown',
solutionMetadata: {},
};
}
24 changes: 17 additions & 7 deletions src/api/(parsers)/usaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ export default function parseUsaco(url: string, htmlContent: string) {
const id = url.split('=').at(-1);
if (!id) throw new Error(`${url} does not contain problem id`);
const problem = htmlContent.match(/<h2> Problem (\d). (.*) <\/h2>/);
if (!problem) throw new Error(`Problem title not found at url ${url}`);
const number = problem[1],
let number, title;
if (!problem) {
number = 'unknown';
title = 'unknown';
} else {
number = problem[1];
title = problem[2];
}
const contest = htmlContent.match(
/<h2> USACO (\d+) (December|January|February|(?:US Open)) Contest, (Bronze|Silver|Gold|Platinum) <\/h2>/
);
if (!contest) throw new Error(`Contest not found at url ${url}`);
// year and month could be used someday idk
const year = contest[1],
month = contest[2],
division = contest[3];
let year, month, division;
if (!contest) {
year = 'unknown';
month = 'unknown';
division = 'unknown';
} else {
year = contest[1];
month = contest[2]!;
division = contest[3]!;
}
return {
uniqueId: `usaco-${id}`,
name: title,
Expand Down