Last active
September 12, 2018 07:36
-
-
Save Rajeshr34/8da62f0adab4e7b5f5a4d026ae53979e to your computer and use it in GitHub Desktop.
Price Tracker India
This file contains 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 | |
use DiDom\Document; | |
use Stringy\Stringy as S; | |
class tracker | |
{ | |
public function __construct() | |
{ | |
$this->priceTrack(); | |
} | |
private function priceTrack() | |
{ | |
$savePath = "petrol_price_data.json"; | |
$saveData = file_exists($savePath) ? json_decode(file_get_contents($savePath), true) : []; | |
$priviousPrice = "https://www.iocl.com/Product_PreviousPrice/PetrolPreviousPrice.aspx"; | |
$currentPrice = "https://www.iocl.com/Product_PreviousPrice/PetrolPreviousPriceDynamic.aspx"; | |
if (empty($saveData)) { | |
$this->getPriceDataOfUrl($currentPrice, $saveData); | |
$this->getPriceDataOfUrl($priviousPrice, $saveData); | |
file_put_contents($savePath, json_encode($saveData)); | |
} | |
$governments = [ | |
[ | |
'name' => 'Manmohan Singh', | |
'term_from' => ("2004-05"), | |
'term_to' => ("2009-05"), | |
'data' => [] | |
], | |
[ | |
'name' => 'Manmohan Singh', | |
'term_from' => ("2009-05"), | |
'term_to' => ("2014-05"), | |
'data' => [] | |
], | |
[ | |
'name' => 'Narendra Modi', | |
'term_from' => ("2014-05"), | |
'term_to' => ("2019-05"), | |
'data' => [] | |
] | |
]; | |
foreach ($saveData['data'] as $month => $datum) { | |
$monthTime = strtotime($month); | |
foreach ($governments as $governmentKey => $government) { | |
if ($monthTime >= strtotime($government['term_from']) && $monthTime <= strtotime($government['term_to'])) { | |
$governments[$governmentKey]['data'][date('Y', strtotime($month))][date('m', strtotime($month))] = array_first($datum); | |
} | |
} | |
} | |
echo "Prices is based on IndianOil | " . $priviousPrice; | |
echo " | " . $currentPrice . "\n"; | |
echo "Below Prices are for the " . join(",", $saveData['head']) . ' cities' . " \n\n"; | |
echo "Code: https://gist.github.com/Rajeshr34/8da62f0adab4e7b5f5a4d026ae53979e \n\n"; | |
foreach ($governments as $government) { | |
echo "Governament : " . $government['name'] . "\n"; | |
echo "Term From : " . $government['term_from'] . "\n"; | |
echo "Term TO : " . $government['term_to'] . "\n"; | |
ksort($government['data']); | |
$startingTermPrice = 0; | |
foreach ($government['data'] as $year => $datum) { | |
echo "\nYear : " . $year . "\n"; | |
ksort($datum); | |
$startPrice = 0; | |
$endPrice = 0; | |
foreach ($datum as $month => $item) { | |
if ($startPrice == 0) | |
$startPrice = end($item); | |
$endPrice = end($item); | |
echo 'Month: ' . $month . " | " . join(' , ', $item) . "\n"; | |
} | |
$diff = round(($endPrice - $startPrice), 2); | |
echo "Start Price $startPrice | End Price $endPrice | Diff " . $diff . "\n"; | |
$startingTermPrice += $diff; | |
} | |
echo "\n\nAt the end of term Price Difference is " . $startingTermPrice; | |
echo "\n\n\n\n------------\n\n\n\n"; | |
} | |
exit; | |
} | |
private function getPriceDataOfUrl($url, &$output) | |
{ | |
$data = file_get_contents($url); | |
$document = new Document($data); | |
$table = $document->first('.wrapper-table'); | |
$theadTD = $table->find("thead tr td"); | |
foreach ($theadTD as $lp => $item) { | |
if ($lp != 0) { | |
$output['head'][$lp] = $item->text(); | |
} | |
} | |
$bodyTR = $table->find("tbody .generic-table.general-table tr"); | |
foreach ($bodyTR as $itemObject) { | |
$TDItems = $itemObject->find('td'); | |
$date = date('d', strtotime($TDItems[0]->text())); | |
$month = date('Y-m', strtotime($TDItems[0]->text())); | |
foreach ($TDItems as $itemK => $itemV) { | |
if ($itemK != 0) { | |
$output['data'][$month][$date][] = S::create($itemV->text())->stripWhitespace()->__toString(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment