· 5 years ago · Apr 25, 2020, 04:06 PM
1class BitcoinPriceIndex{
2
3 //var $bitfinex_source_url = 'https://api.bitfinex.com/v1/pubticker/BTCUSD';
4 var $bitfinex_source_url = 'https://www.binance.com/api/v1/ticker/24hr?symbol=BTCUSDT';
5 var $bitstamp_source_url = 'https://www.bitstamp.net/api/v2/ticker/btcusd/';
6 var $time_period_to_grab = '900';//in seconds, 15 minutes
7// var $time_period_to_grab = '5';//5 seconds
8
9
10 var $default_navigator = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36';
11
12// function action($data){
13// $action = $data['action'];
14// switch ($action) {
15// case 'get_data':
16// echo $this->getJsonData();
17// break;
18//
19// default:
20// break;
21// }
22//
23// }
24
25 function grabData(){
26
27 $last_saved_date = $this->getLatestSavedDate();
28
29 $data = array();
30 //grab Bitfinex
31// $jsondata = $this->curlRequest($this->bitfinex_source_url);
32 $jsondata = file_get_contents($this->bitfinex_source_url);
33
34 $ar_data = json_decode($jsondata,true);
35// $data['timestamp'] = $ar_data['timestamp'];
36// $data['bitfinex_volume'] = round($ar_data['volume'], 2);
37// $data['bitfinex_ask'] = round($ar_data['ask'], 2);
38 //$data['timestamp'] = $ar_data['openTime'];
39 $data['bitfinex_volume'] = round($ar_data['volume'], 2);
40 $data['bitfinex_ask'] = round($ar_data['lastPrice'], 2);
41
42 //grab Bitstamp
43// $jsondata = $this->curlRequest($this->bitstamp_source_url);
44 $jsondata = file_get_contents($this->bitstamp_source_url);
45 $ar_data = json_decode($jsondata,true);
46
47 $data['timestamp'] = $ar_data['timestamp'];
48 $data['bitstamp_volume'] = round($ar_data['volume'], 2);
49 $data['bitstamp_ask'] = round($ar_data['ask'], 2);
50
51 echo "Last saved date: ".date("Y-m-d H:i:s", $last_saved_date);
52 echo "<br>";
53 echo "Current date: ".date("Y-m-d H:i:s", $data['timestamp']);
54 echo "<br>";
55 echo "Save next data after date: ".date("Y-m-d H:i:s", ($last_saved_date + $this->time_period_to_grab));
56
57 if(($last_saved_date + $this->time_period_to_grab) <= $data['timestamp']){
58 echo "<pre>";
59 print_r($data);
60 $this->saveData($data);
61 echo "Data saved";
62 }
63
64 }
65
66
67 function curlRequest($url, $cookie_file, $post_params_string = '', $request_method = false, $referer = ''){
68
69 $ch = curl_init();
70 curl_setopt($ch, CURLOPT_URL, $url);
71
72 if($post_params_string != '' || $request_method == true){
73 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params_string);
74 curl_setopt($ch, CURLOPT_POST, 1);
75 }
76
77 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
78 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
79 'Accept-Charset: utf-8',
80 'Accept-Language: en-us,en;q=0.7,bn-bd;q=0.3',
81 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
82 ));
83 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
84 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
85 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
86 curl_setopt($ch, CURLOPT_USERAGENT, $this->default_navigator);
87 if($referer != '') curl_setopt($ch, CURLOPT_REFERER, $referer);
88
89 $html = curl_exec($ch);
90
91// echo curl_error($ch);
92
93 curl_close($ch);
94 return $html;
95 }
96
97 function saveData($data){
98 global $wpdb;
99
100 // (Оборот BTC за сутки на BitFinex * Цена BTC BitFinex + Оборот BTC за сутки на Bitstamp * Цена BTC на Bitstamp)/ (Оборот BTC за сутки на BitFinex + Оборот BTC за сутки на Bitstamp)
101 $index_value = ($data['bitfinex_volume'] * $data['bitfinex_ask'] + $data['bitstamp_volume'] * $data['bitstamp_ask']) / ($data['bitfinex_volume'] + $data['bitstamp_volume']);
102 $index_value = round($index_value, 2);
103
104 $res = $wpdb->insert($wpdb->prefix."bitcoin_price_index", array(
105 "bitfinex_ask" => $data['bitfinex_ask'],
106 "bitfinex_volume" => $data['bitfinex_volume'],
107 "bitstamp_ask" => $data['bitstamp_ask'],
108 "bitstamp_volume" => $data['bitstamp_volume'],
109 "index_value" => $index_value,
110 "time" => $data['timestamp']
111
112 ));
113
114 }
115
116 function getLatestSavedDate(){
117 global $wpdb;
118
119 $records = $wpdb->get_results("SELECT time from ". $wpdb->prefix."bitcoin_price_index order by id desc limit 1");
120
121 foreach( $records as $row ) {
122 return $row->time;
123 }
124 }
125
126 function getJsonData(){
127 global $wpdb;
128 $data = array();
129 $records = $wpdb->get_results("select index_value, time from ".$wpdb->prefix."bitcoin_price_index order by id asc");
130 foreach( $records as $row ) {
131 $sub = array((int)($row->time * 1000), (int)$row->index_value);
132
133 array_push($data, $sub);
134 }
135
136// $json = str_replace('"', '', json_encode($data));
137 $json = json_encode($data);
138 return $json;
139
140 }
141
142}
143
144function bitcoin_price_index_parse_request($wp) {
145 if (array_key_exists('bitcoin_price_index', $wp->query_vars)) {
146 include("price-index.class.php");
147 $scraper = new BitcoinPriceIndex();
148 //sends json data to chart
149 if(isset($wp->query_vars['action']) && $wp->query_vars['action'] == 'get_data') {
150 header('Content-Type: application/json');
151 echo $scraper->getJsonData();
152 //retrieves data from bitfinex and bitstamp by cron job
153 }elseif(isset($wp->query_vars['action']) && $wp->query_vars['action'] == 'grab_data'){
154 header('Content-Type: text/html; charset=utf-8');
155 $scraper->grabData();
156 }
157 die();
158 }
159}
160
161
162function bitcoin_price_index_query_vars($vars) {
163 $vars[] = 'bitcoin_price_index';
164 $vars[] = 'action';
165 return $vars;
166}
167
168
169function bitcoin_price_index_chart_function() {
170 include_once("price-index-frontend.class.php");
171 $chart = new BitcoinPriceIndexFrontend();
172 $html = $chart->showChart();
173 return $html;
174}
175
176function bitcoin_price_index_price_function(){
177 include_once("price-index-frontend.class.php");
178 $chart = new BitcoinPriceIndexFrontend();
179 $html = $chart->showBitcoinPrice();
180 return $html;
181}
182
183
184function bitcoin_price_on_activation() {
185 global $wpdb;
186 $charset_collate = $wpdb->get_charset_collate();
187 $table_name = $wpdb->prefix . 'bitcoin_price_index';
188
189
190 $sql = "CREATE TABLE $table_name (
191 id int(11) unsigned NOT NULL AUTO_INCREMENT,
192 bitfinex_ask float DEFAULT NULL,
193 bitfinex_volume float DEFAULT NULL,
194 bitstamp_ask float DEFAULT NULL,
195 bitstamp_volume float DEFAULT NULL,
196 index_value float DEFAULT NULL,
197 time int(11) DEFAULT NULL,
198 PRIMARY KEY (`id`)
199 ) $charset_collate;";
200
201 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
202 dbDelta( $sql );
203}
204
205function bitcoin_price_on_uninstall(){
206 global $wpdb;
207 $table_name = $wpdb->prefix . 'bitcoin_price_index';
208 $sql = "DROP TABLE IF EXISTS $table_name";
209 $wpdb->query($sql);
210}