This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
146 lines (124 loc) · 2.63 KB
/
common.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
package dor
import (
"archive/zip"
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
// downloadURL downloads file to a temporary file
func downloadURL(url string, descr string) (filename string, error error) {
zf, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("dor-%s-", descr))
if err != nil {
return "", err
}
resp, err := http.Get(url)
if err != nil {
zf.Close()
os.Remove(zf.Name())
return "", err
}
defer resp.Body.Close()
defer zf.Close()
_, err = io.Copy(zf, resp.Body)
if err != nil {
zf.Close()
os.Remove(zf.Name())
return "", err
}
return zf.Name(), nil
}
// zipContent unpacks zip file in memory with one file inside
func zipContent(path string) (zipFile *zip.ReadCloser, f *io.ReadCloser, error error) {
r, err := zip.OpenReader(path)
if err != nil {
return nil, nil, err
}
rc, err := r.File[0].Open()
if err != nil {
r.Close()
return nil, nil, err
}
return r, &rc, nil
}
// mapFromURLZip translates a common formatted CSV:
// rank,domain
// that is packed in ZIP and hosted on a specified url
func mapFromURLZip(url string, desc string) (LookupMap, error) {
n, err := downloadURL(url, desc)
if err != nil {
return nil, err
}
z, c, err := zipContent(n)
if err != nil {
return nil, err
}
defer (*c).Close()
defer z.Close()
defer os.Remove(n)
m := make(LookupMap)
scanner := bufio.NewScanner(*c)
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ",")
pos, d := parts[0], parts[1]
pint, _ := strconv.ParseInt(pos, 10, 32)
puint := uint32(pint)
m[d] = puint
}
return m, nil
}
// chanFromURLZip translates a common formatted CSV:
// rank,domain
// that is ZIP packed and hosted on a specified url
func chanFromURLZip(url, desc string, rc chan *Entry, sep string, offset int) {
n, err := downloadURL(url, desc)
if err != nil {
close(rc)
log.Println(err)
return
}
log.Printf("%s: %s downloaded successfully", desc, url)
z, c, err := zipContent(n)
if err != nil {
close(rc)
log.Println(err)
return
}
defer (*c).Close()
defer z.Close()
defer os.Remove(n)
scanner := bufio.NewScanner(*c)
var (
i int
line string
)
for scanner.Scan() {
i++
line = scanner.Text()
if i < offset {
continue
}
parts := strings.Split(line, sep)
if len(parts) < 2 {
continue
}
if strings.Contains(parts[0], "\"") || strings.Contains(parts[1], "\"") {
parts[0] = strings.Trim(parts[0], "\"")
parts[1] = strings.Trim(parts[1], "\"")
}
rc <- &Entry{
Rank: strToUint(parts[0]),
Domain: parts[1],
}
}
close(rc)
}
func strToUint(s string) uint32 {
pint, _ := strconv.ParseInt(s, 10, 32)
return uint32(pint)
}