-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathbasisdataexport.php
More file actions
227 lines (202 loc) · 8.27 KB
/
basisdataexport.php
File metadata and controls
227 lines (202 loc) · 8.27 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
*
* Basis Data Export
*
* Utility that exports and saves your Basis device's uploaded sensor device data.
* You can learn more about Basis at http://www.mybasis.com/
*
* @author Bob Troia <bob@quantifiedbob.com>
* @link http://www.quantifiedbob.com
* @license MIT License (see LICENSE.md)
*
* Usage:
* This script can be run several ways. You can edit the BASIS_USERNAME, BASIS_PASSWORD,
* and BASIS_EXPORT_FORMAT values under "Settings" below so you don't have to specify
* them every time the script is run. Make sure the data/ folder is writeable!
*
* [Method 1] Via interactive mode
* a. Open a terminal window and cd to this script's directory.
* b. Type php basisdataexport.php
* c. Follow the prompts (hit ENTER to use default values)
* d. Your data will be save to /data/basis-data-[YYYY-MM-DD].[format]';
*
* [Method 2] Via command-line arguments (useful for crons)
* php basisdataexport.php -h -u[username] -p[pass] -d[YYYY-MM-DD] -f[json|csv|html]
*
* Options:
* -u Basis username (if not used, defaults to BASIS_USERNAME)
* -p Basis password (if not used, defaults to BASIS_PASSWORD)
* -d Data export date (YYYY-MM-DD) (if not used, defaults to current date)
* -f Data export format (json|csv|html) (if not used, defaults to json)
* -h Show this help text
*
* [Method 3] Via web browser
* This assumes your script is in a location that is executable via a web server,
* i.e., http://localhost/basis-data-export/basisdataexport.php?u=[basis_username]&p=[basis_password]&d=[YYYY-MM-DD]&f=[format]
*
*/
require_once(dirname(__FILE__) . '/BasisExport.class.php');
///////////////////////////////////////////////////////
// Settings
///////////////////////////////////////////////////////
// Specify your Basis username, password, and default export format. Leaving blank
// will require inputting these values manually each time the script is run.
define('BASIS_USERNAME', '');
define('BASIS_PASSWORD', '');
define('BASIS_EXPORT_FORMAT', 'json');
// Enable/disable debug mode
define('DEBUG', false);
///////////////////////////////////////////////////////
// You shouldn't need to edit anything below this line!
///////////////////////////////////////////////////////
// See if we are running in command-line mode
if (php_sapi_name() == "cli") {
// Check for command-line arguments, otherwise enter interactive mode.
if($argc > 1) {
$settings = runCommandLine();
} else {
// Enter interactive mode
$settings = runInteractive();
}
} else {
// Request has been make via web browser
$settings = runHttp();
}
// Create instance of BasisExport class
$basis = new BasisExport($settings['basis_username'], $settings['basis_password']);
$basis->debug = DEBUG;
// Query Basis API for biometric data
try {
$basis->getMetrics($settings['basis_export_date'], $settings['basis_export_format']);
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
// Query Basis API for sleep data
try {
$basis->getSleep($settings['basis_export_date'], $settings['basis_export_format']);
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
// Query Basis API for activity data
try {
$basis->getActivities($settings['basis_export_date'], $settings['basis_export_format']);
} catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
/**
* Take parameters via command-line args
**/
function runCommandLine()
{
$options = getopt("h::u::p::d::f::");
$settings = array();
$settings['basis_username'] = (!defined('BASIS_USERNAME')) ? '' : BASIS_USERNAME;
$settings['basis_password'] = (!defined('BASIS_PASSWORD')) ? '' : BASIS_PASSWORD;
$settings['basis_export_date'] = date('Y-m-d', strtotime('now', time()));
$settings['basis_export_format'] = (!defined('BASIS_EXPORT_FORMAT')) ? 'json' : BASIS_EXPORT_FORMAT;
while (list($key, $value) = each($options)) {
if ($key == 'h') {
echo "-------------------------\n";
echo "Basis data export script.\n";
echo "-------------------------\n";
echo "Usage:\n";
echo "php basisdataexport.php -u[username] -p[pass] -d[YYYY-MM-DD] -f[json|csv|html]\n\n";
echo "options:\n";
echo "-u Basis username\n";
echo "-p Basis password\n";
echo "-d Data export date (YYYY-MM-DD). If blank will use today's date\n";
echo "-f Data export format (json|csv|html)\n";
echo "-h Show this help text\n";
echo "-------------------------\n";
exit();
}
if ($key == 'u') {
if (empty($value)) {
die ("No username specified!\n");
} else {
$settings['basis_username'] = trim($value);
}
}
if ($key == 'p') {
if (empty($value)) {
die ("No password specified!\n");
} else {
$settings['basis_password'] = trim($value);
}
}
if ($key == 'd') {
if (empty($value)) {
die ("No date specified!\n");
} else {
$settings['basis_export_date'] = trim($value);
}
}
if ($key == 'f') {
if (empty($value)) {
die ("No format specified!\n");
} else {
$settings['basis_export_format'] = trim($value);
}
}
}
return $settings;
}
/**
* Take parameters via interactive shell
**/
function runInteractive()
{
$basis_username = (!defined('BASIS_USERNAME')) ? '' : BASIS_USERNAME;
$basis_password = (!defined('BASIS_PASSWORD')) ? '' : BASIS_PASSWORD;
$basis_password_mask = (!defined('BASIS_PASSWORD')) ? '' : '********';
// $basis_password_mask = (!)
$basis_export_date = date('Y-m-d', strtotime('now', time()));
$basis_export_format = (!defined('BASIS_EXPORT_FORMAT')) ? 'json' : BASIS_EXPORT_FORMAT;
$settings = array();
echo "-------------------------\n";
echo "Basis data export script.\n";
echo "-------------------------\n";
$handle = fopen ("php://stdin","r");
echo "Enter Basis username [$basis_username]: ";
$input_username = trim(fgets($handle));
$settings['basis_username'] = (empty($input_username) ? $basis_username : $input_username);
echo "Enter Basis password [$basis_password_mask]: ";
$input_password = trim(fgets($handle));
$settings['basis_password'] = (empty($input_password) ? $basis_password : $input_password);
echo "Enter data export date (YYYY-MM-DD) [$basis_export_date] : ";
$input_export_date = trim(fgets($handle));
$settings['basis_export_date'] = (empty($input_export_date) ? $basis_export_date : $input_export_date);
echo "Enter export format (json|csv|html) [$basis_export_format] : ";
$input_export_format = trim(fgets($handle));
$settings['basis_export_format'] = (empty($input_export_format) ? $basis_export_format : $input_export_format);
fclose($handle);
if (DEBUG ) {
echo "-----------------------------\n";
echo "Using the following settings:\n";
echo "-----------------------------\n";
echo 'Username: ' . $settings['basis_username'] . "\n";
echo 'Password: ' . $settings['basis_password'] . "\n";
echo 'Date: ' . $settings['basis_export_date'] . "\n";
echo 'Format: ' . $settings['basis_export_format'] . "\n";
echo "-----------------------------\n";
}
return ($settings);
}
/**
* Take parameters via http $_GET args
**/
function runHttp()
{
$basis_username = (!defined('BASIS_USERNAME')) ? '' : BASIS_USERNAME;
$basis_password = (!defined('BASIS_PASSWORD')) ? '' : BASIS_PASSWORD;
$basis_export_date = date('Y-m-d', strtotime('now', time()));
$basis_export_format = (!defined('BASIS_EXPORT_FORMAT')) ? 'json' : BASIS_EXPORT_FORMAT;
$settings = array();
$settings['basis_username'] = (empty($_GET['u']) ? $basis_username : strip_tags($_GET['u']));
$settings['basis_password'] = (empty($_GET['p']) ? $basis_password : strip_tags($_GET['p']));
$settings['basis_export_date'] = (empty($_GET['d']) ? $basis_export_date : strip_tags($_GET['d']));
$settings['basis_export_format'] = (empty($_GET['f']) ? $basis_export_format : strip_tags($_GET['f']));
return ($settings);
}
?>