/
getSTOCKS.php
executable file
·185 lines (119 loc) · 4.45 KB
/
getSTOCKS.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/php
<?php
error_reporting(0);
$pluginName ="StockTicker";
$myPid = getmypid();
$messageQueue_Plugin = "MessageQueue";
$MESSAGE_QUEUE_PLUGIN_ENABLED=false;
$DEBUG=false;
$LOG_LEVEL=0;
$skipJSsettings = 1;
$fppWWWPath = '/opt/fpp/www/';
set_include_path(get_include_path() . PATH_SEPARATOR . $fppWWWPath);
$ENABLED="";
require("common.php");
include_once("functions.inc.php");
include_once("commonFunctions.inc.php");
require ("lock.helper.php");
$logFile = $settings['logDirectory']."/".$pluginName.".log";
$messageQueuePluginPath = $pluginDirectory."/".$messageQueue_Plugin."/";
$messageQueueFile = urldecode(ReadSettingFromFile("MESSAGE_FILE",$messageQueue_Plugin));
if(file_exists($messageQueuePluginPath."functions.inc.php"))
{
include $messageQueuePluginPath."functions.inc.php";
$MESSAGE_QUEUE_PLUGIN_ENABLED=true;
} else {
logEntry("Message Queue Plugin not installed, some features will be disabled");
}
define('LOCK_DIR', '/tmp/');
define('LOCK_SUFFIX', $pluginName.'.lock');
$pluginConfigFile = $settings['configDirectory'] . "/plugin." .$pluginName;
if (file_exists($pluginConfigFile))
$pluginSettings = parse_ini_file($pluginConfigFile);
if(urldecode($pluginSettings['DEBUG'] != "" || urldecode($pluginSettings['DEBUG'] != 0))) {
$DEBUG=urldecode($pluginSettings['DEBUG']);
}
foreach ($pluginSettings as $key => $value) {
if($DEBUG)
echo "Key: ".$key." " .$value."\n";
$$key = urldecode($value);
}
if(($pid = lockHelper::lock()) === FALSE) {
exit(0);
}
if($ENABLED != "ON" && $ENABLED != "1") {
logEntry("Plugin Status: DISABLED Please enable in Plugin Setup to use & Restart FPPD Daemon");
lockHelper::unlock();
exit(0);
}
$MATRIX_MESSAGE_PLUGIN_NAME = "MatrixMessage";
//page name to run the matrix code to output to matrix (remote or local);
$MATRIX_EXEC_PAGE_NAME = "matrix.php";
$LOG_LEVEL = getFPPLogLevel();
logEntry("Log level in translated from fpp settings file: ".$LOG_LEVEL);
$logFile = $settings['logDirectory']."/".$pluginName.".log";
if($DEBUG){
logEntry("________________________");
logEntry("Plugin Settings");
logEntry("________________________");
while (list($key, $val) = each($pluginSettings)) {
logEntry("$key => $val");
}
}
$messageText="";
logEntry("Log Level: ".$LOG_LEVEL);
$quotesToGet = explode(",",trim(strtoupper($QUOTES)));
if($DEBUG)
logEntry("Quotes to get: ".$QUOTES);
if($DEBUG)
logEntry("Market type: ".$MARKET_TYPE);
$quoteIndex =0;
foreach ($quotesToGet as $q) {
if($DEBUG)
logEntry("getting quote for: ".$q);
//Obtain Quote Info - This collects the Microsoft Stock Info
$quote = file_get_contents("http://finance.google.co.uk/finance/info?client=ig&q=".$MARKET_TYPE.":".$q);
//Remove CR's from ouput - make it one line
$json = str_replace("\n", "", $quote);
//Remove //, [ and ] to build qualified string
$data = substr($json, 4, strlen($json) -5);
//decode JSON data
$json_output = json_decode($data, true);
//Un-remark print_r to see all array keys
if($DEBUG)
print_r($json_output);
//Output Stock price array key.
$stockValue = $json_output['l'];
$stockChange = $json_output['c'];
//echo "\nPrice: ".$stockValue;
//echo "\nChange: ".$stockChange;
if(count($quotesToGet)>1 && $quoteIndex > 0) {
$messageText .= " | ";
}
$messageText .= " ".$q." ".$stockValue." [".$stockChange."]";
$quoteIndex++;
//if there is more than one stock ticker, then put a PIPE symbol on the string
}
if($MESSAGE_QUEUE_PLUGIN_ENABLED) {
addNewMessage($messageText,$pluginName,$QUOTES);
} else {
logEntry("MessageQueue plugin is not enabled/installed: Cannot add message: ".$messageText);
}
if($IMMEDIATE_OUTPUT != "on" && $IMMEDIATE_OUTPUT != "1") {
logEntry("NOT immediately outputting to matrix");
} else {
logEntry("IMMEDIATE OUTPUT ENABLED");
logEntry("Matrix location: ".$MATRIX_LOCATION);
logEntry("Matrix Exec page: ".$MATRIX_EXEC_PAGE_NAME);
if($MATRIX_LOCATION != "127.0.0.1") {
$remoteCMD = "/usr/bin/curl -s --basic 'http://".$MATRIX_LOCATION."/plugin.php?plugin=".$MATRIX_MESSAGE_PLUGIN_NAME."&page=".$MATRIX_EXEC_PAGE_NAME."&nopage=1' > /dev/null";
logEntry("REMOTE MATRIX TRIGGER: ".$remoteCMD);
exec($remoteCMD);
} else {
$IMMEDIATE_CMD = $settings['pluginDirectory']."/".$MATRIX_MESSAGE_PLUGIN_NAME."/matrix.php";
logEntry("LOCAL command: ".$IMMEDIATE_CMD);
exec($IMMEDIATE_CMD);
}
}
lockHelper::unlock();
?>