660b060ed3e5 — Andrew@edwin8 tip 2 years ago
Collect and average the reviews.  Sort by average review.

Becomes kinda useful at this point.
M src/main/java/com/ajtjp/trailaggregator/Main.java +37 -4
@@ 21,7 21,7 @@ import org.jsoup.select.Elements;
 public class Main {
     public static void main(String[] args) {
         try {
-            Document ohio = Jsoup.connect("https://www.traillink.com/state/oh-trails/").get();
+            Document ohio = Jsoup.connect("https://www.traillink.com/state/wv-trails/").get();
             Elements trailResults = ohio.select(".search-result-card");
             
             Object longTrails = trailResults.stream().filter(new LengthFilter()).filter(new TrailConglomerationFilter()).collect(Collectors.toList());

          
@@ 43,21 43,54 @@ public class Main {
                 }
             });
             
+            
+            System.out.println("There are " + longTrailList.size() + " long trails in West Virginia");
+            
             List<Trail> trails = new ArrayList<>();
             longTrailList.iterator().forEachRemaining((elementTrail) -> {
                 String name = elementTrail.selectFirst(".info h3").text();
                 float length = Float.parseFloat(elementTrail.selectFirst(".length").text().split(" ")[0]);
                 float initialRating = elementTrail.select(".rating i").size();
+                String href = elementTrail.selectFirst(".info a").attr("href");
                 
-                Trail trail = new Trail(name, length, initialRating);
+                Trail trail = new Trail(name, length, initialRating, href);
                 trails.add(trail);
+                
+                //Now let's get even more data
+                String fullURL = "https://www.traillink.com" + href;
+                try {
+                    Document details = Jsoup.connect(fullURL).get();
+                    Elements ratedReviews = details.select(".review .stars [itemprop='ratingValue']");
+                    trail.setReviewCount(ratedReviews.size());
+                    float totalScore = 0;
+                    for (Element review : ratedReviews) {
+                        int rating = Integer.parseInt(review.attr("content"));
+                        totalScore += rating;
+                    }
+                    float averageScore = totalScore / ratedReviews.size();
+                    trail.setRating(averageScore * 2);
+                    System.out.print(".");
+                }
+                catch(IOException ex) {
+                    System.err.println("Could not get details for " + name + " at " + fullURL);
+                }
+            });
+            System.out.println();
+            
+            trails.sort(new Comparator(){
+                @Override
+                public int compare(Object o1, Object o2) {
+                    Trail one = (Trail)o1;
+                    Trail two = (Trail)o2;
+                    return one.getRating() < two.getRating() ? 1 : -1;
+                }
             });
             
-            System.out.println("There are " + trails.size() + " long trails in Ohio");
             for (Trail trail : trails) {
-                System.out.println("  " + trail.getName() + " (" + trail.getLength() + " miles), " + trail.getRating());
+                System.out.println("  " + trail.getName() + " (" + trail.getLength() + " miles), " + trail.getFormattedRating() + " [" + trail.getReviewCount() + "]");
             }
             
+            
         } catch (IOException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         }

          
M src/main/java/com/ajtjp/trailaggregator/Trail.java +26 -1
@@ 1,6 1,8 @@ 
 
 package com.ajtjp.trailaggregator;
 
+import java.util.Formatter;
+
 /**
  * Represents a Trail, and its various attributes.
  * @author Andrew

          
@@ 9,11 11,14 @@ public class Trail {
     private String name;
     private float length;
     private float rating;
+    private int reviewCount;
+    private String href;
 
-    public Trail(String name, float length, float rating) {
+    public Trail(String name, float length, float rating, String href) {
         this.name = name;
         this.length = length;
         this.rating = rating;
+        this.href = href;
     }
 
     public String getName() {

          
@@ 35,8 40,28 @@ public class Trail {
     public float getRating() {
         return rating;
     }
+    
+    public String getFormattedRating() {
+        return String.format("%.2f", rating);
+    }
 
     public void setRating(float rating) {
         this.rating = rating;
     }
+
+    public String getHref() {
+        return href;
+    }
+
+    public void setHref(String href) {
+        this.href = href;
+    }
+
+    public int getReviewCount() {
+        return reviewCount;
+    }
+
+    public void setReviewCount(int reviewCount) {
+        this.reviewCount = reviewCount;
+    }
 }