<commit_before><?php
/**
 * Shitty hacky class to grab data from the KimonoAPI
 *
 * Thanks for reading!
 *
 * @author kris / @switchsystems on twitter, Switch Systems Ltd
 *
 */
namespace Sochimon\Service;

use Zend\ServiceManager\ServiceLocatorAwareInterface;

class DataGrabber implements ServiceLocatorAwareInterface
{
	use \Zend\ServiceManager\ServiceLocatorAwareTrait;

	const API_KEY 		= 'd6edb21660b5659ef6c9b73e713fa801';
	const URL_SOCHI 	= 'http://sochi.kimonolabs.com/api/';
	const API_LIMIT 	= 9999;

	public function getAthletes() {
		return json_dencode(file_get_contents('data/cache/athletes.json'));
	}

	public function getCountries() {
		return json_dencode(file_get_contents('data/cache/countries.json'));
	}


	/**
	 * Updates the countries information from all APIs
	 *
	 * @throws RuntimeException
	 * @return boolean
	 */
	public function updateCountriesCache() {

		$countries = $this->doSochiRequest('countries');

		if ($countries===false)
			throw new RuntimeException('Something bad happened with the sochi/coutnries API feed updater');

		$result = file_put_contents('data/cache/countries.json',$countries);
		if ($result===false)
			throw new RuntimeException('Unable to write to data/cache/countries.json');

		return true;

	}

	/**
	 * Updates the athletes information
	 * DRY, mega-lolz :D
	 *
	 * @throws RuntimeException
	 * @return boolean
	 */
	public function updateAthletesCache() {

		$athletes = $this->doSochiRequest('athletes');

		if ($athletes===false)
			throw new RuntimeException('Something bad happened with the sochi/athletes API feed updater');

		$result = file_put_contents('data/cache/athletes.json',$athletes);
		if ($result===false)
			throw new RuntimeException('Unable to write to data/cache/athletes.json');

		return true;

	}


	/**
	 * Fetches data from the Sochi datafeed
	 *
	 * requires allow_url_fopen!
	 *
	 * @param unknown $urlPath
	 * @param array $args
	 * @return string
	 */
	protected function doSochiRequest($urlPath, array $args=array()) {

		$args['apikey'] = self::API_KEY;
		$args['limit'] = self::API_LIMIT;

		$url = self::URL_SOCHI.$urlPath;
		$x=0;
		foreach ($args as $k=>$v) {
			$url.= ($x==0 ? '?' : '&') .$k.'='.$v;
			$x++;
		}

		return file_get_contents($url);

	}


}<commit_msg>updated the data grabber to combine APIs for country information<commit_after><?php
/**
 * Shitty hacky class to grab data from the KimonoAPI
 *
 * Thanks for reading!
 *
 * @author kris / @switchsystems on twitter, Switch Systems Ltd
 *
 */
namespace Sochimon\Service;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\Json\Json;

class DataGrabber implements ServiceLocatorAwareInterface
{
	use \Zend\ServiceManager\ServiceLocatorAwareTrait;

	const API_KEY 		= 'd6edb21660b5659ef6c9b73e713fa801';
	const URL_SOCHI 	= 'http://sochi.kimonolabs.com/api/';
	const API_LIMIT 	= 9999;


	/**
	 * Get all athletes
	 *
	 * @return mixed
	 */
	public function getAthletes() {

		return json_decode(file_get_contents('data/cache/athletes.json'));
	}

	/**
	 * Get all countries
	 *
	 * @return mixed
	 */
	public function getCountries() {
		$countries=json_decode(file_get_contents('data/cache/countries.json'),true);

		$result = array();

		foreach ($countries['data'] as $country) {
			$objCountry = new Sochimoon\Model\Country();
			foreach ($country as $k=>$v) {
				$objCountry->$k = $v;
			}
			$result[]=$objCountry;
		}

		return $result;

	}


	/**
	 * Updates the countries information from all APIs
	 *
	 * @throws RuntimeException
	 * @return boolean
	 */
	public function updateCountriesCache() {

		$countries = $this->doSochiRequest('countries');

		if ($countries===false)
			throw new RuntimeException('Something bad happened with the sochi/coutnries API feed updater');

		$countries = json_decode($countries,true);

		$countries_geo = file_get_contents('http://restcountries.eu/rest/v1');

		try {
		// use the zend decoder, as we have some funky utf-8/encoding going on
		$countries_geo = Json::decode($countries_geo,Json::TYPE_ARRAY);
		} catch (\Exception $e) {
			var_dump($e->getMessage());
		}

		// populate sochi country data with lat/lng coords
		foreach ($countries_geo as $geoCountry) {
			foreach ($countries['data'] as &$country) {
				if ($geoCountry['alpha3Code'] == $country['abbr']) {
					echo 'Found LATLNG for '.$country['abbr']." ";
					$country['latlng'] = $geoCountry['latlng'];
					break;
				}
			}
		}

		$result = file_put_contents('data/cache/countries.json',json_encode($countries));
		if ($result===false)
			throw new RuntimeException('Unable to write to data/cache/countries.json');

		return true;

	}

	/**
	 * Updates the athletes information
	 * DRY, mega-lolz :D
	 *
	 * @throws RuntimeException
	 * @return boolean
	 */
	public function updateAthletesCache() {

		$athletes = $this->doSochiRequest('athletes');

		if ($athletes===false)
			throw new RuntimeException('Something bad happened with the sochi/athletes API feed updater');

		$result = file_put_contents('data/cache/athletes.json',$athletes);
		if ($result===false)
			throw new RuntimeException('Unable to write to data/cache/athletes.json');

		return true;

	}


	/**
	 * Fetches data from the Sochi datafeed
	 *
	 * requires allow_url_fopen!
	 *
	 * @param unknown $urlPath
	 * @param array $args
	 * @return string
	 */
	protected function doSochiRequest($urlPath, array $args=array()) {

		$args['apikey'] = self::API_KEY;
		$args['limit'] = self::API_LIMIT;

		$url = self::URL_SOCHI.$urlPath;
		$x=0;
		foreach ($args as $k=>$v) {
			$url.= ($x==0 ? '?' : '&') .$k.'='.$v;
			$x++;
		}

		return file_get_contents($url);

	}


}
{'fraction_non_alphanumeric': 0.141716920893934, 'fraction_numerical': 0.009755232351897836, 'mean_word_length': 4.098553345388789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7386507', 'n_tokens_mistral': 2130, 'n_tokens_neox': 1848, 'n_words': 431}