-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspacewrapper.go
More file actions
188 lines (169 loc) · 4.54 KB
/
spacewrapper.go
File metadata and controls
188 lines (169 loc) · 4.54 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
/*
Package spacewrapper implements
an wrapper to access ocr.space OCR service
*/
package spacewrapper
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
)
type config struct {
apikey string
}
/*
File it's a file
*/
type File struct {
FileName string //test.jpg
Content []byte //[]byte
}
/*
Params are the request options alias
*/
type Params map[string]string
/*
DefaultConfig API configuration
*/
var DefaultConfig config
/*
ProcessedDoc returns an processed OCR'ed document
*/
type ProcessedDoc struct {
ParsedResults []struct {
TextOverlay struct {
Lines []interface{} `json:"Lines"`
HasOverlay bool `json:"HasOverlay"`
Message string `json:"Message"`
} `json:"TextOverlay"`
TextOrientation string `json:"TextOrientation"`
FileParseExitCode int `json:"FileParseExitCode"`
ParsedText string `json:"ParsedText"`
ErrorMessage string `json:"ErrorMessage"`
ErrorDetails string `json:"ErrorDetails"`
} `json:"ParsedResults"`
OCRExitCode int `json:"OCRExitCode"`
IsErroredOnProcessing bool `json:"IsErroredOnProcessing"`
ProcessingTimeInMilliseconds string `json:"ProcessingTimeInMilliseconds"`
SearchablePDFURL string `json:"SearchablePDFURL"`
}
/*
Init creates a default configuration file
*/
func Init(key string) {
DefaultConfig.apikey = key
}
func buildForm(paramTexts Params, file File) (*bytes.Buffer, string, error) {
paramTexts["apikey"] = DefaultConfig.apikey
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
for k, v := range paramTexts {
bodyWriter.WriteField(k, v)
}
if file.FileName != "" {
fileWriter, err := bodyWriter.CreateFormFile("file", file.FileName)
if err != nil {
return nil, "", err
}
fileWriter.Write(file.Content)
}
bodyWriter.Close()
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
return bodyBuf, contentType, nil
}
/*
Get makes a requisition with the informed URL
*/
func Get(imageURL string, args Params) (ProcessedDoc, error) {
argString := ""
doc := ProcessedDoc{}
baseURL := fmt.Sprintf("https://api.ocr.space/parse/imageurl?apikey=%s&url=%s", DefaultConfig.apikey, imageURL)
for k, v := range args {
argString = strings.Join([]string{argString, fmt.Sprintf("&%s=%s", k, v)}, "")
}
resp, err := http.Get(strings.Join([]string{baseURL, argString}, ""))
if err != nil {
return ProcessedDoc{}, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&doc)
if err != nil {
return ProcessedDoc{}, err
}
return doc, nil
}
/*
PostFile submits a file
*/
func PostFile(paramTexts Params, paramFile File) (ProcessedDoc, error) {
paramTexts["apikey"] = DefaultConfig.apikey
doc := ProcessedDoc{}
url := "https://api.ocr.space/parse/image"
form, contentType, err := buildForm(paramTexts, paramFile)
resp, err := http.Post(url, contentType, form)
if err != nil {
return doc, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return doc, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
err = json.NewDecoder(resp.Body).Decode(&doc)
if err != nil {
return ProcessedDoc{}, err
}
return doc, nil
}
/*
PostBase64 submits a file
*/
func PostBase64(paramTexts Params, base64 string) (ProcessedDoc, error) {
paramTexts["base64Image"] = base64
doc := ProcessedDoc{}
url := "https://api.ocr.space/parse/image"
form, contentType, err := buildForm(paramTexts, File{})
resp, err := http.Post(url, contentType, form)
if err != nil {
return doc, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return doc, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
err = json.NewDecoder(resp.Body).Decode(&doc)
if err != nil {
return ProcessedDoc{}, err
}
return doc, nil
}
/*
PostURL submits a url
*/
func PostURL(paramTexts Params, URL string) (ProcessedDoc, error) {
paramTexts["apikey"] = DefaultConfig.apikey
paramTexts["url"] = URL
doc := ProcessedDoc{}
url := "https://api.ocr.space/parse/image"
form, contentType, err := buildForm(paramTexts, File{})
resp, err := http.Post(url, contentType, form)
if err != nil {
return doc, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
b, _ := ioutil.ReadAll(resp.Body)
return doc, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))
}
err = json.NewDecoder(resp.Body).Decode(&doc)
if err != nil {
return ProcessedDoc{}, err
}
return doc, nil
}