Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
afiqiqmal committed May 23, 2018
2 parents f076b6a + e517133 commit 3ee1384
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 199 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ BTW, Currently available (Successfully Scraped)
2. [GDEX](http://www.gdexpress.com/malaysia/home/)
3. [ABX Express](http://www.abxexpress.com.my/)
4. [DHL Express](https://www.logistics.dhl/my-en/home.html)
5. [SkyNet Express](http://www.skynet.com.my/)
6. [CityLink Express](http://www.citylinkexpress.com/MY/Consignment.aspx)
7. [FedEx Express](https://www.fedex.com/my/)
8. [LEL Express](http://www.lex.com.my/)
5. [DHL E-Commerce](https://www.logistics.dhl/my-en/home/our-divisions/ecommerce.html)
6. [SkyNet Express](http://www.skynet.com.my/)
7. [CityLink Express](http://www.citylinkexpress.com/MY/Consignment.aspx)
8. [FedEx Express](https://www.fedex.com/my/)
9. [LEL Express](http://www.lex.com.my/)


Tested in PHP 7.1 Only
Expand Down Expand Up @@ -107,6 +108,11 @@ $data = parcel_track()
<td></td>
<td>DHL Express Courier</td>
</tr>
<tr>
<td>dhlECommerce()</td>
<td></td>
<td>DHL E-Commerce Courier</td>
</tr>
<tr>
<td>gdex()</td>
<td></td>
Expand Down
3 changes: 2 additions & 1 deletion example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
//$response = parcel_track()->lelExpress()->setTrackingNumber("MYMP000000573505")->fetch();
//$response = parcel_track()->postLaju()->setTrackingNumber("ER287051644MY")->fetch();
//$response = parcel_track()->lelExpress()->setTrackingNumber("MYMP000000573505")->fetch();
$response = parcel_track()->dhlECommerce()->setTrackingNumber("5218031053514008AAAA")->fetch();

$response = parcel_track()->setTrackingNumber("EZP843055940197")->checkCourier();
//$response = parcel_track()->setTrackingNumber("EZP843055940197")->checkCourier();

header('Content-type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);
14 changes: 8 additions & 6 deletions library/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,22 @@ function fetch($requestUrl = null, $requestBody = [], $method = null, $header =
'status_code' => $response->getStatusCode(),
];

return array_merge($data, $this->appendJson);

} catch (\Exception $ex) {
return [
$data = [
'error' => true,
'message' => $ex->getMessage(),
'reference' => $ex->getTraceAsString()
'reference' => $ex->getTraceAsString(),
'status_code' => 400
];
} catch (GuzzleException $ex) {
return [
$data = [
'error' => true,
'message' => $ex->getMessage(),
'reference' => $ex->getTraceAsString()
'reference' => $ex->getTraceAsString(),
'status_code' => 400
];
}

return array_merge($data, $this->appendJson);
}
}
14 changes: 9 additions & 5 deletions src/Contract/BaseParcelTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use afiqiqmal\ParcelTrack\Tracker\BaseTracker;
use afiqiqmal\ParcelTrack\Tracker\CityLink;
use afiqiqmal\ParcelTrack\Tracker\DHL;
use afiqiqmal\ParcelTrack\Tracker\DHLCommerce;
use afiqiqmal\ParcelTrack\Tracker\FedEx;
use afiqiqmal\ParcelTrack\Tracker\Gdex;
use afiqiqmal\ParcelTrack\Tracker\LELExpress;
Expand Down Expand Up @@ -49,6 +50,12 @@ public function dhlExpress()
return $this;
}

public function dhlECommerce()
{
$this->source = new DHLCommerce();
return $this;
}

public function skynet()
{
$this->source = new SkyNet();
Expand Down Expand Up @@ -97,6 +104,7 @@ protected function getWhichCourier()

if (strlen($this->trackingCode) >= 14) {
$courier_matched[] = (new CityLink())->getSourceName();
$courier_matched[] = (new DHLCommerce())->getSourceName();
}

return array_merge([
Expand Down Expand Up @@ -127,11 +135,7 @@ protected function execute($requestBody)
$result = $result->fetch();
}

if (isset($result['body'])) {
return $result;
}

return null;
return $result;
}

/**
Expand Down
52 changes: 28 additions & 24 deletions src/Tracker/Abx.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,38 @@ public function setTrackingNumber($refNum)

public function startCrawl($result)
{
$crawler = new Crawler($result['body']);
if (isset($result['body'])) {
$crawler = new Crawler($result['body']);

$crawlerResult = $crawler->filter('.ResultsTableCell3')->eq(1)
->filter('tr:not(:nth-child(1)):not(:nth-child(2)):not(:nth-child(3))')
->each(function (Crawler $node, $i) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
return trim_spaces($node->text());
});
$crawlerResult = $crawler->filter('.ResultsTableCell3')->eq(1)
->filter('tr:not(:nth-child(1)):not(:nth-child(2)):not(:nth-child(3))')
->each(function (Crawler $node, $i) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
return trim_spaces($node->text());
});

$data = [];
foreach ($result as $key => $item) {
if ($key == 1) {
$parcel = Carbon::createFromFormat("d/m/Y H:i:s", $item);
$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
}
if ($key == 2) {
$data['process'] = $item;
$data['type'] = $this->distinguishProcess($item);
$data = [];
foreach ($result as $key => $item) {
if ($key == 1) {
$parcel = Carbon::createFromFormat("d/m/Y H:i:s", $item);
$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
}
if ($key == 2) {
$data['process'] = $item;
$data['type'] = $this->distinguishProcess($item);
}
if ($key == 0) {
$data['event'] = $item;
}
}
if ($key == 0) {
$data['event'] = $item;
}
}

return $data;
});
return $data;
});

return $this->buildResponse($result, $crawlerResult);
return $this->buildResponse($result, $crawlerResult);
} else {
return $this->buildResponse($result, []);
}
}
}
5 changes: 2 additions & 3 deletions src/Tracker/BaseTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ protected function buildResponse($result, $data, $status_code = 200, $reverse =
return isset($item['type']) && $item['type'] == 'delivered';
})) != null;
$tracker['checkpoints'] = $reverse ? array_reverse($data) : $data;

return [
'code' => $result['status_code'] ? $result['status_code'] : 400,
'error' => $result['status_code'] && $result['status_code'] >= 300 ? true : false,
'code' => $status_code,
'error' => $status_code >= 300 ? true : false,
'tracker' => $tracker,
'generated_at' => Carbon::now()->toDateTimeString(),
'footer' => $result['footer']
Expand Down
85 changes: 44 additions & 41 deletions src/Tracker/CityLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,62 +29,65 @@ public function setTrackingNumber($refNum)

public function startCrawl($result)
{
$crawler = new Crawler($result['body']);
// $crawlerResult = $crawler->filter('#btmPanel')->html();
$crawlerResult = $crawler->filter('#btmPanel tr')->each(function (Crawler $node, $i) {
if (strpos($node->html(), 'tabletitle') !== false) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
$value = trim_spaces($node->text());
if (strlen($value) > 2) {
return $value;
}
});

return $this->removeAllNullInArray($result)[0];
}

if (strpos($node->html(), 'table_detail') !== false) {
$result = $node->filter('table tr')->each(function (Crawler $node, $i) {
if (isset($result['body'])) {
$crawler = new Crawler($result['body']);
$crawlerResult = $crawler->filter('#btmPanel tr')->each(function (Crawler $node, $i) {
if (strpos($node->html(), 'tabletitle') !== false) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
$value = trim_spaces($node->text());
if (strlen($value) > 2) {
return $value;
}
});

return $this->removeAllNullInArray($result)[0];
}

if (strpos($node->html(), 'table_detail') !== false) {
$result = $node->filter('table tr')->each(function (Crawler $node, $i) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
$value = trim_spaces($node->text());
if (strlen($value) > 2) {
return $value;
}
});

return $this->removeAllNullInArray($result);
});
return $this->removeAllNullInArray($result);
});
return $this->removeAllNullInArray($result);
}
});
}
});

$crawlerResult = $this->removeAllNullInArray($crawlerResult);
$currentDate = null;
$finalResult = [];
foreach ($crawlerResult as $key => $item) {
//header
if ($key % 2 == 0) {
$currentDate = $item;
}
$crawlerResult = $this->removeAllNullInArray($crawlerResult);
$currentDate = null;
$finalResult = [];
foreach ($crawlerResult as $key => $item) {
//header
if ($key % 2 == 0) {
$currentDate = $item;
}

//detail
if ($key % 2 == 1) {
if (is_array($item)) {
foreach ($item as $checkpoint) {
$data = [];
$parcel = Carbon::createFromFormat('l, F d, Y H:i a', $currentDate." ".$checkpoint[1]);
$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
$data['process'] = isset($checkpoint[0]) ? $checkpoint[0] : null;
$data['type'] = $this->distinguishProcess($data['process']);
$data['event'] = isset($checkpoint[2]) ? $checkpoint[2] : null;
$finalResult[] = $data;
//detail
if ($key % 2 == 1) {
if (is_array($item)) {
foreach ($item as $checkpoint) {
$data = [];
$parcel = Carbon::createFromFormat('l, F d, Y H:i a', $currentDate . " " . $checkpoint[1]);
$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
$data['process'] = isset($checkpoint[0]) ? $checkpoint[0] : null;
$data['type'] = $this->distinguishProcess($data['process']);
$data['event'] = isset($checkpoint[2]) ? $checkpoint[2] : null;
$finalResult[] = $data;
}
}
}
}

return $this->buildResponse($result, $finalResult);
}

return $this->buildResponse($result, $finalResult);
return $this->buildResponse($result, []);
}

private function removeAllNullInArray($array)
Expand Down
74 changes: 74 additions & 0 deletions src/Tracker/DHLCommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 01/05/2018
* Time: 11:02 PM
*/

namespace afiqiqmal\ParcelTrack\Tracker;

use Carbon\Carbon;

class DHLCommerce extends BaseTracker
{
protected $url = "https://www.logistics.dhl/v1/mailitems/track";
protected $source = "DHL E-Commerce";
protected $code = "dhl_e_commerce";

public function setTrackingNumber($refNum)
{
parent::setTrackingNumber($refNum);
return [
'number' => $refNum,
'access_token' => 'nBv2wb9Uc6jW7x0EC22onGwvIA/lMR8k2Z6mem/jLNA+bQPTF80+nJiYoEWCdFQUQQW3wv4jQx8WNrGd2JEAEXYSrip77np4F7X2icSxAgorjRdabr7d1jjktOI1Z4487KpkdJes+I4byatWZRX7Uig7v/VTsztthTk8IUrWowttiEBQnw0/NjRe4drp3mFAzlzrOYtroRjZ13eqE6l+nQ==', // temporary
'client_id' => '32152', //temporary
];
}

public function getHeader()
{
return [
'User-Agent' => PARCEL_USER_AGENT,
'Accept-Encoding' => 'gzip',
'Accept' => 'application/json'
];
}

public function rawOutput()
{
return false;
}

public function startCrawl($result)
{
if (!$result['error']) {
if (isset($result['body']['data']['mailItems'][0])) {
$data = $result['body']['data']['mailItems'][0];
$events = $data['events'];
$finalOutput = [];

$output = array_reverse($events);

foreach ($output as $item) {
$data = [];
$date = trim($item['date']);
$time = trim($item['time']);
$parcel = Carbon::createFromFormat("Y-m-d H:i:s", $date." ".$time);

$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
$data['process'] = trim_spaces($item['description']);
$data['type'] = $this->distinguishProcess(trim_spaces($item['description']), $item == reset($output));
$data['event'] = trim_spaces($item['location']);

$finalOutput[] = $data;
}

return $this->buildResponse($result, $finalOutput, 200, false);
}
}

return $this->buildResponse($result, []);
}
}
Loading

0 comments on commit 3ee1384

Please sign in to comment.