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

Liubrian267/issue57 #60

Open
wants to merge 4 commits into
base: dev
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
12 changes: 12 additions & 0 deletions scraper/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from scrapers import rmp

def main():
rmp_professors = rmp.get_professors() # Assuming rmp.get_professors() retrieves data
#print(rmp_professors)
if rmp_professors:
print("RMP Professors:")
for professor in rmp_professors:
print(professor)
else:
print("No professor data found in the response.")
main()
Binary file added scraper/scrapers/__pycache__/rmp.cpython-311.pyc
Binary file not shown.
74 changes: 74 additions & 0 deletions scraper/scrapers/rmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import requests

RMP_URL = "https://www.ratemyprofessors.com/graphql"
AUTH_USERNAME = "test"
AUTH_PASSWORD = "test"

query = """

query TeacherSearchPaginationQuery(
$query: TeacherSearchQuery!
) {
search: newSearch {
...TeacherSearchPagination_search_1jWD3d
}
}

fragment TeacherSearchPagination_search_1jWD3d on newSearch {
teachers(query: $query, first: 2, after: "") {
edges {
node {
...TeacherCard_teacher
#id <- school id
}
}
resultCount
}
}

fragment TeacherCard_teacher on Teacher {
id
# legacyId
avgRating
numRatings
...CardFeedback_teacher
...CardSchool_teacher
...CardName_teacher
}

fragment CardFeedback_teacher on Teacher {
wouldTakeAgainPercent
avgDifficulty
}

fragment CardSchool_teacher on Teacher {
department
}

fragment CardName_teacher on Teacher {
firstName
lastName
}



"""
variables = {
"query": {
"text": "",
"schoolID": "U2Nob29sLTg4MQ==",
"fallback": True,
"departmentID": None
},
"count": 1,
"cursor": "YXJyYXljb25uZWN0aW9uOjIz"
}
def get_professors():
response = requests.post(RMP_URL, json={"query": query, "variables": variables}, auth=(AUTH_USERNAME, AUTH_PASSWORD))
teachers = response.json()['data']['search']['teachers']['edges']
# print(teachers)
# print(response.json())
# teachers = response['data']['search']['teachers']['edges']
return teachers
#

5 changes: 5 additions & 0 deletions scraper/scrapers/tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
city
state
country
legacyId
teachers
7 changes: 7 additions & 0 deletions scraper/tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if rmp_professors:
print("RMP Professors:")
for professor in rmp_professors['data']['search']['teachers']['edges']:
professor_data = professor['node']
print(professor_data)
else:
print("No professor data found in the response.")
61 changes: 61 additions & 0 deletions src/lib/components/ProfessorSearch.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script lang="ts" context="module">
export interface Professor {
id: number;
name: string;
}
</script>

<script lang="ts">
import type { ProfSearchSchema } from '$lib/forms/schema';
import { superForm, type Infer, type SuperValidated } from 'sveltekit-superforms';
import { goto } from '$app/navigation';

interface Props {
professors: Professor[];
profFormData: SuperValidated<Infer<ProfSearchSchema>>;
}

let { professors, profFormData }: Props = $props();

const { form } = superForm(profFormData, {
invalidateAll: false,
resetForm: false
});

let suggestions = $state<Professor[]>([]);

function handleInput(event: Event) {
const inputValue = (event.target as HTMLInputElement).value;
if (inputValue.trim().length > 0) {
suggestions = professors.filter((professor) =>
professor.name.toLowerCase().includes(inputValue.toLowerCase())
);
} else {
suggestions = [];
}
}

function handleSubmit(event: Event) {
event.preventDefault();
goto(`./professors/${$form.profName}`);
}
</script>

<form method="POST" class="flex items-center space-x-4" onsubmit={handleSubmit}>
<input
type="text"
placeholder="Type professor name"
class="input input-bordered w-full max-w-xs"
name="profName"
bind:value={$form.profName}
list="profNames"
oninput={handleInput}
/>
<datalist id="profNames">
{#each suggestions as suggestion}
<option>{suggestion.name}</option>
{/each}
</datalist>

<button class="btn" type="submit" disabled={!$form.profName}>Search</button>
</form>
5 changes: 5 additions & 0 deletions src/lib/forms/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ export const searchSchema = z.object({
courseNumber: z.string()
});

export const profSearchSchema = z.object({
profName: z.string()
});

export type SearchSchema = typeof searchSchema;
export type ProfSearchSchema = typeof profSearchSchema;
18 changes: 15 additions & 3 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { type RequestEvent } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';

import { db } from '$lib/db/db.server';
import { coursesTable } from '$lib/db/schema';
import { coursesTable, professorsTable } from '$lib/db/schema';
import { asc } from 'drizzle-orm';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { searchSchema } from '$lib/forms/schema';
import { profSearchSchema } from '$lib/forms/schema';

export const load: PageServerLoad = async (event: RequestEvent) => {
const result = await db
Expand All @@ -20,13 +21,24 @@ export const load: PageServerLoad = async (event: RequestEvent) => {
.from(coursesTable)
.orderBy(asc(coursesTable.courseNumber));

const profData = await db
.select({
id: professorsTable.id,
name: professorsTable.name
})
.from(professorsTable);

const form = await superValidate(zod(searchSchema));
const profForm = await superValidate(zod(profSearchSchema));

if (event.locals.user) {
return {
user: event.locals.user,
form,
allCourseData: result
profForm,
allCourseData: result,
professorData: profData
};
}
return { form, allCourseData: result };
return { form, profForm, allCourseData: result, professorData: profData };
};
5 changes: 3 additions & 2 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { redirect, type Actions } from '@sveltejs/kit';
import { zod } from 'sveltekit-superforms/adapters';
import { searchSchema } from '$lib/forms/schema';
import { profSearchSchema, searchSchema } from '$lib/forms/schema';
import { superValidate } from 'sveltekit-superforms';
import { db } from '$lib/db/db.server';
import { coursesTable } from '$lib/db/schema';
import { and, eq } from 'drizzle-orm';

export const load = async () => {
const form = await superValidate(zod(searchSchema));
return { form };
const profForm = await superValidate(zod(profSearchSchema));
return { form, profForm };
};

export const actions: Actions = {
Expand Down
5 changes: 4 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<script lang="ts">
import Search from '$lib/components/Search.svelte';

import ProfessorSearch from '$lib/components/ProfessorSearch.svelte';
let { data } = $props();
let courses = $derived(data.allCourseData);
let form = $derived(data.form);
let professors = $derived(data.professorData);
let profForm = $derived(data.profForm);
</script>

<div class="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 transform">
<Search {courses} formData={form} />
<ProfessorSearch {professors} profFormData={profForm} />
</div>
1 change: 0 additions & 1 deletion src/routes/dev-login/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { goto, invalidateAll } from '$app/navigation';
import { toast } from 'svelte-sonner';
</script>

Expand Down