From 9e1f85ba9fe6f7b778b71678520e2004937a9cdd Mon Sep 17 00:00:00 2001 From: korigamik Date: Sat, 21 May 2022 10:01:05 +0530 Subject: [PATCH] add leetCode parser --- .gitignore | 1 + src/parsers/parsers.ts | 2 ++ src/parsers/problem/LeetCodeProblemParser.ts | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 src/parsers/problem/LeetCodeProblemParser.ts diff --git a/.gitignore b/.gitignore index 41496f6c..de1d7e04 100644 --- a/.gitignore +++ b/.gitignore @@ -120,3 +120,4 @@ dist/ # NPM (Yarn is used) .package-lock.json +.cph diff --git a/src/parsers/parsers.ts b/src/parsers/parsers.ts index c60320fe..3e12dea8 100644 --- a/src/parsers/parsers.ts +++ b/src/parsers/parsers.ts @@ -70,6 +70,7 @@ import { HrbustOnlineJudgeProblemParser } from './problem/HrbustOnlineJudgeProbl import { HydroProblemParser } from './problem/HydroProblemParser'; import { JutgeProblemParser } from './problem/JutgeProblemParser'; import { KattisProblemParser } from './problem/KattisProblemParser'; +import { LeetCodeProblemParser } from './problem/LeetCodeProblemParser'; import { LibraryCheckerProblemParser } from './problem/LibraryCheckerProblemParser'; import { LibreOJProblemParser } from './problem/LibreOJProblemParser'; import { LightOJProblemParser } from './problem/LightOJProblemParser'; @@ -262,4 +263,5 @@ export const parsers: Parser[] = [ new YukicoderProblemParser(), new YukicoderContestParser(), + new LeetCodeProblemParser(), ]; diff --git a/src/parsers/problem/LeetCodeProblemParser.ts b/src/parsers/problem/LeetCodeProblemParser.ts new file mode 100644 index 00000000..8306abff --- /dev/null +++ b/src/parsers/problem/LeetCodeProblemParser.ts @@ -0,0 +1,20 @@ +import { Sendable } from '../../models/Sendable'; +import { TaskBuilder } from '../../models/TaskBuilder'; +import { htmlToElement } from '../../utils/dom'; +import { Parser } from '../Parser'; + +export class LeetCodeProblemParser extends Parser { + public getMatchPatterns(): string[] { + return ['https://leetcode.com/problems/*', 'https://leetcode.com/contests/*/problems/*']; + } + + public async parse(url: string, html: string): Promise { + console.log('My script engaged!'); + const elem = htmlToElement(html); + const task = new TaskBuilder('HackerEarth').setUrl(url); + const titleElement = elem.querySelector('[data-cy="question-title"]'); + task.setName(titleElement ? titleElement.textContent.trim() : 'LeetCodeProblem'); + task.setCategory(elem.querySelector('[diff]').textContent.trim()); + return task.build(); + } +}