Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse NHL.com and send games scores, once a day, directly to your phone with twilio.
<?php
require "twilio-php/Services/Twilio.php";
class Match
{
public $firstTeam;
public $firstTeamScore;
public $secondTeam;
public $secondTeamScore;
}
function main()
{
$url = constructURL();
$html = getRequest($url);
// Create a new DOM Document to handle scraping
$dom = new DOMDocument();
// In HTML5, there is no DTD to check, which will make DOM use the HTML4 Transitional DTD
// and that doesnt contain those elements, hence the Warnings
libxml_use_internal_errors(true);
$res = $dom -> loadHTML($html);
libxml_use_internal_errors(false);
$xpath = new DomXPath($dom);
$class = 'team left';
$classHeader = "left";
$tableHeaderElements = $xpath -> query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classHeader ')]");
$items = $xpath -> query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");
$number = 0;
$firstTeam = true;
$matches = [];
// Only take the first element
foreach ($tableHeaderElements as $i)
{
$node = $i;
$nodeValue = $node -> nodeValue;
if ($nodeValue == "FINAL" || $nodeValue == "FINAL OT" || $nodeValue == "FINAL SO")
{
$number = 0;
while ($node -> nodeValue != "T")
{
$node = $node -> nextSibling;
$number++;
}
}
else if ($number == 0)
continue;
else
{
$team = $node -> nodeValue;
$numberTemp = $number;
while ($numberTemp > 0)
{
$node = $node -> nextSibling;
$numberTemp--;
}
$score = $node -> nodeValue;
if ($firstTeam)
{
$match = new Match();
$match -> firstTeam = str_replace("é", "e", $team);
$match -> firstTeamScore = $score;
$firstTeam = false;
}
else
{
$match -> secondTeam = str_replace("é", "e", $team);
$match -> secondTeamScore = $score;
$matches[] = $match;
$firstTeam = true;
}
}
}
return $matches;
}
function getRequest($url)
{
// Initialize curl and following options
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
// Grab the html from the page
$html = curl_exec($ch);
if(!$html) {
echo "An error append with the request" . PHP_EOL;
exit();
}
curl_close($ch);
return $html;
}
function constructURL()
{
$date = getDatetimeNow();
return "http://www.nhl.com/ice/scores.htm?date=" . $date . "&season=20152016";
}
function getDatetimeNow() {
$tz_object = new DateTimeZone('America/Montreal');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format("m/j/Y");
}
function printMatches($matches)
{
$matchesString = "";
if ($matches != null)
{
foreach ($matches as &$match)
{
if ($match -> firstTeamScore > $match -> secondTeamScore)
{
$matchesString .= $match -> firstTeam . " - " . $match -> secondTeam . " " .
$match -> firstTeamScore . " - " . $match -> secondTeamScore . "\n";
}
else
{
$matchesString .= $match -> secondTeam . " - " . $match -> firstTeam . " " .
$match -> secondTeamScore . " - " . $match -> firstTeamScore . "\n";
}
}
}
return $matchesString;
}
function twilioSend($matchesString)
{
$account_sid = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$client = new Services_Twilio($account_sid, $auth_token);
$people = array(
"+11111111111" => "Freelix"
);
foreach ($people as $number => $name)
{
$sms = $client->account->messages->sendMessage(
"+99999999999",
$number,
"Hi $name, Here's the results for ". getDatetimeNow() . " :\n\n" . $matchesString
);
// Display a confirmation message on the screen
echo "Just sent a sms to $name";
}
}
twilioSend(printMatches(main()));
?>
#!/bin/sh
# To start script --> nohup ./hockeyScript.sh >> log.txt &
# To stop script --> ps -ef
# then kill -9 pid
while true
do
/usr/bin/php hockeyScript.php &
printf "\n"
date +'%m/%d/%Y %H:%M:%S'
printf "\n"
sleep 86400
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.