-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimFinApi.bas
190 lines (156 loc) · 5.72 KB
/
SimFinApi.bas
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
Attribute VB_Name = "SimFinApi"
Option Explicit
Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr
Public Function URLEncode( _
StringVal As String, _
Optional SpaceAsPlus As Boolean = False _
) As String
Dim StringLen As Long: StringLen = Len(StringVal)
If StringLen > 0 Then
ReDim result(StringLen) As String
Dim i As Long, CharCode As Integer
Dim Char As String, Space As String
If SpaceAsPlus Then Space = "+" Else Space = "%20"
For i = 1 To StringLen
Char = Mid$(StringVal, i, 1)
CharCode = Asc(Char)
Select Case CharCode
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
result(i) = Char
Case 32
result(i) = Space
Case 0 To 15
result(i) = "%0" & Hex(CharCode)
Case Else
result(i) = "%" & Hex(CharCode)
End Select
Next i
URLEncode = Join(result, "")
End If
End Function
Function GetOperatingSystem() As String
Dim os As String
os = Application.OperatingSystem
If InStr(1, os, "Windows") > 0 Then
GetOperatingSystem = "Windows"
ElseIf InStr(1, os, "Macintosh") > 0 Then
GetOperatingSystem = "Mac"
Else
GetOperatingSystem = "Unknown"
End If
End Function
Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As LongPtr
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Function SimFin(Ticker As String, Year As String, Period As String, Columname As String, Token As String, Optional Ttm As String = "false", Optional AsReported As String = "false") As Variant
Dim JsonObject As Object
Dim objRequest As Object
Dim strUrl As String
Dim blnAsync As Boolean
Dim strResponse As String
Dim os As String
Dim output As Variant
If IsMissing(AsReported) Then
AsReported = "false"
End If
If IsMissing(Ttm) Then
Ttm = "false"
End If
os = GetOperatingSystem()
strUrl = "https://backend.simfin.com/api/v3/excel-plugin/statements?ticker=" + URLEncode(Ticker) + "&period=" + Period + "&fyear=" + Year + "&columnName=" + URLEncode(Columname) + "&asreported=" + AsReported + "&ttm=" + Ttm
If os = "Windows" Then
Set objRequest = CreateObject("MSXML2.ServerXMLHTTP")
blnAsync = True
With objRequest
.Open "GET", strUrl, blnAsync
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "api-key " + Token
.Send
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strResponse = .responseText
End With
Else
Dim curlCommand As String
curlCommand = "curl -s -H 'Content-Type: application/json' -H 'Authorization: api-key " & Token & "' -o - """ & strUrl & """"
strResponse = execShell(curlCommand)
End If
If IsNumeric(Trim(strResponse)) Then
output = Trim(strResponse) * 1
Else
output = strResponse
End If
SimFin = output
End Function
Function SimFinPrices(Ticker As String, DateString As String, Columname As String, Token As String, Optional AsReported As String) As Variant
Dim JsonObject As Object
Dim objRequest As Object
Dim strUrl As String
Dim blnAsync As Boolean
Dim strResponse As String
Dim output As Variant
Dim os As String
Dim var1 As Variant
Dim var2 As Variant
Dim t1 As Variant
Dim t2 As Variant
If IsMissing(AsReported) Then
AsReported = "false"
End If
DateString = Format(CDate(DateString), "yyyy-mm-dd")
os = GetOperatingSystem()
strUrl = "https://backend.simfin.com/api/v3/excel-plugin/prices?ticker=" + URLEncode(Ticker) + "&start=" + DateString + "&columnName=" + URLEncode(Columname) + "&asreported=" + AsReported
If os = "Windows" Then
Set objRequest = CreateObject("MSXML2.XMLHTTP")
blnAsync = True
With objRequest
.Open "GET", strUrl, blnAsync
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "api-key " + Token
.Send
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strResponse = .responseText
End With
Else
Dim curlCommand As String
curlCommand = "curl -s -H 'Content-Type: application/json' -H 'Authorization: api-key " & Token & "' -o - """ & strUrl & """"
strResponse = execShell(curlCommand)
End If
If IsNumeric(Trim(strResponse)) Then
var1 = CDbl(Trim(strResponse))
var2 = CDbl(Trim(Replace(strResponse, ".", ",")))
t1 = Abs(var1)
t2 = Abs(var2)
If t2 < t1 Then
output = t2
Else
output = t1
End If
Else
output = strResponse
End If
SimFinPrices = output
End Function