Skip to content

Instantly share code, notes, and snippets.

@prionkor
Last active August 29, 2015 14:16
Show Gist options
  • Save prionkor/0c93d0b38fd7e19c21ec to your computer and use it in GitHub Desktop.
Save prionkor/0c93d0b38fd7e19c21ec to your computer and use it in GitHub Desktop.
Scrapper
<?php
namespace Numstock\Models;
use Sunra\PhpSimple\HtmlDomParser;
class Dse_News{
protected $url = 'http://dsebd.org/display_news.php';
public function __construct(){
}
/**
* @return Array|\WP_Error
*
* */
public function get(){
try{
$html = $this->download();
if(is_wp_error($html)){
return $html;
}
$dom = HtmlDomParser::str_get_html($html);
$tables = $dom->find('table[width=605] table', 0)->find('table');
$news = array();
foreach($tables as $table){
if( empty($table->plaintext) || $table->plaintext == 'No new news found for today:'){
return array();
}
$item = array();
$rows = $table->find('tbody tr');
foreach($rows as $row){
$td = $row->find('td');
$key = strtolower(str_replace(array(' ', ':'), array('_', ''), trim($td[0]->plaintext)));
$value = trim($td[1]->plaintext);
if($key == 'trading_code'){
$value = strtolower($value);
}
$item[$key] = $value;
$item['datetime'] = current_time('mysql');
}
$news[] = $item;
}
return $news;
}catch (\Exception $e){
return new \WP_Error($e->getCode(), $e->getMessage());
}
}
protected function download(){
$res = wp_remote_get($this->url, array('timeout' => 30));
if(is_wp_error($res)){
return $res;
}
if( absint(wp_remote_retrieve_response_code($res)) !== 200
|| 'OK' !== wp_remote_retrieve_response_message($res) ) {
return new \WP_Error('not_200', 'Response code is not 200');
}
return wp_remote_retrieve_body($res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment