-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadBook.go
132 lines (118 loc) · 2.77 KB
/
loadBook.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
package main
import (
"bufio"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
)
func main() {
// Connect to MongoDB
session, err := mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
// Get the collection
c := session.DB("aozora").C("books_go")
// Get current title list
m := []bson.M{}
books := make(map[string]bool)
if err := c.Find(nil).All(&m); err == nil { // Do Query
for _, v := range m {
books[v["title"].(string)] = false
}
}
fname := "fnameList.csv"
// Open File
f, ferr := os.Open(fname)
if ferr != nil {
fmt.Printf("Open file failed: %s\n", ferr)
panic(ferr)
}
defer f.Close()
r := bufio.NewReader(f)
s, e := Readln(r)
for e == nil {
ary := strings.Split(s, ",")
if id, err := strconv.Atoi(ary[0]); err == nil && id > 1000000 {
author, title := GetTitle(ary[1])
if _, ok := books[title]; !ok {
fmt.Printf("%d | %s : %s\n", id, author, title)
_, err = c.Upsert(bson.M{"id": id}, bson.M{"$set": bson.M{
"id": id,
"title": title,
"title_": title,
"otitle": title,
"tag": make([]string, 10),
"cata": make([]string, 3),
"cardlink": "",
"booklink": ary[1],
"txtlink": ary[1],
"author_id": 0,
"author": author,
"author_en": author,
"open_at": time.Now(),
"update_at": time.Now(),
"load_at": time.Now(),
},
},
)
if err != nil {
panic(err)
}
}
}
s, e = Readln(r)
}
}
// Readln returns a single line (without the ending \n)
// from the input buffered reader.
// An error is returned iff there is an error with the
// buffered reader.
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func GetTitle(file string) (string, string) {
if strings.Contains(file, "/") {
file = path.Base(file)
}
re1 := regexp.MustCompile(".txt")
re2 := regexp.MustCompile("(校正.*)")
fname := re1.ReplaceAllString(file, "")
str := re2.ReplaceAllString(fname, " ")
author, title := "", ""
if strings.Contains(str, "-") {
ary := strings.Split(str, "-")
author, title = ary[0], ary[1]
} else if strings.Contains(str, "-") {
ary := strings.Split(str, "-")
author, title = ary[0], ary[1]
} else if strings.Contains(str, " ") {
idx := strings.Index(str, " ")
author, title = str[0:idx-1], str[idx+1:]
} else if strings.Contains(str, "_") {
idx := strings.LastIndex(str, "_")
author, title = str[0:idx-1], str[idx+1:]
} else {
panic(str)
}
if strings.Contains(title, "(") {
title = title[0 : len(title)-2]
}
return author, title
}