Skip to content

Commit

Permalink
Review and Improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenrisulfir committed Aug 16, 2016
1 parent 7499000 commit 9c3a216
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 61 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
firstRun: false
lu: '36648'
tz: America/Toronto
startTime: '0'
endTime: '0'
startTime: 0
endTime: 0
channelNumber: true
subChannelNumber: true
callsign: false
Expand Down
43 changes: 27 additions & 16 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: e649r9
* Date: 25/07/16
* Time: 12:40 PM
*/

namespace Tvscraper;

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

/**
* tvscraper - tvpassport tv schedule scraper
*
* @author module17
* @author Fenrisulfir
*
* @package tvscraper
*
* @version 2.01
*
*/

class Config {
/**
* MEMBER FIELDS
* @var array|mixed $config Description
*
* METHODS
* @method
* @method mixed getSetting(string $setting)
* @method array getSettings()
* @method void saveSetting(string $setting, mixed $value)
* @method void saveSettings(mixed[] $config)
*/

private $config = [
"firstRun" => "1",
Expand Down Expand Up @@ -75,12 +90,11 @@ public function __construct() {
}


$this->saveSettings($config);
$this->saveSettings($this->config);

}

public
function saveSetting($setting, $value) {
public function saveSetting($setting, $value) {
foreach ($this->config as $k => $v) {
if ($k == $setting) {
$this->config[$setting] = $value;
Expand All @@ -89,21 +103,18 @@ function saveSetting($setting, $value) {
file_put_contents(__DIR__ . '/../config.yml', Yaml::dump($this->config));
}

public
function saveSettings(&$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) {
public function getSetting($setting) {
return $this->config[$setting];
}

public
function getSettings() {
public function getSettings() {
return $this->config;
}
}
176 changes: 133 additions & 43 deletions src/Tvscraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,72 @@

require_once('Config.php');

use Symfony\Component\Yaml\Yaml;

/* tvscraper - tvpassport tv schedule scraper
/**
* tvscraper - tvpassport tv schedule scraper
*
* @author module17
* @author Fenrisulfir
*
* Hard-coded to use luid 41501 - Rogers cable Toronto - Digital adapter
* @package tvscraper
*
* ie. http://tvpassport.com/tvgrid.shtml?luid=41501&st=1465584150&sch=15&size=1&tzo=-5&dsto=1#x
* @version 2.01
*
*/

class Tvscraper {
/**
* DEBUGGING FLAGS
* @var boolean $debugLineupNames Display Broadcast Provider Array
* @var boolean $debugPostalCode Display Postal Code and URL
* @var boolean $debugUsingParams Display Params Used To Generate ShowData
* @var boolean $debugEndpoint Display ShowData URL
* @var boolean $debugDataPattern Display ShowData Array
*
* MEMBER FIELDS
* @var string $base_url TVPassport URL
* @var string $schedule_url ShowData URL
* @var string $provider_url BroadCast Provider URL
* @var Config $config Configuration Array
* @var string[] $showData ShowData Array
*
* METHODS
* @method __construct()
* @method void outputBanner()
* @method void run()
* @method void setTimeZone()
* @method void setDisplayOptions()
* @method mixed validateInput(string $input, FILTER_TYPE $filter)
* @method void getLineups()
* @method void getSchedule(string $code, string $timezone)
* @method void buildPattern(string $pattern, string &$html)
* @method string fetchHTML(string $url, string $method, string[] $post_fields)
*
*/
public $debugLineupNames = false;
public $debugPostalCode = true;
public $debugUserParams = true;
public $debugEndPoint = true;
public $debugDataPattern = false;
public $base_url = 'http://www.tvpassport.com/';
public $schedule_url = 'tvlistings/tvlistings/listings';
public $provider_search_url = 'index.php/lineups';
public $postal_code = '';
public $timeZone = '';
public $debugDataPattern = true;

public $baseUrl = 'http://www.tvpassport.com/';
public $scheduleUrl = 'tvlistings/tvlistings/listings';
public $providerSearchUrl = 'index.php/lineups';

protected $config;

protected $showData = array();

/*
* @param $config array of configuration settings
/**
* Constructorizationify
*/
public function __construct() {
$this->config = new Config();
$this->outputBanner();
}

/**
* Claim this console output in the name of tvscraper
*/
public function outputBanner() {
echo <<<DATA
********************************************************************************
Expand All @@ -46,6 +78,13 @@ public function outputBanner() {
DATA;
}

/**
* 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")) {
Expand All @@ -63,20 +102,18 @@ public function run() {

}

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;
}
}
}

/**
* 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) {
Expand All @@ -95,6 +132,32 @@ public function validateInput($input, $filter) {

}

/**
* 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) {
Expand All @@ -114,17 +177,20 @@ public function setDisplayOptions() {
}
++$count;
}

$this->config->saveSettings($config);
}

/**
* 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->base_url, $this->provider_search_url);
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);
$url = sprintf('%s%s', $this->baseUrl, $this->providerSearchUrl);
$html = $this->fetchHTML($url, 'POST', array('postalCode' => $postalCode));

if ($html) {
Expand All @@ -134,6 +200,7 @@ public function getLineups() {
if ($this->debugLineupNames) {
var_dump($lineupNames);
}

if (sizeof($lineupNames[0]) < 1) {
echo sprintf(
'Cannot find any service area providers for postal/zip code: %s' . PHP_EOL . PHP_EOL,
Expand Down Expand Up @@ -169,11 +236,25 @@ public function getLineups() {
}
}

/**
* Uses the broadcast code and timezone to get all of the shows currently playing.
*
* @param int $code The code used to represent the Broadcast Provider Lineup
* @param string $timezone The timezone
*
* @return void
*
* @todo build the regex pattern more gracefully
* @todo figure out how to nicely display potentially all the data
* @todo sanitize the data before displaying it
* @todo display data headings
* @todo handle missing data causing different sized arrays
*/
public function getSchedule($code, $timezone) {
$params = array(
'lu' => $code,
'st' => time(), // start timestamp, if null automatically starts at most recent hour mark
'et' => time(),
'et' => time(), //end timestamp, set to same as st to find only currently playing shows
'tz' => $timezone
);

Expand All @@ -182,7 +263,7 @@ public function getSchedule($code, $timezone) {
var_dump($params);
}

$url = $this->base_url . $this->schedule_url;
$url = $this->baseUrl . $this->scheduleUrl;
if ($this->debugEndPoint) {
echo sprintf(PHP_EOL . 'ENDPOINT: %s' . PHP_EOL . PHP_EOL, $url);
}
Expand Down Expand Up @@ -428,7 +509,7 @@ public function getSchedule($code, $timezone) {

//Number of shows
for ($i = 0; $i < sizeof($this->showData[0][0]); ++$i) {
//Number of columns
//Number of data points
for ($j = 0; $j < $count; ++$j) {
echo sprintf("%s\t", $this->showData[$j][1][$i]);
}
Expand All @@ -437,22 +518,31 @@ public function getSchedule($code, $timezone) {

}

/**
* Scrapes the data from the HTML and pushes it into the showData array
*
* @param string $pattern Regex pattern for each data point
* @param string $html The scraped html
*
* @return void
*
* @todo stop adding raw data to showData array
*/
public
function buildPattern($pattern, &$html) {
function buildPattern($pattern, $html) {
preg_match_all('/' . $pattern . '/ms', $html, $arr);

array_push($this->showData, $arr);
}

public
function timeRemaining($showET) {
return round(($showET - time()) / 60);
}

/*
* @param $url string URL to fetch
/**
* @param string $url URL to fetch
* @param string $method 'POST' or 'GET'
* @param mixed[] $post_fields
*
* @return string $html Response from URL endpoint
*
* @return $html string Response from URL endpoint
* @todo handle invalid method
*/
public
function fetchHTML($url, $method = 'GET', $post_fields = array()) {
Expand Down

0 comments on commit 9c3a216

Please sign in to comment.