IMDb Scraper #1

Merged
reyhne merged 1 commits from develop into main 2026-06-26 15:15:40 +00:00
3 changed files with 83 additions and 59 deletions
+31 -13
View File
@@ -1,22 +1,40 @@
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String[] args) {
// The relative path automatically points to the resources folder
public static void main(String[] args)
{
String filePath = "src/main/resources/Movies.html";
try {
// Instantiate the parser
try
{
Parser parser = new Parser(filePath);
// TODO: You can test your code here before you run the unit tests.
// Example: Call your sorting methods, iterate through the returned list,
// and print out the movies to see if they match the expected results in the Help folder.
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size());
} catch (IOException e) {
System.out.println("--------------------------------------------------");
System.out.println("Sorted by Title:");
printList(parser.sortByTitle());
System.out.println("--------------------------------------------------");
System.out.println("Sorted by Rating (Descending):");
printList(parser.sortByRating());
System.out.println("--------------------------------------------------");
System.out.println("Sorted by Year (Descending):");
printList(parser.sortByYear());
}
catch (IOException e)
{
System.err.println("Error reading or parsing the HTML file: " + e.getMessage());
}
}
}
private static void printList(List<Movie> movies)
{
for (Movie m : movies) {System.out.println(m);}
System.out.println();
}
}
+16 -20
View File
@@ -5,35 +5,31 @@ public class Movie {
private double rating;
private int year;
public Movie(String title, double rating, int year) {
// TODO: Initialize the instance variables (this.title, this.rating, this.year)
// with the values passed as arguments to this constructor.
public Movie(String title, double rating, int year)
{
this.title = title;
this.rating = rating;
this.year = year;
}
public String getTitle() {
return title;
}
public String getTitle() {return title;}
public double getRating() {
return rating;
}
public double getRating() {return rating;}
public int getYear() {
return year;
public int getYear() {return year;}
@Override
public String toString()
{
return "Movie: " + title + ", Rating: " + rating + ", Year: " + year;
}
@Override
public String toString() {
// TODO: Return a string representation of the movie matching the exact required format.
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
return "";
}
@Override
public boolean equals(Object o) {
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title);
}
}
}
+36 -26
View File
@@ -6,47 +6,57 @@ import java.io.File;
import java.io.IOException;
import java.util.*;
public class Parser {
public class Parser
{
private List<Movie> movies = new ArrayList<>();
public Parser(String filePath) throws IOException {
setUp(filePath);
}
public Parser(String filePath) throws IOException {setUp(filePath);}
public List<Movie> sortByTitle() {
public List<Movie> sortByTitle()
{
List<Movie> sortedByTitle = new ArrayList<>(movies);
// TODO: Sort the 'sortedByTitle' list alphabetically by the movie's title.
// Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator.
// Example: Comparator.comparing(Movie::getTitle)
sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
return sortedByTitle;
}
public List<Movie> sortByRating() {
public List<Movie> sortByRating()
{
List<Movie> sortedByRating = new ArrayList<>(movies);
// TODO: Sort the 'sortedByRating' list by the movie's rating in descending order (highest to lowest).
// Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed().
sortedByRating.sort(Comparator.comparingDouble(Movie::getRating).reversed());
return sortedByRating;
}
public List<Movie> sortByYear() {
public List<Movie> sortByYear()
{
List<Movie> sortedByYear = new ArrayList<>(movies);
// TODO: Sort the 'sortedByYear' list by the movie's release year in descending order (newest to oldest).
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
sortedByYear.sort(Comparator.comparingInt(Movie::getYear).reversed());
return sortedByYear;
}
private void setUp(String filePath) throws IOException {
// TODO: Create a java.io.File object using the given 'filePath'.
// TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object.
private void setUp(String filePath) throws IOException
{
File input = new File(filePath);
// TODO: Extract the list of movie elements from the Document.
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
Document doc = Jsoup.parse(input, "UTF-8");
// TODO: Iterate through each movie Element.
// For each movie element:
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text.
// 2. Find the rating element (e.g., using selector "span.movie-rating"). Extract the text, remove the "/10" part, and parse it to a double.
// 3. Find the year element (e.g., using selector "span.movie-year"). Extract the text and parse it to an int.
// 4. Create a new Movie object with these values and add it to the 'movies' list.
Elements movieElements = doc.select(".movie");
for (Element e : movieElements)
{
String title = e.select(".movie-title").text();
String ratingText = e.select(".movie-rating").text().replace("/10", "");
double rating = Double.parseDouble(ratingText);
int year = Integer.parseInt(e.select(".movie-year").text());
movies.add(new Movie(title, rating, year));
}
}
}
}