This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeili.go
178 lines (140 loc) · 3.1 KB
/
meili.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/cheggaaa/pb/v3"
"github.com/meilisearch/meilisearch-go"
)
// Document type for MeiliSearch
type Document struct {
ID string
MPN string
Weight float32
Packaging string
Title string
URL string
Manufacturer string
Stock int
Datasheet []string
Package string
Categories []string
Status string
StockSZ int
StockJS int
StockHK int
Updated time.Time
}
func getEnqueued(client *meilisearch.Client) int {
var enqueued int
list, err := client.Updates("lcsc").List()
if err != nil {
log.Fatalln(err)
}
for _, u := range list {
if u.Status == meilisearch.UpdateStatusEnqueued {
enqueued++
}
}
return enqueued
}
func newMeiliClient() *meilisearch.Client {
var client = meilisearch.NewClientWithCustomHTTPClient(meilisearch.Config{
Host: meiliHost,
APIKey: "",
}, http.Client{
Timeout: 10 * time.Second,
})
// Create an index if your index does not already exist
_, err := client.Indexes().Create(meilisearch.CreateIndexRequest{
UID: "lcsc",
Name: "LCSC",
PrimaryKey: "ID",
})
if err != nil {
fmt.Println(err)
// os.Exit(1)
}
return client
}
func convertAndImport(client *meilisearch.Client, products []Product) error {
var documents []Document
updated := time.Now()
for _, p := range products {
datasheets := make([]string, 0, len(p.Datasheet))
for _, d := range p.Datasheet {
datasheets = append(datasheets, d)
}
d := Document{
ID: p.Number,
MPN: p.Info.Number,
Weight: p.Info.Weight,
Packaging: p.Info.Packaging,
Title: p.Info.Title,
URL: p.URL,
Manufacturer: p.Manufacturer.EN,
Stock: p.Stock,
Datasheet: datasheets,
Package: p.Package,
Categories: p.Categories,
Status: p.Status,
StockSZ: p.StockSZ,
StockJS: p.StockJS,
StockHK: p.StockHK,
Updated: updated,
}
documents = append(documents, d)
}
_, err := client.Documents("lcsc").AddOrUpdate(documents)
return err
}
func importFolder(path string) {
client := newMeiliClient()
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
fmt.Println("Pushing data to MeiliSearch...")
bar := pb.StartNew(len(files))
for _, file := range files {
var products []Product
bar.Increment()
if !strings.HasSuffix(file.Name(), "json") {
continue
}
f, err := os.Open("../json/" + file.Name())
if err != nil {
log.Fatalln(err)
}
dec := json.NewDecoder(f)
if err := dec.Decode(&products); err != nil {
log.Fatalln(err)
}
if products == nil {
continue
}
if err := convertAndImport(client, products); err != nil {
fmt.Println(file.Name())
fmt.Println(err)
os.Exit(1)
}
}
bar.Finish()
fmt.Println("Waiting for updates to finish...")
var updateCount = getEnqueued(client)
bar = pb.StartNew(updateCount)
for {
running := getEnqueued(client)
if running == 0 {
break
}
bar.SetCurrent(int64(updateCount - running))
time.Sleep(time.Second)
}
bar.Finish()
}