<commit_before><?php

namespace JPinkney\TVMaze;

class TVMaze {

	CONST APIURL = 'http://api.tvmaze.com';

	/**
	 * Takes in a show name
	 * Outputs array of all the related shows for that given name
	 *
	 * @param $show_name
	 *
	 * @return array
	 */
	function search($show_name){
		$url = self::APIURL."/search/shows?q=".$show_name;

		$shows = $this->getFile($url);

		$relevant_shows = array();
		foreach($shows as $series){
			$TVShow = new TVShow($series['show']);
			array_push($relevant_shows, $TVShow);
		}
		return $relevant_shows;
	}

	/**
	 * Takes in a show name with optional modifiers (episodes)
	 * Outputs array of the MOST related shows for that given name
	 *
	 * @param $show_name
	 *
	 * @return array
	 */
	function singleSearch($show_name){

		$url = self::APIURL."/singlesearch/shows?q=".$show_name.'&embed=episodes';
		$shows = $this->getFile($url);

		$episode_list = array();
		foreach($shows['_embedded']['episodes'] as $episode){
			$ep = new Episode($episode);
			array_push($episode_list, $ep);
		}

		$TVShow = new TVShow($shows);

		return array($TVShow, $episode_list);
	}

	/**
	 * Allows show lookup by using TVRage or TheTVDB ID
	 * site is the string of the website (either 'tvrage' or 'thetvdb') and the id is the id of the show on that respective site
	 *
	 * @param $site
	 * @param $ID
	 *
	 * @return TVShow
	 */
	function getShowBySiteID($site, $ID){
		$site = strtolower($site);
		$url = self::APIURL.'/lookup/shows?'.$site.'='.$ID;
		$show = $this->getFile($url);

		return new TVShow($show);
	}

	/**
	 * Takes in an actors name and outputs their actor object
	 *
	 * @param $name
	 *
	 * @return array
	 */
	function getPersonByName($name){
		$name = strtolower($name);
		$url = self::APIURL.'/search/people?q='.$name;
		$person = $this->getFile($url);

		$people = array();
		foreach($person as $peeps){
		array_push($people, new Actor($peeps['person']));
		}

		return $people;
	}

	/**
	 * TODO: this still needs to be done
	 *
	 * @param null $country
	 * @param null $date
	 *
	 * @return array
	 */
	function getSchedule($country=null, $date=null){
		if($country != null && $date != null){
			$url = self::APIURL . '/schedule?country=' . $country .'&date='. $date;
		}else if($country == null && $date != null){
			$url = self::APIURL . '/schedule?date=' . $date;
		}else if($country != null && $date == null){
			$url = self::APIURL . '/schedule?country=' . $country;
		}else{
			$url = self::APIURL . '/schedule';
		}

		$schedule = $this->getFile($url);

		$show_list = array();
		foreach($schedule as $episode){
			$ep = new Episode($episode);
			$show = new TVShow($episode['show']);
			array_push($show_list, $show, $ep);
		}

		return $show_list;
	}

	/**
	 * Takes in a show ID and outputs the TVShow Object
	 *
	 * @param      $ID
	 * @param null $embed_cast
	 *
	 * @return array
	 */
	function getShowByShowID($ID, $embed_cast=null){
		if($embed_cast === true){
			$url = self::APIURL.'/shows/'.$ID.'?embed=cast';
		}else{
			$url = self::APIURL.'/shows/'.$ID;
		}

		$show = $this->getFile($url);

		$cast = array();
		foreach($show['_embedded']['cast'] as $person){
			$actor = new Actor($person['person']);
			$character = new Character($person['character']);
			array_push($cast, array($actor, $character));
		}

		$TVShow = new TVShow($show);

		return $embed_cast === true ? array($TVShow, $cast) : array($TVShow);
	}

	/**
	 * Takes in a show ID and outputs the AKA Object
	 *
	 * @param      $ID
	 *
	 * @return array
	 */
	function getShowAKAs($ID)
	{
		$url = self::APIURL . '/shows/' . $ID . '/akas';

		$akas = $this->getFile($url);

		$AKA = new AKA($akas);

		if (!empty($akas['name'])) {

			return $AKA;
		}

		return false;
	}

	/**
	 * Takes in a show ID and outputs all the episode objects for that show in an array
	 *
	 * @param $ID
	 *
	 * @return array
	 */
	function getEpisodesByShowID($ID){

		$url = self::APIURL.'/shows/'.$ID.'/episodes';

		$episodes = $this->getFile($url);

		$allEpisodes = array();
		foreach($episodes as $episode){
			$ep = new Episode($episode);
			array_push($allEpisodes, $ep);
		}

		return $allEpisodes;
	}

	/**
	 * Takes in a show ID and outputs all of the cast members in the form (actor, character)
	 *
	 * @param $ID
	 *
	 * @return array
	 */
	function getCastByShowID($ID){
		$url = self::APIURL.'/shows/'.$ID.'/cast';
		$people = $this->getFile($url);<commit_msg>Add getEpisodeByAirdate and getEpisodeByNumber functions.<commit_after><?php

namespace JPinkney\TVMaze;

class TVMaze {

	CONST APIURL = 'http://api.tvmaze.com';

	/**
	 * Takes in a show name
	 * Outputs array of all the related shows for that given name
	 *
	 * @param $show_name
	 *
	 * @return array
	 */
	function search($show_name){
		$url = self::APIURL."/search/shows?q=".$show_name;

		$shows = $this->getFile($url);

		$relevant_shows = array();
		foreach($shows as $series){
			$TVShow = new TVShow($series['show']);
			array_push($relevant_shows, $TVShow);
		}
		return $relevant_shows;
	}

	/**
	 * Takes in a show name with optional modifiers (episodes)
	 * Outputs array of the MOST related shows for that given name
	 *
	 * @param $show_name
	 *
	 * @return array
	 */
	function singleSearch($show_name){

		$url = self::APIURL."/singlesearch/shows?q=".$show_name.'&embed=episodes';
		$shows = $this->getFile($url);

		$episode_list = array();
		foreach($shows['_embedded']['episodes'] as $episode){
			$ep = new Episode($episode);
			array_push($episode_list, $ep);
		}

		$TVShow = new TVShow($shows);

		return array($TVShow, $episode_list);
	}

	/**
	 * Allows show lookup by using TVRage or TheTVDB ID
	 * site is the string of the website (either 'tvrage' or 'thetvdb') and the id is the id of the show on that respective site
	 *
	 * @param $site
	 * @param $ID
	 *
	 * @return TVShow
	 */
	function getShowBySiteID($site, $ID){
		$site = strtolower($site);
		$url = self::APIURL.'/lookup/shows?'.$site.'='.$ID;
		$show = $this->getFile($url);

		return new TVShow($show);
	}

	/**
	 * Takes in an actors name and outputs their actor object
	 *
	 * @param $name
	 *
	 * @return array
	 */
	function getPersonByName($name){
		$name = strtolower($name);
		$url = self::APIURL.'/search/people?q='.$name;
		$person = $this->getFile($url);

		$people = array();
		foreach($person as $peeps){
		array_push($people, new Actor($peeps['person']));
		}

		return $people;
	}

	/**
	 * TODO: this still needs to be done
	 *
	 * @param null $country
	 * @param null $date
	 *
	 * @return array
	 */
	function getSchedule($country=null, $date=null){
		if($country != null && $date != null){
			$url = self::APIURL . '/schedule?country=' . $country .'&date='. $date;
		}else if($country == null && $date != null){
			$url = self::APIURL . '/schedule?date=' . $date;
		}else if($country != null && $date == null){
			$url = self::APIURL . '/schedule?country=' . $country;
		}else{
			$url = self::APIURL . '/schedule';
		}

		$schedule = $this->getFile($url);

		$show_list = array();
		foreach($schedule as $episode){
			$ep = new Episode($episode);
			$show = new TVShow($episode['show']);
			array_push($show_list, $show, $ep);
		}

		return $show_list;
	}

	/**
	 * Takes in a show ID and outputs the TVShow Object
	 *
	 * @param      $ID
	 * @param null $embed_cast
	 *
	 * @return array
	 */
	function getShowByShowID($ID, $embed_cast=null){
		if($embed_cast === true){
			$url = self::APIURL.'/shows/'.$ID.'?embed=cast';
		}else{
			$url = self::APIURL.'/shows/'.$ID;
		}

		$show = $this->getFile($url);

		$cast = array();
		foreach($show['_embedded']['cast'] as $person){
			$actor = new Actor($person['person']);
			$character = new Character($person['character']);
			array_push($cast, array($actor, $character));
		}

		$TVShow = new TVShow($show);

		return $embed_cast === true ? array($TVShow, $cast) : array($TVShow);
	}

	/**
	 * Takes in a show ID and outputs the AKA Object
	 *
	 * @param      $ID
	 *
	 * @return array
	 */
	function getShowAKAs($ID)
	{
		$url = self::APIURL . '/shows/' . $ID . '/akas';

		$akas = $this->getFile($url);

		$AKA = new AKA($akas);

		if (!empty($akas['name'])) {

			return $AKA;
		}

		return false;
	}

	/**
	 * Takes in a show ID and outputs all the episode objects for that show in an array
	 *
	 * @param $ID
	 *
	 * @return array
	 */
	function getEpisodesByShowID($ID){

		$url = self::APIURL.'/shows/'.$ID.'/episodes';

		$episodes = $this->getFile($url);

		$allEpisodes = array();
		foreach($episodes as $episode){
			$ep = new Episode($episode);
			array_push($allEpisodes, $ep);
		}

		return $allEpisodes;
	}
	
	/**
	 * Returns a single episodes information by its show ID, season and episode numbers
	 *
	 * @param $ID
	 * @param $season
	 * @param $episode
	 *
	 * @return Episode|mixed
	 */
	function getEpisodeByNumber($ID, $season, $episode)
	{
		$ep = false;
		$url = self::APIURL . '/shows/' . $ID . '/episodebynumber?season='. $season . '&number=' . $episode;
		$response = $this->getFile($url);
		if (is_array($response)) {
			$ep = new Episode($response);
		}
		return $ep;
	}

	/**
	 * Returns episodes for a given show ID and ISO 8601 airdate
	 *
	 * @param $ID
	 * @param $airdate
	 *
	 * @return Episode|mixed
	 */
	function getEpisodesByAirdate($ID, $airdate)
	{
		$url = self::APIURL . '/shows/' . $ID . '/episodesbydate?date=' . date('Y-m-d', strtotime($airdate));
		$episodes = $this->getFile($url);

		$allEpisodes = array();
		if (is_array($episodes)) {
			foreach ($episodes as $episode) {
				$ep = new Episode($episode);
				array_push($allEpisodes, $ep);
			}
		}
		return $allEpisodes;
	}

	/**
	 * Takes in a show ID and outputs all of the cast members in the form (actor, character)
	 *
	 * @param $ID
	 *
	 * @return array
	 */
	function getCastByShowID($ID){
		$url = self::APIURL.'/shows/'.$ID.'/cast';
		$people = $this->getFile($url);
{'fraction_non_alphanumeric': 0.1816980172852059, 'fraction_numerical': 0.0004067107269954245, 'mean_word_length': 3.365734576120728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3050751', 'n_tokens_mistral': 3952, 'n_tokens_neox': 3490, 'n_words': 877}