-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
147 lines (131 loc) · 3.19 KB
/
main.go
File metadata and controls
147 lines (131 loc) · 3.19 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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/atotto/clipboard"
"github.com/otiai10/gosseract"
"github.com/schollz/closestmatch"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
var codes = make(map[string]string)
var imageFile string
var key string
func main() {
setupFlags()
log.Println("image path", imageFile)
f, err := os.Open(imageFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
hash := hex.EncodeToString(h.Sum(nil))
dir := os.TempDir()
fileName := fmt.Sprintf("%s/%s.json", dir, hash)
// check if the parsed OCR file exists so we dont have to create it again and rely on its data
if _, err := os.Stat(fileName); err == nil {
file, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(file, &codes)
if err != nil {
log.Fatal(err)
}
val, err := findNemIdKey(key)
if err != nil {
log.Fatal(err)
}
err = setClipboardValue(val)
if err != nil {
log.Printf("Could not set clipboard value %v", err)
}
fmt.Printf("Key: %s \nCode: %s - Copied to clipboard", key, val)
return
}
client := gosseract.NewClient()
err = client.SetImage(imageFile)
if err != nil {
log.Fatal(err)
}
text, _ := client.Text()
err = client.Close()
if err != nil {
log.Fatal(err)
}
words := strings.Fields(text)
for idx, word := range words {
if len(words)-1 > idx && len(word) == 4 {
if _, err := strconv.Atoi(word); err == nil {
key := word
code := words[idx+1]
codes[key] = code
}
}
}
jsonString, _ := json.Marshal(codes)
err = ioutil.WriteFile(fileName, jsonString, 0644)
if err != nil {
log.Fatal(err)
}
val, err := findNemIdKey(key)
if err != nil {
log.Fatal(err)
}
err = setClipboardValue(val)
if err != nil {
log.Printf("Could not set clipboard value %v", err)
}
fmt.Printf("Key: %s \nCode: %s \n Code copied to clipboard", key, val)
}
// findNemIdKey will find a specific key and returns its value if it exists or return an error
func findNemIdKey(key string) (string, error) {
if _, ok := codes[key]; !ok {
keys := make([]string, 0, len(codes))
for k := range codes {
keys = append(keys, k)
}
bagSizes := []int{2, 3, 4}
cm := closestmatch.New(keys, bagSizes)
match := cm.Closest(key)
var msg string
if match != "" {
msg = fmt.Sprintf("Could not find key %s did you mean %s", key, match)
} else {
msg = fmt.Sprintf("Could not find key %s", key)
}
return "", errors.New(msg)
}
return codes[key], nil
}
// setupFlags will setup all the necessary flags for the CLI to run
func setupFlags() {
flag.StringVar(&imageFile, "image", "", "Path to a NemID image file 125% zoom level")
flag.StringVar(&key, "key", "", "NemID 4 digit key")
flag.Parse()
if imageFile == "" {
log.Fatal("You must provide a NemID image path using the --image option")
}
if _, err := os.Stat(imageFile); os.IsNotExist(err) {
log.Fatalf("image file does not exists %s", imageFile)
}
if key == "" {
log.Fatal("You must provide a 4 digit key using the --key option")
}
}
func setClipboardValue(value string) error {
err := clipboard.WriteAll(value)
return err
}