<commit_before>/**
 * Copyright 2013 <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.
 */

package fr.cph.stock.external;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import fr.cph.stock.exception.YahooException;

/**
 * This class take care of the connexion to Yahoo API. It uses YQL language.
 * 
 * @author <NAME>
 * 
 */
public class Yahoo {
	/** Logger **/
	private static final Logger LOG = Logger.getLogger(Yahoo.class);
	/** Url base **/
	private static final String URL_BASE = "http://query.yahooapis.com/v1/public/yql?q=";
	/** Url end **/
	private static final String URL_END = "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc";
	/** The request **/
	private String request;

	/**
	 * Constructor that build a yahoo object with the given request
	 * 
	 * @param req
	 *            the request
	 */
	public Yahoo(final String req) {
		this.request = req;
	}

	/**
	 * This function build the URL.
	 * 
	 * @return a url
	 * @throws YahooException
	 *             the yahoo exception
	 */
	protected final String urlBuilder() throws YahooException {
		try {
			return URL_BASE + URLEncoder.encode(request, "UTF-8") + URL_END;
		} catch (UnsupportedEncodingException e) {
			throw new YahooException("Error: " + YahooException.ENCODE_ERROR, e);
		}
	}

	/**
	 * THis function connect to the given address and return in a string the content of the page
	 * 
	 * @param address
	 *            the address
	 * @return the content of the page
	 * @throws YahooException
	 *             the yahoo exception
	 */
	protected final String connectUrl(final String address) throws YahooException {
		LOG.debug("URL " + address);
		String toreturn = null;
		try {
			URL url = new URL(address);
			URLConnection uc = url.openConnection();
			Charset charset = Charset.forName("UTF8");
			InputStreamReader in = new InputStreamReader(uc.getInputStream(), charset);
			int c = in.read();
			StringBuilder build = new StringBuilder();
			while (c != -1) {
				build.append((char) c);
				c = in.read();
			}
			toreturn = build.toString();
			in.close();
		} catch (IOException e) {
			throw new YahooException("Error: " + YahooException.CONNECT_ERROR, e);
		}
		return toreturn;
	}

	/**
	 * Convert the string to a json object, removing the unwanted part
	 * 
	 * @param data
	 *            the data to convert
	 * @return a JSONObject
	 */
	protected final JSONObject convertDataToJSONObject(final String data) {
		String temp = data.substring(11, data.length());
		String temp2 = temp.substring(0, temp.length() - 2);
		return JSONObject.fromObject(temp2);
	}

	/**
	 * Connect to url and get the response in JSON
	 * 
	 * @return a JSONObject that contains the data
	 * @throws YahooException
	 *             the yahoo exception
	 */
	public final JSONObject getJSONObject() throws YahooException {
		String data = connectUrl(urlBuilder());
		return convertDataToJSONObject(data);
	}
}<commit_msg>Add some finals in Yahoo<commit_after>/**
 * Copyright 2013 <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.
 */

package fr.cph.stock.external;

import fr.cph.stock.exception.YahooException;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;

/**
 * This class take care of the connexion to Yahoo API. It uses YQL language.
 * 
 * @author <NAME>
 * 
 */
public class Yahoo {
	/** Logger **/
	private static final Logger LOG = Logger.getLogger(Yahoo.class);
	/** Url base **/
	private static final String URL_BASE = "http://query.yahooapis.com/v1/public/yql?q=";
	/** Url end **/
	private static final String URL_END = "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc";
	/** The request **/
	private String request;

	/**
	 * Constructor that build a yahoo object with the given request
	 * 
	 * @param req
	 *            the request
	 */
	public Yahoo(final String req) {
		this.request = req;
	}

	/**
	 * This function build the URL.
	 * 
	 * @return a url
	 * @throws YahooException
	 *             the yahoo exception
	 */
	protected final String urlBuilder() throws YahooException {
		try {
			return URL_BASE + URLEncoder.encode(request, "UTF-8") + URL_END;
		} catch (UnsupportedEncodingException e) {
			throw new YahooException("Error: " + YahooException.ENCODE_ERROR, e);
		}
	}

	/**
	 * THis function connect to the given address and return in a string the content of the page
	 * 
	 * @param address
	 *            the address
	 * @return the content of the page
	 * @throws YahooException
	 *             the yahoo exception
	 */
	protected final String connectUrl(final String address) throws YahooException {
		LOG.debug("URL " + address);
		String toReturn;
		try {
			final URL url = new URL(address);
			final URLConnection uc = url.openConnection();
			final InputStreamReader in = new InputStreamReader(uc.getInputStream(), Charset.forName("UTF8"));
			int c = in.read();
			final StringBuilder build = new StringBuilder();
			while (c != -1) {
				build.append((char) c);
				c = in.read();
			}
			toReturn = build.toString();
			in.close();
		} catch (final IOException e) {
			throw new YahooException("Error: " + YahooException.CONNECT_ERROR, e);
		}
		return toReturn;
	}

	/**
	 * Convert the string to a json object, removing the unwanted part
	 * 
	 * @param data
	 *            the data to convert
	 * @return a JSONObject
	 */
	protected final JSONObject convertDataToJSONObject(final String data) {
		final String temp = data.substring(11, data.length());
		final String temp2 = temp.substring(0, temp.length() - 2);
		return JSONObject.fromObject(temp2);
	}

	/**
	 * Connect to url and get the response in JSON
	 * 
	 * @return a JSONObject that contains the data
	 * @throws YahooException
	 *             the yahoo exception
	 */
	public final JSONObject getJSONObject() throws YahooException {
		final String data = connectUrl(urlBuilder());
		return convertDataToJSONObject(data);
	}
}
{'fraction_non_alphanumeric': 0.10511440107671602, 'fraction_numerical': 0.006191117092866756, 'mean_word_length': 3.4179548156956003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1553551', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2126, 'n_words': 730}