Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwebable committed Jul 23, 2018
0 parents commit 91c926d
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
.DS_STORE
phpunit.xml
composer.phar
composer.lock
composer-test.lock
vendor/
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2018 SIMON GOMES

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#Dhaka & Chittagong Stock Exchange Update (DSE & CSE)

A PHP library to fetch updates from Bangladesh share market, including Dhaka and Chittagong Stock Exchange (DSE & CSE).

[DSE](https://www.dsebd.org), [CSE](http://www.cse.com.bd)

##Installation
Download the latest release.

[![Latest Version](https://img.shields.io/badge/release-v1.0.0-blue.svg?longCache=true&style=for-the-badge)](#)

####Usages
- Include the library
```php
<?php
require __DIR__ . '/lib/autoload.php';
use Simon\BDShareMarket;
```
- Create an object and call the necessary
```php
$BDShareMarket = new BDShareMarket();

// this will print the DSE data
print_r($BDShareMarket->getDSEData());
```

####Output
```
Array
(
[0] => Array
(
[company] => 1JANATAMF
[ltp] => 6.1
[high] => 6.1
[low] => 6.1
[closep] => 6.1
[ycp] => 6.2
[change] => -0.1
[trade] => 28
[value] => 0.25
[volume] => 40,942
)
[1] => Array
(
[company] => 1STPRIMFMF
[ltp] => 11.4
[high] => 11.4
[low] => 11.2
[closep] => 11.4
[ycp] => 11.2
[change] => 0.2
[trade] => 107
[value] => 1.973
[volume] => 174,194
)
[2] => Array
(
[company] => AAMRANET
[ltp] => 91.2
[high] => 92
[low] => 85.3
[closep] => 91
[ycp] => 84.2
[change] => 7
[trade] => 3,011
[value] => 144.176
[volume] => 1,616,666
)
...
```

## License

[![License](https://img.shields.io/github/license/mashape/apistatus.svg?longCache=true&style=for-the-badge)](http://opensource.org/licenses/MIT)

Copyright (c) 2018, <a href="https://simongomes.me" target="_blank">Simon Gomes</a>
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "simon/bd-stock-exchange-update",
"description": "PHP library to fetch bangladesh share market updates, including Dhaka and Chittagong Stock Exchange (DSE & CSE).",
"type": "library",
"keywords": ["library", "bd stock exchange", "dse", "cse", "bd share market", "bangladesh", "bangladesh stock exchange", "bangladesh share market", "dhaka stock exchange", "chittagong stock exchange"],
"authors": [
{
"name": "Simon Gomes",
"email": "busy.s.simon@gmail.com",
"homepage": "https://simongomes.me/"
}
],
"minimum-stability": "dev",
"require": {},
"license": "MIT",
"autoload": {
"files": ["lib/autoload.php"],
"psr-4": {
"Simon\\BDShareMarket\\": "lib/BDShareMarket"
}
}
}
52 changes: 52 additions & 0 deletions lib/BDShareMarket/BDShareMarket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Author: Simon Gomes
* Author URL: http://busysimon.com
*/

namespace Simon;

class BDShareMarket {

// CSE Updates
/**
* @return array
* Returns CSE Latest Update
*/
public function getCSEData() {
$CSEUpdate = new CSEUpdate();
return $CSEUpdate->getData();
}

/**
* @param $company_name
* @return array
* Returns CSE Latest Update for specific Company
*/
public function getCSECompanyData($company_name) {
$CSEUpdate = new CSEUpdate();
return $CSEUpdate->getCompanyData($company_name);
}


// DSE Updates
/**
* @return array
* Returns CSE Latest Update
*/
public function getDSEData() {
$DSEUpdate = new DSEUpdate();
return $DSEUpdate->getData();
}

/**
* @param $company_name
* @return array
* Returns CSE Latest Update for specific Company
*/
public function getDSECompanyData($company_name) {
$DSEUpdate = new DSEUpdate();
return $DSEUpdate->getCompanyData($company_name);
}

}
110 changes: 110 additions & 0 deletions lib/BDShareMarket/CSEUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Created by PhpStorm.
* User: SIMON
* Date: 7/16/2018
* Time: 3:56 PM
*/

namespace Simon;

use DOMDocument;
use DOMXPath;

class CSEUpdate {

private $siteUrl;
private $companyName;

public function __construct($siteUrl = 'http://cse.com.bd/current_share_price_tc.php') {
$this->siteUrl = $siteUrl;
}

/**
* Sets the Site URL
* @param $siteUrl
*/
public function setSiteUrl($siteUrl) {
$this->siteUrl = $siteUrl;
}

/**
* Returns the Site URL
* @return string
*/
public function getSiteUrl() {
return $this->siteUrl;
}

/**
* @return array
* Fetches and returns latest update from CSE
*/
public function getData() {
$htmlData = $this->getSiteContent($this->siteUrl);
$DOM = new DOMDocument();
@$DOM->loadHTML($htmlData);
$htmlContents = new DOMXPath($DOM);

$array = array();

foreach ($htmlContents->query("//table[contains(concat(' ',normalize-space(@id),' '),' report ')] //tr") as $key => $node) {
if( !$key ) continue;
$array[] = $this->cleanData($node->nodeValue);
}

return $array;
}

/**
* @param $company_name
* @return array
* Fetches and returns data of provided company
*/
public function getCompanyData($company_name) {
$this->companyName = $company_name;
$allData = $this->getData();

$companyData = array_filter($allData, function($item) {
return $item['company'] === $this->companyName;
});

return array_pop($companyData);
}

/**
* @param $data
* @return array
* Beautify and makes the fetched data human readable
*/
protected function cleanData($data = null)
{
$data = utf8_decode($data);
preg_match_all('([\w-\.]+)', $data, $cleaned);
return $newArray = array(
'company' => $cleaned[0][1],
'ltp' => $cleaned[0][2],
'high' => $cleaned[0][3],
'low' => $cleaned[0][4],
'ycp' => $cleaned[0][5],
'close_price' => $cleaned[0][6],
'change' => $cleaned[0][7],
'trade' => $cleaned[0][8],
'volume' => $cleaned[0][9]
);
}

/**
* @param $siteUrl
* @return mixed
* Makes cURL request to fetch the data
*/
protected function getSiteContent($siteUrl) {
$ch = curl_init ($siteUrl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
curl_close ($ch) ;
return ($response);
}

}
106 changes: 106 additions & 0 deletions lib/BDShareMarket/DSEUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Created by PhpStorm.
* User: SIMON
* Date: 7/16/2018
* Time: 6:14 PM
*/

namespace Simon;

use DOMDocument;
use DOMXPath;

class DSEUpdate {
private $siteUrl;
private $companyName;

public function __construct($siteUrl = 'https://www.dsebd.org/latest_share_price_all.php') {
$this->siteUrl = $siteUrl;
}

/**
* Sets the Site URL
* @param $siteUrl
*/
public function setSiteUrl($siteUrl) {
$this->siteUrl = $siteUrl;
}

/**
* Returns the Site URL
* @return string
*/
public function getSiteUrl() {
return $this->siteUrl;
}

/**
* @return array
* Fetches and returns latest update from DSE
*/
public function getData() {
$htmlData = $this->getSiteContent($this->siteUrl);
$DOM = new DOMDocument();
@$DOM->loadHTML($htmlData);
$htmlContents = new DOMXPath($DOM);

$array = array();

$counter = 0;
$index = 0;

foreach ($htmlContents->query("//table //tr //td") as $key => $node) {

if( $key <= 10 ) continue;
if( $counter > 10 ) {$counter = 0; $index++;}
if($counter) {
switch ($counter) {
case 1: $array[$index]['company'] = trim($node->nodeValue); break;
case 2: $array[$index]['ltp'] = $node->nodeValue; break;
case 3: $array[$index]['high'] = $node->nodeValue; break;
case 4: $array[$index]['low'] = $node->nodeValue; break;
case 5: $array[$index]['closep'] = $node->nodeValue; break;
case 6: $array[$index]['ycp'] = $node->nodeValue; break;
case 7: $array[$index]['change'] = $node->nodeValue; break;
case 8: $array[$index]['trade'] = $node->nodeValue; break;
case 9: $array[$index]['value'] = $node->nodeValue; break;
case 10: $array[$index]['volume'] = $node->nodeValue; break;
}
}
$counter++;
}

return $array;
}

/**
* @param $company_name
* @return array
* * Fetches and returns data of provided company
*/
public function getCompanyData($company_name) {
$this->companyName = $company_name;
$allData = $this->getData();

$companyData = array_filter($allData, function($item) {
return $item['company'] === $this->companyName;
});

return array_pop($companyData);
}

/**
* @param $siteUrl
* @return mixed
* Makes cURL request to fetch the data
*/
protected function getSiteContent($siteUrl) {
$ch = curl_init ($siteUrl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
curl_close ($ch) ;
return ($response);
}

}
Loading

0 comments on commit 91c926d

Please sign in to comment.