From 3b791532f2551281f450dd736151ad8ae1fde67e Mon Sep 17 00:00:00 2001 From: Mike Penner Date: Fri, 22 Jul 2016 18:16:51 -0400 Subject: [PATCH 1/2] Updated for new site compatability Pulls all data and saves config settings Review and Improved --- config.yml | 52 +++- src/Config.php | 120 ++++++++ src/Tvscraper.php | 698 +++++++++++++++++++++++++++++++--------------- timezones.yml | 507 +++++++++++++++++++++++++++++++++ tvscraper | 2 +- 5 files changed, 1154 insertions(+), 225 deletions(-) create mode 100644 src/Config.php create mode 100644 timezones.yml diff --git a/config.yml b/config.yml index 970b260..0e201f7 100644 --- a/config.yml +++ b/config.yml @@ -1,3 +1,49 @@ -listing_id: 41501 -timezone: -5 -dst: true \ No newline at end of file +firstRun: false +lu: '36648' +tz: America/Toronto +startTime: 0 +endTime: 0 +channelNumber: true +subChannelNumber: true +callsign: false +listingId: false +listDateTime: false +duration: false +showId: false +seriesId: false +showName: false +episodeTitle: false +episodeNumber: false +parts: false +partNumber: false +seriesPremiere: false +seasonPremiere: false +seriesFinale: false +seasonFinale: false +repeat: false +newShow: false +rating: false +captioned: false +educational: false +blackwhite: false +subtitled: false +live: false +hd: false +descriptiveVideo: false +inProgress: false +showTypeId: false +breakoutLevel: false +showType: false +year: false +guest: false +cast: false +director: false +starRating: false +description: false +league: false +team1: false +team2: false +sportEvent: false +location: false +showPicture: false +showTitle: true 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..eca5d42 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 = explode(PHP_EOL, file_get_contents(__DIR__ . '/../timezones.yml')); + + 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('/