Android Open Source - UTMShuttleAndroid Scraper






From Project

Back to project page UTMShuttleAndroid.

License

The source code is released under:

GNU General Public License

If you think the Android project UTMShuttleAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package util;
//from  www.j  a v a  2s . co m
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import classes.Route;
import classes.Routes;

public class Scraper {

    private final static String URL =
            "http://m.utm.utoronto.ca/shuttle.php";
    private final static String URL_DATE =
            "http://m.utm.utoronto.ca/shuttleByDate.php?month=%s&day=%s&year=%s";
    private final static String DATE_FORMAT =
            "dd-MM-yyyy";

    /**
     * Retrieves the notice information and returns it as a String.
     *
     * @return The notice information as a String.
     */
    public static String getNotices() {
        try {
            Document doc = Jsoup.connect(URL).get();

            // Get notice message
            String notice = "";

            Elements elNotice = doc.select(".notice");
            for (Element noticeNode : elNotice) {
                notice += noticeNode.text();
            }

            return notice;
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * Retrieves the route information as a single Routes object, if the given
     * date is valid.
     *
     * @param month The month to check.
     * @param date The date to check.
     * @param year The year to check.
     * @return A Routes object with all the available route information for the
     *         given date.
     */
    public static Routes getDataByDate(String month, String date, String year) {
        String checkDate = String.format("%s-%s-%s", date, month, year);

        if (isDateValid(checkDate)) {
            try {
                Document doc = Jsoup.connect(
                        String.format(URL_DATE, month, date, year)).get();

                Routes routes = new Routes();

                // Get route options
                Elements elRoutes = doc.select("#chooseRoute option");

                if (elRoutes.isEmpty()) {
                    return null;
                }

                for (Element routeNode : elRoutes) {
                    int routeID = Integer.valueOf(routeNode.attr("value"));
                    String routeName = routeNode.text();
                    routes.addRoute(new Route(routeID, routeName));
                }

                // Get corresponding route times
                Elements elLists = doc.select(".list");
                for (Element listNode : elLists) {
                    int routeID = Integer.valueOf(listNode.id());
                    Route route = routes.getRoute(routeID);

                    for (Element time : listNode.children()) {
                        route.addTime(time.text());
                    }
                }

                return routes;
            } catch (IOException e) {
                return null;
            }
        }

        return null;
    }

    /**
     * Checks if the given date is valid.
     *
     * @param date The date to check, in DATE_FORMAT format.
     * @return True iff the given date is valid.
     */
    private static boolean isDateValid(String date) {
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            df.setLenient(false);
            df.parse(date);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

}




Java Source Code List

activities.MainActivity.java
adapters.TimesListAdapter.java
async.NoticesTask.java
async.RoutesTask.java
classes.Route.java
classes.Routes.java
global.App.java
util.NetworkHelper.java
util.Scraper.java