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
6 changes: 6 additions & 0 deletions convex/crons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ crons.monthly(
{ dryRun: false }
);

crons.daily(
"Fetch PGA players",
{ hourUTC: 10, minuteUTC: 0 }, // 10:00 UTC, Midnight HST
api.pga.getPGAPlayers
);

export default crons;
62 changes: 62 additions & 0 deletions convex/pga.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use node";

import { action, internalMutation, query } from "./_generated/server";
import { api } from "./_generated/api";
import { v } from "convex/values";
import got from "got";
import * as cheerio from "cheerio";

interface Player {
rank: number;
name: string;
country: string;
}

export const getPGAPlayers = action({
args: {},
handler: async (ctx) => {
const response = await got(
"https://www.pgatour.com/stats/detail/186"
);
const $ = cheerio.load(response.body);
const players: Player[] = [];

$("tr").each((i, el) => {
if (i > 0 && i <= 50) {
const rank = parseInt(
$(el).find("td:nth-child(1)").text().trim()
);
const name = $(el).find("td:nth-child(2)").text().trim();
const country = $(el).find("td:nth-child(3)").text().trim();
players.push({ rank, name, country });
}
});

await ctx.runMutation(api.pga.storePGAPlayers, { players });

return players;
},
});

export const storePGAPlayers = internalMutation({
args: {
players: v.array(
v.object({
rank: v.number(),
name: v.string(),
country: v.string(),
})
),
},
handler: async (ctx, { players }) => {
// Clear the table before inserting new players
const allPlayers = await ctx.db.query("pgaPlayers").collect();
for (const player of allPlayers) {
await ctx.db.delete(player._id);
}

for (const player of players) {
await ctx.db.insert("pgaPlayers", player);
}
},
});
10 changes: 10 additions & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,16 @@ export default defineSchema({
})
.index("by_participant", ["participantId"])
.index("by_matchup", ["matchupId"]),

//////////////////PGA//////////////////////////////
pgaPlayers: defineTable({
rank: v.number(),
name: v.string(),
country: v.string(),
})
.index("by_rank", ["rank"])
.index("by_name", ["name"]),

//////////////////USERS//////////////////////////////

friendRequests: defineTable({
Expand Down
Loading