-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetch-and-parse.php
189 lines (160 loc) · 4.6 KB
/
fetch-and-parse.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
186
187
188
189
<?php
include("Robinhood.php");
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
session_start();
$logged_in = false;
$username = null;
$password = null;
$mfa_code = null;
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if( isset($_POST["Rusername"]) && isset($_POST["Rpassword"]) )
{
$username= test_input($_POST["Rusername"]);
$password= test_input($_POST["Rpassword"]);
}
if( isset($_POST['mfa_code']) )
{
$mfa_code = test_input($_POST["mfa_code"]);
$_SESSION['mfa_code'] = $mfa_code;
if( isset($_SESSION['username']) && isset($_SESSION['password']))
{
$username = $_SESSION['username'];
$password= $_SESSION['password'];
//unset($_SESSION['username']);
//unset($_SESSION['password']);
}
}
$robinhood = new Robinhood();
//while (($logged_in != true))
while(!($logged_in === true))
{
if (isset($username) && isset($password) && isset($mfa_code) )
{
$logged_in = kwargs_method_call($robinhood, 'login', [], ["username" => $username,"password" => $password,"mfa_code" => $mfa_code]);
}
else if ( isset($username) && isset($password) )
{
unset($_SESSION['mfa_code']);// Unset stray/previous mfa
print("Logging in with uNam & pswd ony");
$logged_in = kwargs_method_call($robinhood, 'login', [], ["username" => $username,"password" => $password]);
print("logged_in:");
print_r($logged_in);
}
// Login incomplete. Requires MFA
if (!($logged_in === true) /*&& ($logged_in->get('non_field_errors') == null)*/ && ($logged_in['mfa_required'] == true))
{
$_SESSION['mfa_needed'] = true;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("Location:http://$host$uri/login.php");
exit();
// Get mfa, and then
//$logged_in = kwargs_method_call($robinhood, 'login', [], ["username" => $username,"password" => $password,"mfa_code" => $mfa_code]);
}
// Login failed
if (isset($logged_in['non_field_errors']))
{
print("inside");
print_r($logged_in);
$password = '';
$_SESSION['invalid_login'] = true;
$_SESSION['error_msg'] = "Oops! Something is not right. Invalid Robinhood Login credentials provided";
header("Location:http://$host$uri/login.php");
print_r('Invalid username or password. Try again.');
exit();
}
}
if($logged_in === true)
{
print("Logged In!!");
print('Pulling trades. Please wait...');
$trade_count = 0;
$queued_count = 0;
$orders = $robinhood->get_endpoint('orders');
$orders = var_export($orders, false);//
var_dump($orders);
$paginated = true;
$page = 0;
while ($paginated)
{
foreach( array_values($orders['results']) as $i => $order)
{
$executions = $order['executions'];
$instrument = $robinhood->get_custom_endpoint($order['instrument']);
$fields[($i + ($page * 100))]['symbol'] = $instrument['symbol'];
foreach( array_keys($order) as $key => $value)
{
if (($value != 'executions')) {
$fields[($i + ($page * 100))][$value] = $order[$value];
}
}
if (($order['state'] == 'filled'))
{
$trade_count += 1;
foreach( array_keys($executions[0]) as $key => $value)
{
$fields[($i + ($page * 100))][$value] = $executions[0][$value];
}
}
else if (($order['state'] == 'queued'))
{
$queued_count += 1;
}
}
print_r(sprintf("Summary #", $page, ":", "queued_count=", $queued_count));
if (($orders['next'] != null))
//if (isset($orders['next']))
{
$page = ($page + 1);
$orders = $robinhood->get_custom_endpoint($orders['next']);
$orders = var_export($orders, false);
// Handle error cases when $orders turns out to be not valid
if (isset($orders['results']))
continue;
else
{
print_r("Fetching next pages of orders failed.");
$paginated = false;
}
}
else
{
$paginated = false;
}
}
if (($trade_count > 0) || ($queued_count > 0))
{
print_r(sprintf('%d queued trades and %d executed trades found in your account.', $queued_count, $trade_count));
}
else {
print('No trade history found in your account.');
exit;
}
$keys = array_keys($fields[0]);
ksort($keys);
$csv = ','. $keys . '\n';
foreach( $fields as $row )
{
foreach( array_keys($keys) as $idx => $key)
{
if (($idx > 0)) {
$csv .= ',';
}
$csv += (string)$fields[$row][$key];
}
$csv .= '\n';
}
print_r($csv);
//if ($args->profit) {
// $profit_csv = new profit_extractor($csv, $filename);
//}
}//logged in
}// if post
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}