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

Updates Photos on activities page #185

Closed
wants to merge 14 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: add headers for browser request and improve error handling in hu…
…stle API
2004yash committed Jan 24, 2025
commit 04e318b729780273e8ebb46a8f005b3a81973e4b
47 changes: 41 additions & 6 deletions app/(default)/api/hustle/route.ts
Original file line number Diff line number Diff line change
@@ -31,10 +31,30 @@ export async function POST() {
process.env.VJUDGE_CONTEST_API ||
"https://vjudge.net/contest/data?draw=2&start=0&length=20&sortDir=desc&sortCol=0&category=mine&running=3&title=&owner=Pbhustle&_=1733642420751";

const { data } = await axios.get(API_URL);
// Add headers to mimic a browser request
const headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9',
'Referer': 'https://vjudge.net/',
'Origin': 'https://vjudge.net',
'Cookie': process.env.VJUDGE_COOKIE || 'JSESSlONID=7YP9VCUCK4ZTTTNQO0U0OQFW6ZDHMJXV; _ga=GA1.1.533089565.1728634731; Jax.Q=04yash|PMVRLFQGIPE5GOTC1SGE6B4W785LQJ; __gads=ID=99f1126d6fe46914:T=1728634731:RT=1737718428:S=ALNI_Mb_KPRQ0uquyzQSN44fpJKwUo53GA; __gpi=UID=00000f3e7503e3cd:T=1728634731:RT=1737718428:S=ALNI_Mb0ueGqLg2jRi_frTwTAZsUQqg_wQ; __eoi=ID=9732350f6625bc1b:T=1728634731:RT=1737718428:S=AA-AfjZnX0ZhSiL1BHBPEMWVFpeh; cf_clearance=rHe4M.4i47txtgYJQPXs0a.LnH.QYSRELQUQKvBeHv0-1737728607-1.2.1.1-QS6DaWVn4ZlR0CDzelDl644mlMOCZU1ty_NqdHf68O5PjZEOniNFWXUEEk3GrVQj0CwVlOScM6DpclcDaZKTv39_vxVd.tdzDDdfI3hBk.BKBOopC_pgojfmWDpHxvMqGBJbqfcoq36wQJQchRX4B0.IUalcf8OnqtyKwFa6mOj1a1oNoBst3jz_nVLMzWjzzQsxtJmhLlSGveAigCmovzVeHuJsXGSGifWipcLBKO2U_QCnzDVWlZ5N0Rqs7qlOPhzT9xmvceNQGlIZLoINYpq5_WyfCbumeC6XsX0i.H4; FCNEC=%5B%5B%22AKsRol-aSOdWtkNjxLPUwAZiyc5kmxDI81NA-AWYxiD_dMfHxJZ0hX5MBVRm9H0Pb0bLbRu7vmWOG3ZJAKwynbFz-3CLj98y_Ps-u7uC3PX4myF02jFz23muu0K9r3Xot0YQRKs-gKcJoQetbuaLAVrOLTIdtXKr4w%3D%3D%22%5D%5D; JSESSIONID=E55418E82CB952060BE04F7A459FD1FF; _ga_374JLX1715=GS1.1.1737728605.51.1.1737729138.60.0.0',
};

const { data } = await axios.get(API_URL, { headers });

if (!data || !data.data || !data.data[0]) {
console.error("Invalid response format from VJudge");
return NextResponse.json({
error: "Invalid response from VJudge",
details: "No contest data available"
}, { status: 400 });
}

const ccode = data.data[0][0];
const { data: rankData } = await axios.get(
`https://vjudge.net/contest/rank/single/${ccode}`
`https://vjudge.net/contest/rank/single/${ccode}`,
{ headers }
);

const leaderboardDoc = await LeaderboardModel.findOne({
@@ -113,8 +133,19 @@ export async function POST() {
message: "Leaderboard updated successfully",
rankings: leaderboardRankings,
});
} catch (error) {
return NextResponse.json({ error});
} catch (error: any) {
if (axios.isAxiosError(error)) {
console.error("API Error:", error.response?.data || error.message);
} else {
console.error("API Error:", error);
}
return NextResponse.json({
error: "Failed to update leaderboard",
details: error.response?.data || error.message,
status: error.response?.status || 500
}, {
status: error.response?.status || 500
});
}
}

@@ -133,7 +164,11 @@ export async function GET() {
leaderboard: leaderboardDoc,
},
});
} catch (error) {
return NextResponse.json({error});
} catch (error: any) {
console.error("Database error:", error);
return NextResponse.json({
error: "Failed to fetch hustle data",
details: error.message
}, { status: 500 });
}
}