6353232b87b6 — Andrew@edwin8 2 years ago
Find trails in Ohio, filter them, and sort them by length.
3 files changed, 112 insertions(+), 0 deletions(-)

A => .hgignore
A => pom.xml
A => src/main/java/com/ajtjp/trailaggregator/Main.java
A => .hgignore +5 -0
@@ 0,0 1,5 @@ 
+\.orig$
+\.orig\..*$
+\.chg\..*$
+\.rej$
+\.conflict\~$

          
A => pom.xml +20 -0
@@ 0,0 1,20 @@ 
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.ajtjp</groupId>
+    <artifactId>TrailAggregator</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <packaging>jar</packaging>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.jsoup</groupId>
+            <artifactId>jsoup</artifactId>
+            <version>1.11.2</version>
+        </dependency>
+    </dependencies>
+</project>
  No newline at end of file

          
A => src/main/java/com/ajtjp/trailaggregator/Main.java +87 -0
@@ 0,0 1,87 @@ 
+
+package com.ajtjp.trailaggregator;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+/**
+ *
+ * @author Andrew
+ */
+public class Main {
+    public static void main(String[] args) {
+        try {
+            Document ohio = Jsoup.connect("https://www.traillink.com/state/oh-trails/").get();
+            Elements trails = ohio.select(".search-result-card");
+            
+            Object longTrails = trails.stream().filter(new LengthFilter()).filter(new TrailConglomerationFilter()).collect(Collectors.toList());
+            List<Element> longTrailList = (List<Element>)longTrails;
+            
+            longTrailList.sort(new Comparator(){
+                @Override
+                public int compare(Object o1, Object o2) {
+                    Element one = (Element)o1;
+                    Element two = (Element)o2;
+                    float oneLength = Float.parseFloat(one.selectFirst(".length").text().split(" ")[0]);
+                    float twoLength = Float.parseFloat(two.selectFirst(".length").text().split(" ")[0]);
+                    if (oneLength > twoLength) {
+                        return -1;
+                    }
+                    else {
+                        return 1;
+                    }
+                }
+            });
+            
+            System.out.println("There are " + longTrailList.size() + " long trails in Ohio");
+            for (Element trail : longTrailList) {
+                System.out.println("  " + trail.selectFirst(".info h3").text() + " (" + trail.selectFirst(".length").text().split(" ")[0] + " miles)");
+            }
+            
+        } catch (IOException ex) {
+            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+        }
+    }
+    
+    /**
+     * Filters out trails that are composed of multiple other trails.
+     * This is a bit of a challenge because sometimes the combination is more desirable.
+     * E.g. What do you do for the Ohio to Erie Trail?  A lot of its segments are small,
+     * but the whole thing is quite large.  The sum is somewhere you might go, but the
+     * individual parts sometimes are, and sometimes are not.
+     * 
+     * For now, my plan is to only exclude multi-state conglomerations, primarily
+     * the Great American Rail-Trail and the East Coast Greenway.  These are the
+     * ones that are too long for most people to plan a vacation around.
+     */
+    private static class TrailConglomerationFilter implements Predicate {        
+        @Override
+        public boolean test(Object t) {
+            Element e = (Element)t;
+            String name = e.selectFirst(".info h3").text();
+            if (name.equals("Great American Rail-Trail")) {
+                return false;
+            }
+            return true;
+        }
+    }
+
+    private static class LengthFilter implements Predicate {
+        @Override
+        public boolean test(Object t) {
+            Element e = (Element)t;
+            String lengthText = e.selectFirst(".length").text();
+            float miles = Float.parseFloat(lengthText.split(" ")[0]);
+            return miles >= 12.5;
+        }
+    }
+}