-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
117 lines (96 loc) · 1.93 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
)
const imgPath string = "PATH OF IMAGES STORAGE DIRECTORY"
const csvPath string = "PATH OF CSV FILES FROM EXPORT TOOLS"
const numberOfWorker int = 10
const urlIndex int = 12
const nameIndex int = 32
const reportRate = 100 // report progress every 100 download
var r, _ = regexp.Compile("[0-9]+$")
type data struct {
url string
name string
}
func main() {
input := make(chan data, 10)
output := make(chan bool, 10)
var counter int = 0 // use to name downloaded image files
var queue []data
csvFile, err := os.Open(csvPath)
if err != nil {
log.Fatal(err)
}
reader := csv.NewReader(csvFile)
/* read csv file */
for {
line, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
/* get rid of lichen from fungi dataset */
if !strings.Contains(line[nameIndex], "lichen") {
queue = append(queue, data{
url: line[urlIndex],
name: line[nameIndex],
})
}
}
queue = queue[1:] // remove title row
/* init worker */
for i := 0; i < numberOfWorker; i++ {
url := queue[0]
queue = queue[1:]
counter++
input <- url
go worker(input, output)
}
for i := 0; i < len(queue); i++ {
data := queue[i]
<-output
input <- data
go worker(input, output)
if i%reportRate == 0 {
fmt.Println(i)
}
}
}
func worker(input chan data, done chan bool) {
data := <-input
url := data.url
name := data.name
resp, err := http.Get(url)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
path := imgPath + name + "/"
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, os.ModePerm)
}
index := r.FindStringSubmatch(url)
file, err := os.Create(path + strings.Join(index, "") + ".jpg")
if err != nil {
log.Println(err)
return
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
log.Println(err)
return
}
done <- true
}