From 5fe861ac0de00ba1ff85fa28874938a9e389af2c Mon Sep 17 00:00:00 2001 From: Saahil Date: Mon, 9 Dec 2024 22:35:02 -0500 Subject: [PATCH] chore: zeon:ai_comment --- .gitignore | 1 + package.json | 2 +- src/commands/bdays.ts | 28 +++++++++++++ src/commands/shipments-hackclub.ts | 31 ++++++++++++++ src/index.ts | 9 +++++ src/modules/index.ts | 1 + src/modules/parseShipments.ts | 65 ++++++++++++++++++++++++++++++ yarn.lock | 4 +- 8 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 src/commands/bdays.ts create mode 100644 src/commands/shipments-hackclub.ts create mode 100644 src/modules/parseShipments.ts diff --git a/.gitignore b/.gitignore index 8df99a7..afbccee 100644 --- a/.gitignore +++ b/.gitignore @@ -135,3 +135,4 @@ t2.ts ctf/unenc *.bin *.txt +t3.ts \ No newline at end of file diff --git a/package.json b/package.json index 6ca350f..9d94e78 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "@sentry/profiling-node": "^8.41.0", "@slack/bolt": "^3.21.2", "body-parser": "latest", - "cheerio": "^1.0.0", + "cheerio": "latest", "dotenv": "^16.4.5", "eventsource": "latest", "form-data": "^4.0.1", diff --git a/src/commands/bdays.ts b/src/commands/bdays.ts new file mode 100644 index 0000000..921dfeb --- /dev/null +++ b/src/commands/bdays.ts @@ -0,0 +1,28 @@ +import { App } from "@slack/bolt"; +import { Command, onlyForMe } from "../modules/BaseCommand"; + +export default class Ping implements Command { + name: string; + description: string; + constructor() { + this.name = `/bday`; + this.description = `Bday :D`; + } + run(app: App) { + // app.command() + app.command(this.name, async ({ command, ack, respond }) => { + const stamp = Date.now(); + await ack(); + + // first make it store them + // TODO + const args = command.text.split(/ +/) + const cmd = args.shift().toLowerCase() + if(cmd == "config") { + + } else if(cmd == "remove-my-data") { + + } + }); + } +} diff --git a/src/commands/shipments-hackclub.ts b/src/commands/shipments-hackclub.ts new file mode 100644 index 0000000..2458863 --- /dev/null +++ b/src/commands/shipments-hackclub.ts @@ -0,0 +1,31 @@ +import { App } from "@slack/bolt"; +import { Command, onlyForMe } from "../modules/BaseCommand"; +import { createShipmentURL } from "../modules/parseShipments"; +import { ModifiedApp } from "../modules/slackapp"; + +export default class Ping implements Command { + name: string; + description: string; + constructor() { + this.name = `/zeon-hackclub-shipments`; + this.description = `add your shipment url so zeon can scarpe them`; + } + run(app: ModifiedApp) { + // app.command() + app.command(this.name, async ({ command, ack, respond }) => { + const stamp = Date.now(); + await ack(); + + + // respond(`Pong took: \`${Date.now() - stamp}ms\``).then((d) => { + // console.debug(`after ping`, d); + // }); + const shipmentURL = command.text + if (!shipmentURL) return respond(`:x: You need to provide a shipment url.`); + const parse = new URLSearchParams(shipmentURL) + const properURL = createShipmentURL(parse.get(`signature`), parse.get(`email`)) + await app.db.set(`shipment_url_${command.user_id}`, properURL) + respond(`:white_check_mark: Shipment URL was set! check the app home to see ur packages`) + }); + } +} diff --git a/src/index.ts b/src/index.ts index 1c89f27..0175b42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -199,5 +199,14 @@ cron.schedule("0 23 * 12 *", async () => { cron.schedule("0 0 * 12 *", () => { app.utils.adventOfCode.newDayNewChallange(app, `C01GF9987SL`); }); +cron.schedule("* * * * *", async () => { + const allUsersWithAShipmentURL = Object.keys(app.db.JSON()).filter(e=>e.startsWith(`shipment_url_`)) + if(allUsersWithAShipmentURL.length > 0) { +for(const userURLID of allUsersWithAShipmentURL) { + const shipments = await app.utils.hcshipments.parseShipments(app.db.get(`userURLID`)) + await app.db.set(`shipments_${userURLID}`, shipments) +} + } +}) process.on("unhandledRejection", handleError); process.on("unhandledException", handleError); diff --git a/src/modules/index.ts b/src/modules/index.ts index 7f71ab8..62eb7e4 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -10,3 +10,4 @@ export * as songs from "./Songs"; export * as nextdns from "./nextdns"; export * as hacktime from "./hacktime"; export * as adventOfCode from "./adventofcode"; +export * as hcshipments from "./parseShipments" \ No newline at end of file diff --git a/src/modules/parseShipments.ts b/src/modules/parseShipments.ts new file mode 100644 index 0000000..4865180 --- /dev/null +++ b/src/modules/parseShipments.ts @@ -0,0 +1,65 @@ +const cheerio = require('cheerio') +export function createShipmentURL(token: string, email: string) { +return `https://shipment-viewer.hackclub.com/shipments?email=${encodeURIComponent(email)}&signature=${token}&show_ids=yep` +} +export function requestEmailForUser() { + // TODO +} +export function parseShipments(shipmentsURL: string) { + return new Promise((res,rej) => { + const final = [] +fetch(shipmentsURL).then(r=>r.text()).then(rhtml => { + const $ = cheerio.load(rhtml) + const divs = ($(`[class="col-12 col-sm-6 col-md-4 col-lg-4"]`)) + for (const d of divs) { + // console.log(d) + const dd = $(d) + // YYYY-MM-DD + const addedDate = $(dd.find(`[class="card-header d-flex align-items-center justify-content-between"]`)).text().replaceAll(`on`, ``).replaceAll(`fulfilled`, ``).replaceAll(`added`, ``).trim() + let isDone = $(dd.find(`[class="card-header d-flex align-items-center justify-content-between"]`)).text().includes(`fulfilled`) + const shipmentTitle = $(dd.find(`[class="card-title"]`)).text() + const potshipProvider = $(Array.from($(dd.find(`[class="card-body"] > div > p`)))[0]) + const potContents = $($(dd.find(`[class="card-body"] > div > ul`))) +const potTracking = $($(Array.from(dd.find(`p`)).find(e=>$(e).text().startsWith(`tracking #`))).find(`a`)) +const Airtable = $($(Array.from(dd.find(`p`)).find(e=>$(e).text().startsWith(`Airtable`))).find(`a`)) +let airtable = null; +let tracking = null +let shiprovider = null; + let contents = null; +if(potContents) { +contents = (Array.from(potContents.find(`li`)).map(e=>$(e).text())) +} + + // console.log(potTrackingUrl.html()) + if(potTracking.html()) { + tracking = { + text: potTracking.text(), + url: potTracking.attr(`href`) + } + } + if(Airtable.html()) { + airtable = { + text: Airtable.text(), + url: Airtable.attr(`href`) + } + } + if(potshipProvider.html) { + shiprovider = potshipProvider.text() + } + final.push({ + shipmentTitle, + shipmentsURL, + shiprovider, + tracking, + isDone, + contents, + airtable, + addedDate + }) + + } +// console.log(final) +res (final) +}) +}) +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 7ea7d77..ac593ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1082,7 +1082,7 @@ cheerio-select@^2.1.0: domhandler "^5.0.3" domutils "^3.0.1" -cheerio@^1.0.0: +cheerio@latest: version "1.0.0" resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0.tgz#1ede4895a82f26e8af71009f961a9b8cb60d6a81" integrity sha512-quS9HgjQpdaXOvsZz82Oz7uxtXiy6UIsIQcpBj7HRw2M63Skasm9qlDocAM7jNuaxdhpPU7c4kJN+gA5MCu4ww== @@ -1216,7 +1216,7 @@ core-util-is@~1.0.0: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85" integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ== -cors@~2.8.5: +cors@latest, cors@~2.8.5: version "2.8.5" resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==