forked from super3/primecoin.io
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtickerinfo.php
More file actions
105 lines (89 loc) · 3.22 KB
/
tickerinfo.php
File metadata and controls
105 lines (89 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
require('include/simple_html_dom.php');
// Configuration values
define('DB_PATH', 'xpmmarket.sqlite3');
define('INFO_EXPIRY', 90); // Seconds before cached data is re-downloaded
// Database schema is one table, as follows:
// CREATE TABLE xpmmarket(
// price REAL NOT NULL,
// market_cap REAL NOT NULL,
// total_supply INTEGER NOT NULL,
// updated DATETIME NOT NULL
// )
// Grab new data from CoinMarketCap if our data is expired
function fetch_market_info() {
// Grab the CMC homepage
$html = file_get_html("http://www.coinmarketcap.com/");
// Get Peercoin's table
$xpm_data = $html->find('#xpm', 0);
// Market cap is stored in the "data-usd" tag of a 'td'
$market_cap_td = $xpm_data->find('td[class=market-cap]', 0);
if (is_object($market_cap_td)) {
$market_cap = $market_cap_td->getAttribute('data-usd');
// Market cap is returned with commas that break floatval()
$market_cap = str_replace(",", "", $market_cap);
}
else {
$market_cap = null;
}
// Individual price is stored in the "data-usd" tag of an 'a'
$xpm_usd_a = $xpm_data->find('a[class=price]', 0);
if (is_object($xpm_usd_a)) {
$xpm_usd = $xpm_usd_a->getAttribute('data-usd');
}
else {
$xpm_usd = null;
}
// Total supply does not have a marking class,
// but at the time of writing is the 5th <td>
$total_supply_td = $xpm_data->find('td', 4);
if (is_object($total_supply_td)) {
$total_supply = $total_supply_td->plaintext;
// Ugly hack because CMC doesn't provide a data tag
// so we have to strip off the text
$total_supply = str_replace(",", "", $total_supply);
$total_supply = str_replace(" XPM", "", $total_supply);
}
else {
$total_supply = null;
}
// Prepare an array for the data,
// returned as floats
$info = array(
'price' => floatval($xpm_usd),
'market_cap' => floatval($market_cap),
'total_supply' => intval($total_supply),
);
return $info;
}
// Get either cached or new market data
function market_info() {
$db = new SQLite3(DB_PATH);
$query = $db->query("SELECT * FROM ppcmarket LIMIT 1");
$row = $query->fetchArray();
// To check if cached data has expired
$now = time();
if (!$row) {
// SQLite3 database is empty, populate it
$info = fetch_market_info();
$db->query("INSERT INTO ppcmarket(price, market_cap, total_supply, updated) VALUES({$info['price']}, {$info['market_cap']}, {$info['total_supply']}, {$now})");
}
else {
if ($now - $row['updated'] > INFO_EXPIRY) {
// Cached data is expired, fetch new data
$info = fetch_market_info();
$db->query("UPDATE ppcmarket SET price={$info['price']}, market_cap={$info['market_cap']}, total_supply={$info['total_supply']}, updated={$now}");
$row = $info;
}
// Else, return the cached data
$info = array(
'price' => $row['price'],
'market_cap' => $row['market_cap'],
'total_supply' => $row['total_supply'],
);
}
return $info;
}
// Return a JSON array of price / market cap / total supply
echo(JSON_encode(market_info()));
?>