diff --git a/.gitignore b/.gitignore index d556c58..e679f69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea/* vendor/* composer.lock + +config.yml diff --git a/config.yml b/config.yml deleted file mode 100644 index 970b260..0000000 --- a/config.yml +++ /dev/null @@ -1,3 +0,0 @@ -listing_id: 41501 -timezone: -5 -dst: true \ No newline at end of file diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..9f16343 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,120 @@ + "1", + "lu" => "", + "tz" => "", + "startTime" => "", + "endTime" => "", + "channelNumber" => "", + "subChannelNumber" => "", + "callsign" => "", + "listingId" => "", + "listDateTime" => "", + "duration" => "", + "showId" => "", + "seriesId" => "", + "showName" => "", + "episodeTitle" => "", + "episodeNumber" => "", + "parts" => "", + "partNumber" => "", + "seriesPremiere" => "", + "seasonPremiere" => "", + "seriesFinale" => "", + "seasonFinale" => "", + "repeat" => "", + "newShow" => "", + "rating" => "", + "captioned" => "", + "educational" => "", + "blackwhite" => "", + "subtitled" => "", + "live" => "", + "hd" => "", + "descriptiveVideo" => "", + "inProgress" => "", + "showTypeId" => "", + "breakoutLevel" => "", + "showType" => "", + "year" => "", + "guest" => "", + "cast" => "", + "director" => "", + "starRating" => "", + "description" => "", + "league" => "", + "team1" => "", + "team2" => "", + "sportEvent" => "", + "location" => "", + "showPicture" => "", + "showTitle" => "" + ]; + + public function __construct() { + try { + $this->config = Yaml::parse(file_get_contents(__DIR__ . '/../config.yml')); + + } catch (ParseException $e) { + throw new \Exception(sprintf('Unable to parse the YAML config file: %s', $e->getMessage())); + } + + + $this->saveSettings($this->config); + + } + + public function saveSetting($setting, $value) { + foreach ($this->config as $k => $v) { + if ($k == $setting) { + $this->config[$setting] = $value; + } + } + file_put_contents(__DIR__ . '/../config.yml', Yaml::dump($this->config)); + } + + public function saveSettings($config) { + foreach ((array) $config as $k => $v) { + $this->config[$k] = $v; + } + file_put_contents(__DIR__ . '/../config.yml', Yaml::dump($this->config)); + } + + public function getSetting($setting) { + return $this->config[$setting]; + } + + public function getSettings() { + return $this->config; + } +} \ No newline at end of file diff --git a/src/Tvscraper.php b/src/Tvscraper.php index dec5844..114ee34 100644 --- a/src/Tvscraper.php +++ b/src/Tvscraper.php @@ -1,48 +1,74 @@ time_block = $config['time_block']; - $this->debug = $config['debug']; - $this->display_single = $config['display_single']; - } + public $debugLineupNames = false; + public $debugPostalCode = true; + public $debugUserParams = true; + public $debugEndPoint = true; + public $debugDataPattern = true; + + public $baseUrl = 'http://www.tvpassport.com/'; + public $scheduleUrl = 'tvlistings/tvlistings/listings'; + public $providerSearchUrl = 'index.php/lineups'; + + protected $config; + + protected $showData = array(); + + /** + * Constructorizationify + */ + public function __construct() { + $this->config = new Config(); $this->outputBanner(); - $this->readConfig(); } + /** + * Claim this console output in the name of tvscraper + */ public function outputBanner() { echo <<getMessage())); + /** + * Initialize config options + * + * @return void + * + * @todo create better FSM + */ + public function run() { + $input = readline('Do you want to enter your timezone? '); + if ($this->validateInput($input, FILTER_VALIDATE_BOOLEAN) || $this->config->getSetting("firstRun")) { + $this->setTimeZone(); + } + + $input = readline('Do you want to setup your display options? '); + if ($this->validateInput($input, FILTER_VALIDATE_BOOLEAN) || $this->config->getSetting("firstRun")) { + $this->setDisplayOptions(); } - $this->initProvider($config['listing_id'], $config['timezone'], $config['dst']); + + $config['firstRun'] = false; + $this->config->saveSettings($config); + $this->getLineups(); + } - public function getLocation() { - $code = readline('Enter your postal/zip code: '); - if ($code && preg_match('/^[0-9A-Za-z]{5,6}$/', $code)) { - if ($this->debug) { - echo sprintf(PHP_EOL . 'POST %s to %s' . PHP_EOL . PHP_EOL, $code, $this->provider_search_url); + /** + * Validate user input + * + * @param mixed $input User input data to be validated + * @param filter $filter Type of filter used to validate input + * + * @return mixed NULL if validation fails + * + * @todo sanitize input + * @todo handle different data types + * @todo pass all input through here + */ + public function validateInput($input, $filter) { + + if ($filter === FILTER_VALIDATE_BOOLEAN) { + if (strtolower($input) == 'y') { + $input = 'true'; + } elseif (strtolower($input) == 'n') { + $input = 'false'; } - $url = sprintf('%s%s', $this->base_url, $this->provider_search_url); - $html = $this->fetchHTML($url, 'POST', array('city_search_string' => $code)); + } - if ($html) { - // get service_area_id options - preg_match_all('/*(.*?)<\/b>/ms', - $html, $service_area_ids); + if ($input == '\n') { + $input = 'false'; + } - if (sizeof($service_area_ids[0]) < 1) { - echo sprintf( - 'Cannot find any service area providers for postal/zip code: %s' . PHP_EOL . PHP_EOL, - $code - ); - $this->getLocation(); - return; - } else { - //var_dump($service_area_ids); - echo 'Enter the number of your city from the list below.' . PHP_EOL . PHP_EOL; - for ($i=0;$iservice_areas[] = array($code, $name); - echo sprintf("%d\t%s (%s)" . PHP_EOL, $i, $name, $code); + return filter_var($input, $filter, FILTER_NULL_ON_FAILURE); + + } + + /** + * Reads user input and searches a precompiled list to select the appropriate IANA timezone + * + * @return void + * + * @todo create greedy search function for timezone cuz I'm lazy + */ + public function setTimezone() { + $tz = readline('Enter your timezone (ie, America/Toronto): '); + + $timeZoneList = \DateTimeZone::listIdentifiers(); + + foreach ($timeZoneList as $timeZone) { + if ($timeZone === $tz) { + $config['tz'] = $tz; + $this->config->saveSettings($config); + break; + } + } + } + + /** + * Interactively allows the user to choose which data points to display and saves each option to an array + * + * @return void + */ + public function setDisplayOptions() { + $count = 0; + foreach ($this->config->getSettings() as $k => $v) { + if ($count > 4) { + $valid = false; + while ($valid == false) { + + $input = readline('Do you want to see the ' . $k . '? [Y/n] - Defaults to n: '); + $boolean = $this->validateInput($input, FILTER_VALIDATE_BOOLEAN); + if (is_null($boolean)) { + echo 'Invalid input! Smarten up.' . PHP_EOL . PHP_EOL; + } else { + $this->config->saveSetting($k, $boolean); + $valid = true; } - $this->postal_code = $code; - $this->getProvider(); - return; } - } else { - throw new \Exception('Error searching for provider. Try again.'); } - } else { - $this->getLocation(); + ++$count; } } - public function getProvider() { - $city_selection = readline(PHP_EOL . 'City selection number: '); - - if (isset($this->service_areas[$city_selection])) { - if ($this->debug) { - echo sprintf(PHP_EOL . 'POST %s, %s to %s' . PHP_EOL . PHP_EOL, $this->postal_code, $this->service_areas[$city_selection][0], $this->provider_search_url); + /** + * Gets a list of broadcast providers based on the users postal code. + * + * @throws \Exception + */ + public function getLineups() { + $postalCode = readline('Enter your postal/zip code: '); + if ($postalCode && preg_match('/^[0-9A-Za-z]{5,6}$/', $postalCode)) { + if ($this->debugPostalCode) { + echo sprintf(PHP_EOL . 'POST %s to %s%s' . PHP_EOL . PHP_EOL, $postalCode, $this->baseUrl, $this->providerSearchUrl); } - $url = sprintf('%s%s', $this->base_url, $this->provider_search_url); - $html = $this->fetchHTML( - $url, - 'POST', - array( - 'city_search_string' => $this->postal_code, - 'service_area_id' => $this->service_areas[$city_selection][0] - ) - ); + $url = sprintf('%s%s', $this->baseUrl, $this->providerSearchUrl); + $html = $this->fetchHTML($url, 'POST', array('postalCode' => $postalCode)); if ($html) { - // get all lineup selection options - preg_match_all('/