/
file_title.go
77 lines (72 loc) · 1.43 KB
/
file_title.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
package builder_pg
import (
"bufio"
"database/sql"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
type Title struct {
ID int
Name string
YearStart sql.NullInt32
YearEnd sql.NullInt32
Language string
DOI string
}
const (
idF = 0
nameF = 4
yearStartF = 7
yearEndF = 8
langF = 9
)
func (b BuilderPG) prepareTitle(doiMap map[int]string) (map[int]*Title, error) {
log.Println("Processing title.txt")
res := make(map[int]*Title)
path := filepath.Join(b.Config.DownloadDir, "title.txt")
f, err := os.Open(path)
if err != nil {
return res, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
header := true
for scanner.Scan() {
if header {
header = false
continue
}
l := scanner.Text()
fields := strings.Split(l, "\t")
id, err := strconv.Atoi(fields[idF])
if err != nil {
return res, err
}
t := &Title{
ID: id,
Name: fields[nameF],
Language: fields[langF],
}
ys, err := strconv.Atoi(fields[yearStartF])
if err == nil {
t.YearStart = sql.NullInt32{Int32: int32(ys), Valid: true}
} else {
t.YearStart = sql.NullInt32{Valid: false}
}
ye, err := strconv.Atoi(fields[yearEndF])
if err == nil {
t.YearEnd = sql.NullInt32{Int32: int32(ye), Valid: true}
} else {
t.YearEnd = sql.NullInt32{Valid: false}
}
t.DOI = doiMap[t.ID]
res[t.ID] = t
}
if err := scanner.Err(); err != nil {
return res, err
}
return res, nil
}