Skip to content

Commit

Permalink
v1 success !
Browse files Browse the repository at this point in the history
  • Loading branch information
as6325400 committed Feb 4, 2024
1 parent 429945e commit e9f35f7
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 1,884 deletions.
1,857 changes: 0 additions & 1,857 deletions file/Problem.json

This file was deleted.

2 changes: 1 addition & 1 deletion file/User.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"USERID_SET": []
"USERID_SET": ["223796"]
}
9 changes: 0 additions & 9 deletions src/file.ts

This file was deleted.

29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Client } from "discord.js";
import { IntentOptions } from "./config/IntentOptions";
import { setProblemJson } from "./file";
import * as dotenv from "dotenv";
import { Query } from "./query";
import { USERID_SET } from "../file/User.json";
import { User } from "./user";
import { getAcceptSet } from "./webcrawler";

dotenv.config();



(async () => {
Expand All @@ -10,7 +17,25 @@ import { setProblemJson } from "./file";
BOT.on("ready", () => {
console.log("Bot is ready!");
});
await setProblemJson();

const query : Query = await Query.init();
const userQueue : User[] = await User.loadUser(USERID_SET);

// eslint-disable-next-line no-constant-condition
while (true) {
for (const user of userQueue) {
const newProblemSets = await getAcceptSet(user.Id);
if (!user.equalSets(newProblemSets)) {
console.log("User " + user.Name + " has new accepted problems!");
const diff = user.diffSets(newProblemSets);
for (const problem of diff) {
console.log("New problem: " + query.getProblem(problem)!.title);
}
user.updateAccept(diff);
}
}
await new Promise(resolve => setTimeout(resolve, 1000 * 5));
}


})();
2 changes: 1 addition & 1 deletion src/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import cheerio from "cheerio";
import qs from "qs";
import dotenv from "dotenv";
import * as dotenv from "dotenv";

dotenv.config();

Expand Down
6 changes: 1 addition & 5 deletions src/problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ export interface Problem {
url: string;
}

export interface ProblemSet {
tags: string;
problems: Problem[];
}

export interface ProblemSetMap {
[key: string]: ProblemSet;
[key: string]: Problem;
}
32 changes: 32 additions & 0 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getProblemSet } from "./webcrawler";
import { Problem, ProblemSetMap } from "./problem";

export class Query {
private problemSet: ProblemSetMap;

private constructor(Set : ProblemSetMap) {
this.problemSet = Set;
}

static async init() : Promise<Query> {
const Set : ProblemSetMap = await getProblemSet();
return new Query(Set);
}

public getProblemSet(): ProblemSetMap {
return this.problemSet;
}

public getProblem(name: string): Problem | null{
if (!this.problemSet) {
return null;
}

for (const key in this.problemSet) {
if (key === name) {
return this.problemSet[key];
}
}
return null;
}
}
Empty file removed src/search.ts
Empty file.
52 changes: 49 additions & 3 deletions src/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
export interface User {
import { getUserName, getAcceptedNum, getAcceptSet } from "./webcrawler";


export class User{

Name: string;
Id: string;
Accept: number;
AcceptProblem: number[];
}
AcceptProblem: Set<string>;

private constructor(name: string, id: string, accept: number, acceptProblem: Set<string>) {
this.Name = name;
this.Id = id;
this.Accept = accept;
this.AcceptProblem = acceptProblem;
}

static async createUser(id: string): Promise<User> {

const name : string = await getUserName(id);
const accept : number= await getAcceptedNum(id);
const acceptProblem : Set<string> = await getAcceptSet(id);

return new User(name, id, accept, acceptProblem);
}

static async loadUser(idSets : string []) : Promise<User[]> {
const users : User[] = [];
for (const id of idSets) {
users.push(await User.createUser(id));
}
return users;
}

public diffSets (newSet: Set<string>): Set<string> {
const diff = new Set<string>();
for (const item of newSet) if (!this.AcceptProblem.has(item)) diff.add(item);
for (const item of this.AcceptProblem) if (!newSet.has(item)) diff.add(item);
return diff;
}

public equalSets (newSet : Set<string>): boolean {
return this.diffSets(newSet).size === 0;
}

public updateAccept(newAcceptProblem: Set<string>): void {
for (const problem of newAcceptProblem) {
this.AcceptProblem.add(problem);
}
}
}

21 changes: 16 additions & 5 deletions src/webcrawler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosResponse } from "axios";
import cheerio from "cheerio";
import { Problem, ProblemSetMap } from "./problem";
import dotenv from "dotenv";
import { ProblemSetMap } from "./problem";
import * as dotenv from "dotenv";

dotenv.config();

Expand All @@ -15,15 +15,13 @@ export async function getProblemSet(): Promise<ProblemSetMap> {

$("h2").each((index, element) => {
const tags = $(element).text();
const problems: Problem[] = [];
if (tags !== "General") {
$(element).next().find("a").each((index, element) => {
const title = $(element).text();
const url = $(element).attr("href")!;
const id = url.split("/")[3];
problems.push({ id: id, title: title, tags: tags, url: url });
problemMaps[title] = { id: id, title: title, tags: tags, url: url };
});
problemMaps[tags] = { tags: tags, problems: problems };
}
});

Expand Down Expand Up @@ -95,4 +93,17 @@ export async function getAcceptSet(id: string): Promise<Set<string>> {
}
}

export async function getUserName(id: string): Promise<string> {
const url = process.env.CSES_URL + "user/" + id;
try {
const response : AxiosResponse = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const name = $("h1").text().split(" ")[1];
return name;
} catch (error) {
console.error("Error fetching or parsing:", error);
throw error;
}
}

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

Expand Down

0 comments on commit e9f35f7

Please sign in to comment.