Skip to content

Instantly share code, notes, and snippets.

@f1dz
Created June 8, 2018 06:51
Show Gist options
  • Save f1dz/3d4b834aa7e47163db43404fdee6aa3f to your computer and use it in GitHub Desktop.
Save f1dz/3d4b834aa7e47163db43404fdee6aa3f to your computer and use it in GitHub Desktop.
Fleetmon
<?php
/**
* Created by PhpStorm.
* User: ofid
* Date: 04/06/18
* Time: 15.35
*
* @author Khofidin <offiedz@gmail.com>
*/
namespace app\components\scraper\fleetmon;
use app\components\Helpers;
use Goutte\Client;
use yii\base\Component;
use yii\base\Exception;
class Fleetmon extends Component
{
const MAIN_URL = 'https://www.fleetmon.com/vessels/';
public $imo;
public $fleetmon_id;
/**
* @var LatestPositionModel
*/
private $position;
/**
* @var Client $client
*/
private $client;
public function init()
{
$this->client = new Client();
parent::init();
}
/**
* @return LatestPositionModel
*/
public function latestPosition(){
$this->position = new LatestPositionModel();
$url = self::MAIN_URL . $this->imo . '_' . $this->fleetmon_id;
$crawler = $this->client->request('GET', $url);
$this->position->imo = $this->imo;
try{
$crawler->filter('#lat-long')->each(function($node){
$text = trim($node->text());
$data = preg_split('/[^\dSNWE]+/', $text);
$latLong = Helpers::DMStoDD($data);
$this->position->latitude = $latLong->latitude;
$this->position->longitude = $latLong->longitude;
});
$crawler->filter('#status-speed > span:nth-child(2)')->each(function ($node){
$text = trim($node->text());
$data = explode(' ', $text);
$this->position->speed = (double)@$data[0];
});
$crawler->filter('#status-course > span')->each(function ($node){
$text = trim($node->text());
preg_match('/[\d]+/', $text, $course);
$this->position->course = (int)@$course[0];
});
$crawler->filter('#status-position-received > div:nth-child(1) > span > time')->each(function ($node){
$text = trim($node->attr('datetime'));
$dt = new \DateTime($text);
$this->position->received_time = $dt->format('Y-m-d H:i:s');
});
$crawler->filter('#position-breadcrumb')->each(function ($node){
$text = trim($node->attr('data-pos'));
$this->position->area = $text;
});
$crawler->filter('#status-nav-status > span')->each(function ($node){
$text = trim($node->text());
$this->position->status = $text;
});
}catch (Exception $e){}
return $this->position;
}
}
<?php
/**
* Created by PhpStorm.
* User: ofid
* Date: 04/06/18
* Time: 10.46
*
* @author Khofidin <offiedz@gmail.com>
*/
namespace app\components\scraper\fleetmon;
use yii\base\Model;
class LatestPositionModel extends Model
{
public $imo;
public $latitude;
public $longitude;
public $speed;
public $course;
public $received_time;
public $area;
public $status;
public function rules()
{
return [
[['imo'], 'required'],
[['latitude','longitude','speed'], 'number'],
[['course'], 'integer'],
[['area','status'], 'string', 'max' => 128],
[['received_time'], 'safe']
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment