<commit_before>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    <NAME> (<EMAIL>)
 */
package org.jenetics.example.tsp;

import static java.lang.String.format;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.jenetics.util.ISeq;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version !__version__!
 * @since !__version__!
 */
public class Fetch {

	static final String ROUTE_PATH = "/home/fwilhelm/Workspace/Development/Projects/" +
		"Jenetics/org.jenetics.example/src/main/resources/org/jenetics/example/tsp/routes/";

	static final String URL_PATTERN = "http://open.mapquestapi.com/directions/v2/route?" +
		"key=tt8nTqSZ9ni53nhGUnXfGgjn5RYvZ7vq&callback=renderAdvancedNarrative&" +
		"avoids=Limited Access&avoids=Toll road&avoids=Approximate Seasonal Closure&" +
		"avoids=Unpaved&avoids=Ferry&avoids=Country border crossing&outFormat=json&" +
		"routeType=fastest&timeType=1&narrativeType=text&enhancedNarrative=false&" +
		"shapeFormat=raw&generalize=0&locale=en_US&unit=k&" +
		"from=${FROM}0&to=${TO}&" +
		"drivingStyle=2&highwayEfficiency=21.0";

	public static void main(final String[] args) throws Exception {
		final ISeq<Point> points = points();
		for (int i = 0; i < points.length(); ++i) {
			final String from = toString(points.get(i));

			for (int j = i; j < points.length(); ++j) {
				final String to = toString(points.get((j + 1)%points.length()));
				final File routeFile = file(from, to);

				if (!routeFile.isFile()) {
					final URL url = new URL(URL_PATTERN
						.replace("${FROM}", from)
						.replace("${TO}", to)
						.replace(" ", "%20"));

					System.out.println("ROUTE: " + routeFile.getName());
					final String output = GET(url);
					Files.write(routeFile.toPath(), output.getBytes());
				}
			}
		}
	}

	private static File file(final String from, final String to) {
		return new File(
			ROUTE_PATH,
			format("%s--%s.path", from, to));
	}

	private static String GET(final URL url) throws Exception {
		final HttpURLConnection http = (HttpURLConnection)url.openConnection();
		try {
			http.setRequestMethod("GET");
			http.setDoOutput(true);
			http.setRequestProperty("Accept", "application/json");

			/*<commit_msg>Read MapQuest API key from environment variable.<commit_after>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    <NAME> (<EMAIL>)
 */
package org.jenetics.example.tsp;

import static java.lang.String.format;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.jenetics.util.ISeq;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version !__version__!
 * @since !__version__!
 */
public class Fetch {

	static final String ROUTE_PATH = "/home/fwilhelm/Workspace/Development/Projects/" +
		"Jenetics/org.jenetics.example/src/main/resources/org/jenetics/example/tsp/routes/";

	static final String URL_PATTERN = "http://open.mapquestapi.com/directions/v2/route?" +
		"key=${MAP_QUEST_API_KEY}&callback=renderAdvancedNarrative&" +
		"avoids=Limited Access&avoids=Toll road&avoids=Approximate Seasonal Closure&" +
		"avoids=Unpaved&avoids=Ferry&avoids=Country border crossing&outFormat=json&" +
		"routeType=fastest&timeType=1&narrativeType=text&enhancedNarrative=false&" +
		"shapeFormat=raw&generalize=0&locale=en_US&unit=k&" +
		"from=${FROM}0&to=${TO}&" +
		"drivingStyle=2&highwayEfficiency=21.0";

	public static void main(final String[] args) throws Exception {
		final ISeq<Point> points = points();
		for (int i = 0; i < points.length(); ++i) {
			final String from = toString(points.get(i));

			for (int j = i; j < points.length(); ++j) {
				final String to = toString(points.get((j + 1)%points.length()));
				final File routeFile = file(from, to);

				if (!routeFile.isFile()) {
					final URL url = new URL(URL_PATTERN
						.replace("${MAP_QUEST_API_KEY}", System.getenv("MAP_QUEST_API_KEY"))
						.replace("${FROM}", from)
						.replace("${TO}", to)
						.replace(" ", "%20"));

					System.out.println("ROUTE: " + routeFile.getName());
					final String output = GET(url);
					Files.write(routeFile.toPath(), output.getBytes());
				}
			}
		}
	}

	private static File file(final String from, final String to) {
		return new File(
			ROUTE_PATH,
			format("%s--%s.path", from, to));
	}

	private static String GET(final URL url) throws Exception {
		final HttpURLConnection http = (HttpURLConnection)url.openConnection();
		try {
			http.setRequestMethod("GET");
			http.setDoOutput(true);
			http.setRequestProperty("Accept", "application/json");

			/*
{'fraction_non_alphanumeric': 0.1416746259153136, 'fraction_numerical': 0.0060490289716650745, 'mean_word_length': 4.388507718696398, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4941334', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2023, 'n_words': 508}