Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to supabase from sleeper api #292

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
862 changes: 641 additions & 221 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@
"@smui/tab": "6.0.0-beta.13",
"@smui/tab-bar": "6.0.0-beta.13",
"@smui/textfield": "6.0.0-beta.13",
"@supabase/supabase-js": "^2.39.1",
"@sveltejs/adapter-vercel": "^3.0.1",
"@sveltejs/kit": "^1.15.2",
"contentful": "^9.1.34",
"contentful-management": "^10.12.0",
"cookie": "^0.4.1",
"dotenv": "^16.3.1",
"fast-xml-parser": "^4.2.5",
"fuzzyjs": "^5.0.1",
"svelte": "^3.50.1"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Resources.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import List, { Item, Graphic, Separator, Text } from '@smui/list';
import { dynasty } from './utils/helper';
import { dynasty } from './utils/helper';

const today = new Date();
const resources = [
Expand Down
9 changes: 5 additions & 4 deletions src/lib/utils/helperFunctions/leagueAwards.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase"
import { getLeagueData } from './leagueData';
import { getLeagueRosters } from './leagueRosters';
import { waitForAll } from './multiPromise';
Expand Down Expand Up @@ -77,10 +78,10 @@ const getPodiums = async (previousSeasonID) => {
// fetch the previous season's data from sleeper
const getPreviousLeagueData = async (previousSeasonID) => {
const resPromises = [
fetch(`https://api.sleeper.app/v1/league/${previousSeasonID}`, {compress: true}),
getLeagueRosters(previousSeasonID),
fetch(`https://api.sleeper.app/v1/league/${previousSeasonID}/losers_bracket`, {compress: true}),
fetch(`https://api.sleeper.app/v1/league/${previousSeasonID}/winners_bracket`, {compress: true}),
supabase.from('view_league').select('*').eq('league_id', previousSeasonID),
getLeagueRosters(previousSeasonID), // sleeper
supabase.from('view_league_losers_brackets').select('*').eq('league_id', previousSeasonID),
supabase.from('view_league_winners_brackets').select('*').eq('league_id', previousSeasonID),
]

const [leagueRes, rostersData, losersRes, winnersRes] = await waitForAll(...resPromises).catch((err) => { console.error(err); });
Expand Down
7 changes: 4 additions & 3 deletions src/lib/utils/helperFunctions/leagueBrackets.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase"
import { getLeagueData } from './leagueData';
import { leagueID } from '$lib/utils/leagueInfo';
import { getLeagueRosters } from './leagueRosters';
Expand All @@ -21,8 +22,8 @@ export const getBrackets = async (queryLeagueID = leagueID) => {

// get bracket data for winners and losers
const bracketsAndMatchupFetches = [
fetch(`https://api.sleeper.app/v1/league/${queryLeagueID}/winners_bracket`, {compress: true}),
fetch(`https://api.sleeper.app/v1/league/${queryLeagueID}/losers_bracket`, {compress: true}),
supabase.from('view_league_winners_brackets').select('*').eq('league_id', queryLeagueID),
supabase.from('view_league_losers_brackets').select('*').eq('league_id', queryLeagueID),
]

// variables for playoff records
Expand All @@ -49,7 +50,7 @@ export const getBrackets = async (queryLeagueID = leagueID) => {
// add each week after the regular season to the fetch array
for(let i = playoffsStart; i < 19; i++) {
// Get the matchup data (starters) for the playoff weeks
bracketsAndMatchupFetches.push(fetch(`https://api.sleeper.app/v1/league/${queryLeagueID}/matchups/${i}`, {compress: true}));
bracketsAndMatchupFetches.push(supabase.from('view_league_matchups').select('*').eq('league_key', queryLeagueID)[i]);
}

// Simultaneously fetch the bracket and matchup data
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/helperFunctions/leagueData.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { get } from 'svelte/store';
import { supabase } from "$lib/utils/supabase"
import {leagueData} from '$lib/stores';
import { leagueID } from '$lib/utils/leagueInfo';

export const getLeagueData = async (queryLeagueID = leagueID) => {
if(get(leagueData)[queryLeagueID]) {
return get(leagueData)[queryLeagueID];
}
const res = await fetch(`https://api.sleeper.app/v1/league/${queryLeagueID}`, {compress: true}).catch((err) => { console.error(err); });
const res = await supabase.from('view_league').select('*').eq('league_id', queryLeagueID).catch((err) => { console.error(err); });
const data = await res.json().catch((err) => { console.error(err); });

if (res.ok) {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/utils/helperFunctions/leagueDrafts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getLeagueData } from './leagueData';
import { supabase } from "$lib/utils/supabase";
import { leagueID } from '$lib/utils/leagueInfo';
import { waitForAll } from './multiPromise';
import { get } from 'svelte/store';
Expand All @@ -20,7 +21,7 @@ export const getUpcomingDraft = async () => {
let year = parseInt(leagueData.season);

const [officialDraftRes, picksRes] = await waitForAll(
fetch(`https://api.sleeper.app/v1/draft/${draftID}`, {compress: true}),
supabase.from('view_draft').select('*').eq('draft_id', draftID),
fetch(`https://api.sleeper.app/v1/league/${leagueID}/traded_picks`, {compress: true}),
).catch((err) => { console.error(err); });

Expand Down Expand Up @@ -186,7 +187,7 @@ export const getPreviousDrafts = async () => {
while(curSeason && curSeason != 0) {
const [leagueData, completedDraftsInfo] = await waitForAll(
getLeagueData(curSeason).catch((err) => { console.error(err); }),
fetch(`https://api.sleeper.app/v1/league/${curSeason}/drafts`, {compress: true}),
supabase.from('view_league_drafts').select('*'),
).catch((err) => { console.error(err); });

const completedDrafts = await completedDraftsInfo.json();
Expand All @@ -197,9 +198,9 @@ export const getPreviousDrafts = async () => {
const year = parseInt(completedDraft.season);

const [officialDraftRes, picksRes, playersRes] = await waitForAll(
fetch(`https://api.sleeper.app/v1/draft/${draftID}`, {compress: true}),
supabase.from('view_draft').select('*').eq('draft_id', draftID),
fetch(`https://api.sleeper.app/v1/draft/${draftID}/traded_picks`, {compress: true}),
fetch(`https://api.sleeper.app/v1/draft/${draftID}/picks`, {compress: true}),
supabase.from('view_draft_picks').select('*').eq('draft_id', draftID),
).catch((err) => { console.error(err); });

const [officialDraft, picks, players] = await waitForAll(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/helperFunctions/leagueMatchups.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { getLeagueData } from "./leagueData"
import { leagueID } from '$lib/utils/leagueInfo';
import { getNflState } from "./nflState"
Expand Down Expand Up @@ -27,7 +28,7 @@ export const getLeagueMatchups = async () => {
// pull in all matchup data for the season
const matchupsPromises = [];
for(let i = 1; i < leagueData.settings.playoff_week_start; i++) {
matchupsPromises.push(fetch(`https://api.sleeper.app/v1/league/${leagueID}/matchups/${i}`, {compress: true}))
matchupsPromises.push(supabase.from('view_league_matchups').select('*').eq('league_key', leagueID)[i])
}
const matchupsRes = await waitForAll(...matchupsPromises);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/helperFunctions/leagueRecords.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getLeagueData } from './leagueData';
import { supabase } from "$lib/utils/supabase";
import { leagueID } from '$lib/utils/leagueInfo';
import { getNflState } from './nflState';
import { getLeagueRosters } from "./leagueRosters";
Expand Down Expand Up @@ -148,7 +149,7 @@ const processRegularSeason = async ({rosters, leagueData, curSeason, week, regul
const matchupsPromises = [];
let startWeek = parseInt(week);
while(week > 0) {
matchupsPromises.push(fetch(`https://api.sleeper.app/v1/league/${curSeason}/matchups/${week}`, {compress: true}))
matchupsPromises.push(supabase.from('view_league_matchups').select('*').eq('week', week))
week--;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/helperFunctions/leagueRosters.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { leagueID } from '$lib/utils/leagueInfo';
import { get } from 'svelte/store';
import { rostersStore } from '$lib/stores';
Expand All @@ -12,7 +13,7 @@ export const getLeagueRosters = async (queryLeagueID = leagueID) => {
) {
return storedRoster;
}
const res = await fetch(`https://api.sleeper.app/v1/league/${queryLeagueID}/rosters`, {compress: true}).catch((err) => { console.error(err); });
const res = await supabase.from('view_league_rosters').select('*').eq('league_id', queryLeagueID).catch((err) => { console.error(err); });
const data = await res.json().catch((err) => { console.error(err); });

if (res.ok) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/helperFunctions/leagueStandings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { leagueID } from '$lib/utils/leagueInfo';
import { getNflState } from "./nflState"
import { getLeagueData } from "./leagueData"
Expand Down Expand Up @@ -63,7 +64,7 @@ export const getLeagueStandings = async () => {
// pull in all matchup data for the season
const matchupsPromises = [];
for(let i = week - 1; i > 0; i--) {
matchupsPromises.push(fetch(`https://api.sleeper.app/v1/league/${leagueID}/matchups/${i}`, {compress: true}))
matchupsPromises.push(supabase.from('view_league_matchups').select('*').eq('league_key', leagueID)[i])
}
const matchupsRes = await waitForAll(...matchupsPromises);

Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/helperFunctions/leagueTeamManagers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { leagueID, managers } from '$lib/utils/leagueInfo';
import { get } from 'svelte/store';
import { teamManagersStore } from '$lib/stores';
Expand All @@ -17,9 +18,9 @@ export const getLeagueTeamManagers = async () => {
// loop through all seasons and create a [year][roster_id]: team, managers object
while(currentLeagueID && currentLeagueID != 0) {
const [usersRaw, leagueData, rostersRaw] = await waitForAll(
fetch(`https://api.sleeper.app/v1/league/${currentLeagueID}/users`, {compress: true}),
supabase.from('view_league_users').select('*').eq('league_id', currentLeagueID),
getLeagueData(currentLeagueID),
fetch(`https://api.sleeper.app/v1/league/${currentLeagueID}/rosters`, {compress: true}),
supabase.from('view_league_rosters').select('*').eq('league_id', currentLeagueID),
).catch((err) => { console.error(err); });

const [users, rosters] = await waitForAll(
Expand Down
6 changes: 2 additions & 4 deletions src/lib/utils/helperFunctions/leagueTransactions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { getLeagueData } from './leagueData';
import { leagueID } from '$lib/utils/leagueInfo';
import { getNflState } from './nflState';
Expand Down Expand Up @@ -107,10 +108,7 @@ const combThroughTransactions = async (week, currentLeagueID) => {
const transactionPromises = [];

for(const singleLeagueID of leagueIDs) {
while(week > 0) {
transactionPromises.push(fetch(`https://api.sleeper.app/v1/league/${singleLeagueID}/transactions/${week}`, {compress: true}));
week--;
}
transactionPromises.push(supabase.from('view_league_transactions').select('*').eq('league_id', singleLeagueID));
week = 18;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/helperFunctions/rivalryMatchups.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { getLeagueData } from "./leagueData"
import { leagueID } from '$lib/utils/leagueInfo';
import { getNflState } from "./nflState"
Expand Down Expand Up @@ -51,7 +52,7 @@ export const getRivalryMatchups = async (userOneID, userTwoID) => {
// pull in all matchup data for the season
const matchupsPromises = [];
for(let i = 1; i < leagueData.settings.playoff_week_start; i++) {
matchupsPromises.push(fetch(`https://api.sleeper.app/v1/league/${curLeagueID}/matchups/${i}`, {compress: true}))
matchupsPromises.push(supabase.from('view_league_matchups').select('*').eq('league_key', curLeagueID)[i])
}
const matchupsRes = await waitForAll(...matchupsPromises);

Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/helperFunctions/universalFunctions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { supabase } from "$lib/utils/supabase";
import { managers as managersObj } from '$lib/utils/leagueInfo';
import { goto } from "$app/navigation";
import { stringDate } from './news';
Expand Down
Loading