Created
October 2, 2016 04:21
-
-
Save arthurprogramming/76ec59ed0e1a9ebb3257f7b9493be6e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FutebolCard | |
{ | |
const URL = 'https://www.futebolcard.com/site'; | |
const SOON = 'Soon'; | |
protected $token; | |
public function __construct($token) | |
{ | |
$this->token = $token; | |
} | |
public function searchAndNotify($team, $purchaseAvailableOnly = false) | |
{ | |
$matches = $this->getAvailableMatches($team, $purchaseAvailableOnly); | |
if (!empty($matches)) { | |
$this->notifyMatches($matches); | |
} | |
} | |
protected function getAvailableMatches($team, $purchaseAvailableOnly) | |
{ | |
$dom = new DOMDocument(); | |
@$dom->loadHtml(file_get_contents(self::URL)); | |
$finder = new DomXPath($dom); | |
$tableNodes = $finder->query('//img[@title="' . $team . '"]'); | |
$matches = []; | |
foreach ($tableNodes as $node) { | |
$parentNode = $node->parentNode->parentNode->parentNode->parentNode; | |
$stadium = $finder->query( | |
'.//p[@class="estadio"]', | |
$parentNode | |
)->item(0)->nodeValue; | |
$teamsName = $finder->query('.//img', $node->parentNode->parentNode); | |
$home = $teamsName->item(0)->getAttribute('title'); | |
$opponent = $teamsName->item(1)->getAttribute('title'); | |
$button = $finder->query( | |
'.//div[@id="divbotaocomprarhome"]/a', | |
$parentNode | |
)->item(0)->getAttribute('href'); | |
if (!$purchaseAvailableOnly || !empty($button)) { | |
$matches[] = [ | |
'home' => $home, | |
'opponent' => $opponent, | |
'stadium' => $stadium, | |
'button' => !empty($button) ? $button : self::SOON, | |
]; | |
} | |
} | |
return $matches; | |
} | |
protected function notifyMatches(array $matches) | |
{ | |
if (!empty($matches)) { | |
$ch = curl_init('https://api.pushbullet.com/v2/pushes'); | |
curl_setopt( | |
$ch, | |
CURLOPT_HEADER, | |
['Content-Type: application/json'] | |
); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERPWD, $this->token); | |
foreach ($matches as $match) { | |
curl_setopt( | |
$ch, | |
CURLOPT_POSTFIELDS, | |
[ | |
'type' => $match['button'] == self::SOON ? 'note' : 'link', | |
'title' => $match['home'] . ' X ' . $match['opponent'] . ' (' . $match['stadium'] . ')', | |
'url' => $match['button'], | |
] | |
); | |
$result = curl_exec($ch); | |
print_r($result); | |
} | |
curl_close($ch); | |
print_r($matches); | |
} | |
} | |
} | |
$futebolCard = new FutebolCard('xxxxxxxxxxxxxxxxxxxxxxxxx'); | |
$futebolCard->searchAndNotify('Flamengo', true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment