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

WIP Switch scraper over to selenium #343

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion src/main/java/api/v1/ListTermsEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import api.*;
import io.javalin.http.Context;
import io.javalin.openapi.*;
import java.util.*;

public final class ListTermsEndpoint extends App.Endpoint {
public String getPath() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/database/SelectTerms.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package database;

import static utils.Nyu.*;

import java.sql.*;
import java.util.*;
import org.slf4j.*;

public final class SelectTerms {
static final String SELECT_TERMS = "SELECT DISTINCT term from schools ORDER BY term";
Expand Down
1 change: 0 additions & 1 deletion src/main/java/utils/ArrayJS.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils;

import java.util.*;
import java.util.function.*;

public final class ArrayJS {
public interface ArrayFunc<T> {
Expand Down
54 changes: 25 additions & 29 deletions src/main/java/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package utils;

import static utils.Try.*;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -10,8 +12,7 @@
import java.util.stream.*;

public final class Utils {
private static final BufferedReader inReader =
new BufferedReader(new InputStreamReader(System.in));
private static final BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));

public static final class Ref<T> {
public T value;
Expand All @@ -29,17 +30,21 @@ public static void profileWait() {
// The profile doesn't actually start automatically, you have to attach
// a different program to this. The goal is simply to prevent the program
// from making progress until the profiler is attached correctly.
// - Albert Liu, Feb 04, 2022 Fri 00:58 EST
Scanner scanner = new Scanner(inReader);
scanner.nextLine();
// - Albert Liu, Feb 04, 2022 Fri 00:58 EST
try (Scanner scanner = new Scanner(inReader)) {
scanner.nextLine();
}
}

public static String readResource(String path) {
InputStream resource = Utils.class.getResourceAsStream(path);

if (resource == null) throw new IllegalArgumentException("Resource doesn't exist: " + path);
if (resource == null)
throw new IllegalArgumentException("Resource doesn't exist: " + path);

return new Scanner(resource, StandardCharsets.UTF_8).useDelimiter("\\A").next();
try (var scanner = new Scanner(resource, StandardCharsets.UTF_8).useDelimiter("\\A")) {
return scanner.next();
}
}

// Read entire file and then get it as a list of lines
Expand Down Expand Up @@ -74,8 +79,7 @@ public static void writeToFileOrStdout(String file, Object value) {
if (file == null) {
System.out.println(value);
} else {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(value.toString() + "\n");
writer.flush();
} catch (IOException e) {
Expand All @@ -85,14 +89,10 @@ public static void writeToFileOrStdout(String file, Object value) {
}

public static String readFromFileOrStdin(String file) {
if (file != null) {
try {
return new Scanner(new FileReader(file)).useDelimiter("\\A").next();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
var reader = file != null ? tcPass(() -> new BufferedReader(new FileReader(file))) : inReader;
try (var scanner = new Scanner(reader).useDelimiter("\\A")) {
return scanner.next();
}
return new Scanner(inReader).useDelimiter("\\A").next();
}

public static DayOfWeek parseDayOfWeek(String dayOfWeek) {
Expand Down Expand Up @@ -126,21 +126,21 @@ public static void setObject(PreparedStatement stmt, int index, Object obj) thro
}

public static String getEnvDefault(String name, String defaultValue) {
String value = System.getenv(name);
var value = System.getenv(name);
if (value == null) {
return defaultValue;
}
return value;
}

public static int getEnvDefault(String name, int defaultValue) {
var value = Try.tcIgnore(() -> Integer.parseInt(System.getenv(name)));
var value = tcIgnore(() -> Integer.parseInt(System.getenv(name)));
return value.orElse(defaultValue);
}

static class NullWrapper {
int type;
Object value;
final int type;
final Object value;

NullWrapper(int type, Object value) {
this.type = type;
Expand All @@ -153,20 +153,16 @@ public static NullWrapper nullable(int type, Object value) {
}

public static void setArray(PreparedStatement stmt, Object... objs) {
int i = 0;
try {
for (i = 0; i < objs.length; i++) {
tcPass(() -> {
for (int i = 0; i < objs.length; i++) {
setObject(stmt, i + 1, objs[i]);
}
} catch (Exception e) {
// System.err.println("at index " + i);
throw new RuntimeException(e);
}
});
}

public static String stackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
var sw = new StringWriter();
var pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
}
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/test/CoursesTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package test;

import static actions.CopyTermFromProduction.*;
import static utils.JsonMapper.*;
import static utils.Nyu.*;

Expand All @@ -11,7 +10,7 @@
public class CoursesTest {
@Test
public void testFeature182() {
var subjects = new String[] {"CSCI-UA", "SCA-UA_1", "MATH-UA"};
var subjects = new String[] { "CSCI-UA", "SCA-UA_1", "MATH-UA" };

var testUrl = "/api/courses/sp2021/";
var app = App.makeApp();
Expand All @@ -35,7 +34,7 @@ public void testFeature182() {

@Test
public void testCoursesInvalidSubject() {
var subjects = new String[] {"CSCI-UAd", "CSCI-d", "mEdew"};
var subjects = new String[] { "CSCI-UAd", "CSCI-d", "mEdew" };

var testUrl = "/api/courses/sp2021/";
var app = App.makeApp();
Expand All @@ -49,10 +48,9 @@ public void testCoursesInvalidSubject() {
Assert.assertEquals(resp.code(), 400);

var body = respBody.string();
var expected =
"{\"status\":400,\"message\":\"the subject \\\""
+ subject
+ "\\\" is invalid for the term Term[semester=sp, year=2021]\"}";
var expected = "{\"status\":400,\"message\":\"the subject \\\""
+ subject
+ "\\\" is invalid for the term Term[semester=sp, year=2021]\"}";

Assert.assertEquals(body.toLowerCase(), expected.toLowerCase());
}
Expand Down