Skip to content

Commit

Permalink
Merge pull request #107 from mimotej/fix-thesis-scraping-muni
Browse files Browse the repository at this point in the history
Fix confirmation scraping muni and log issues
  • Loading branch information
mimotej authored Feb 28, 2024
2 parents 9a44b5b + 86a25ea commit 9023ba7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/commands/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ export async function execute(interaction: ChatInputCommandInteraction) {
const userId = interaction.user.id;
const textReview = interaction.options.getString('text');
if (textReview == undefined || subjectCode == undefined) {
console.log('Error with arguments for create review command.');
console.log('LOG: Error with arguments for create review command.');
return interaction.editReply({
content: `Error while creating a review for ${subjectCode}.`,
});
}
console.log(
`LOG: Checking if user ${userId} has already submitted review for ${subjectCode}.`
);
const existingReview = await prisma.reviews.findMany({
where: {
discordUserId: {
Expand All @@ -70,13 +73,13 @@ export async function execute(interaction: ChatInputCommandInteraction) {
},
},
});
console.log(existingReview);
if (existingReview.length != 0) {
return interaction.editReply({
content: `You have already submitted review from subject ${subjectCode}.`,
});
}
try {
console.log(`LOG: Creating review for subject ${subjectCode}.`);
await prisma.reviews.create({
data: {
discordUserId: userId,
Expand All @@ -85,7 +88,7 @@ export async function execute(interaction: ChatInputCommandInteraction) {
},
});
} catch (err) {
console.log(`Database error: ${err}`);
console.log(`LOG: Database error: ${err}`);
return interaction.editReply({
content: `Error while creating a review for ${subjectCode}.`,
});
Expand All @@ -102,11 +105,17 @@ export async function execute(interaction: ChatInputCommandInteraction) {
subjectCode: subjectCode?.toUpperCase(),
},
});
console.log(
`LOG: Submitted reviews for ${subjectCode}: ${submittedReviews}.`
);
if (submittedReviews.length == 0) {
return interaction.editReply({
content: `No reviews for ${subjectCode?.toUpperCase()} :(`,
});
}
console.log(
`LOG: Building embed for review ${submittedReviews[0].id}.`
);
const title = `Reviews for course ${subjectCode}`;
const reviewer = await interaction.client.users.fetch(
submittedReviews[0].discordUserId
Expand Down
4 changes: 4 additions & 0 deletions src/commands/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export async function execute(interaction: ChatInputCommandInteraction) {
});
let user;
try {
console.log(
`LOG: Checking if user ${interaction.user.id} is already registered.`
);
user = await prisma.users.findMany({
where: {
discordId: interaction.user.id,
Expand All @@ -71,6 +74,7 @@ export async function execute(interaction: ChatInputCommandInteraction) {
if (thesisId === undefined) {
thesisId = bachelorThesisParsedUrl.pathname.split('/')[2];
}
console.log(`LOG: Checking if thesis ${thesisId} exists in database.`);
user = await prisma.users.findMany({
where: {
idThesis: thesisId,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/assignRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function assignRole(
bachelorThesisParsedUrl: URL
) {
const today = new Date();
console.log('LOG: Assigning role to user.');
if (
scrapedConfirmationStudy[
'Status of studies as of ' +
Expand Down Expand Up @@ -56,6 +57,9 @@ export async function assignRole(
if (idThesis === undefined) {
idThesis = bachelorThesisParsedUrl.pathname.split('/')[2];
}
console.log(
`LOG: Creating user in database with thesis ${idThesis}.`
);
await prisma.users.create({
data: {
discordId: interaction.user.id,
Expand All @@ -67,6 +71,7 @@ export async function assignRole(
console.log(`Database error: ${err}`);
return undefined;
}
console.log(`LOG: Assigning role to user ${interaction.user.id}.`);
if (roleVerified && roleProgramm) {
member.roles.add(roleVerified);
member.roles.add(roleProgramm);
Expand Down
5 changes: 4 additions & 1 deletion src/utils/scrapeConfirmationStudies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { load } from 'cheerio';
*/
export async function scrapeConfirmationStudies(pathname: string) {
let response;
console.log('LOG: Scraping confirmation of studies.');
try {
response = await axios.get(
'https://is.muni.cz' + pathname + '?lang=en'
Expand All @@ -15,9 +16,11 @@ export async function scrapeConfirmationStudies(pathname: string) {
return null;
}
const $ = load(response.data);
console.log('LOG: Extracting user information from the confirmation page.');
const userInfo = $(
'#el_potvrzeni > div.column.small-12.medium-9.large-9 > div'
'#el_potvrzeni > div.column.small-12.medium-8.large-8 > div'
);
console.log(`LOG: Parsed data from the page ${userInfo}.`);
const children = userInfo
.children()
.map((_, el) => $(el).text())
Expand Down
3 changes: 3 additions & 0 deletions src/utils/scrapeThesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { load } from 'cheerio';
*/
export async function scrapeThesis(bachelorThesisPath: string) {
let response;
console.log('LOG: Scraping author name from the thesis.');
try {
response = await axios.get(
'https://dspace.vutbr.cz' + bachelorThesisPath
Expand All @@ -14,9 +15,11 @@ export async function scrapeThesis(bachelorThesisPath: string) {
return null;
}
const $ = load(response.data);
console.log('LOG: Parsing author name from the page.');
const authorName = $(
'ds-metadata-representation-list.ds-item-page-mixed-author-field:nth-child(4) > ds-metadata-field-wrapper:nth-child(1) > div:nth-child(1) > div:nth-child(2) > ds-metadata-representation-loader:nth-child(1) > ds-plain-text-metadata-list-element:nth-child(1) > div:nth-child(1) > a:nth-child(1)'
).text();
console.log(`LOG: Parsed author name from the page ${authorName}.`);
return authorName
.split(',')
.reverse()
Expand Down

0 comments on commit 9023ba7

Please sign in to comment.