Skip to content

Commit 0408077

Browse files
authored
Merge pull request #1 from lfsrrr/add-function
app function to Main.java
2 parents d25c8e6 + 2a16995 commit 0408077

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/Main.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
1+
import java.io.BufferedReader;
2+
import java.io.FileWriter;
3+
import java.io.InputStreamReader;
4+
import java.io.IOException;
5+
import java.util.Scanner;
6+
17
public class Main {
28
public static void main(String[] args) {
3-
System.out.println("Hello world!");
9+
Scanner scanner = new Scanner(System.in); // Create a Scanner object
10+
System.out.println("Enter Studiengang:");
11+
String studiengang = scanner.nextLine(); // Read user input
12+
13+
// Construct the curl command using the user input
14+
String[] curlCommand = {
15+
"curl",
16+
"-X", "GET",
17+
"https://raumzeit.hka-iwi.de/api/v1/timetables/public/" + studiengang,
18+
"-H", "accept: text/calendar"
19+
};
20+
21+
try {
22+
// Execute the curl command using ProcessBuilder
23+
ProcessBuilder processBuilder = new ProcessBuilder(curlCommand);
24+
Process process = processBuilder.start();
25+
26+
// Read the output of the curl command
27+
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
28+
StringBuilder output = new StringBuilder();
29+
String line;
30+
31+
while ((line = reader.readLine()) != null) {
32+
output.append(line).append("\n");
33+
}
34+
35+
// Wait for the process to complete
36+
int exitCode = process.waitFor();
37+
if (exitCode == 0) {
38+
// Write the API response to a .ics file
39+
String fileName = studiengang + ".ics";
40+
try (FileWriter fileWriter = new FileWriter(fileName)) {
41+
fileWriter.write(output.toString());
42+
System.out.println("API response saved to " + fileName);
43+
}
44+
} else {
45+
System.err.println("Error: API call failed with exit code " + exitCode);
46+
}
47+
48+
} catch (IOException | InterruptedException e) {
49+
e.printStackTrace(); // Handle any exceptions
50+
}
451
}
552
}

0 commit comments

Comments
 (0)