/
list_parse_actors.go
203 lines (181 loc) · 5.43 KB
/
list_parse_actors.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bytes"
"io"
"github.com/BurntSushi/csql"
"github.com/BurntSushi/goim/imdb"
)
func listActors(db *imdb.DB, ractor, ractress io.ReadCloser) (err error) {
defer csql.Safe(&err)
logf("Reading actors list...")
// PostgreSQL wants different transactions for each inserter.
// SQLite can't handle them. The wrapper type here ensures that
// PostgreSQL gets multiple transactions while SQLite only gets one.
tx, err := db.Begin()
csql.Panic(err)
txactor := wrapTx(db, tx)
txcredit := txactor.another()
txname := txactor.another()
txatom := txactor.another()
// Drop data from the actor and credit tables. They will be rebuilt below.
// The key here is to leave the atom and name tables alone. Invariably,
// they will contain stale data. But the only side effect, I think, is
// taking up space.
// (Stale data can be removed with 'goim clean'.)
csql.Truncate(txactor, db.Driver, "actor")
csql.Truncate(txcredit.Tx, db.Driver, "credit")
actIns, err := csql.NewInserter(txactor.Tx, db.Driver, "actor",
"atom_id", "sequence")
csql.Panic(err)
credIns, err := csql.NewInserter(txcredit.Tx, db.Driver, "credit",
"actor_atom_id", "media_atom_id", "character", "position", "attrs")
csql.Panic(err)
nameIns, err := csql.NewInserter(txname.Tx, db.Driver, "name",
"atom_id", "name")
csql.Panic(err)
atoms, err := newAtomizer(db, txatom.Tx)
csql.Panic(err)
// Unfortunately, it looks like credits for an actor can appear in
// multiple locations. (Or there are different actors that erroneously
// have the same name.)
added := make(map[imdb.Atom]struct{}, 3000000)
n1, nc1 := listActs(db, ractress, atoms, added, actIns, credIns, nameIns)
n2, nc2 := listActs(db, ractor, atoms, added, actIns, credIns, nameIns)
csql.Panic(actIns.Exec())
csql.Panic(credIns.Exec())
csql.Panic(nameIns.Exec())
csql.Panic(atoms.Close())
csql.Panic(txactor.Commit())
csql.Panic(txcredit.Commit())
csql.Panic(txname.Commit())
csql.Panic(txatom.Commit())
logf("Done. Added %d actors/actresses and %d credits.", n1+n2, nc1+nc2)
return
}
type credit struct {
ActorId imdb.Atom
MediaId imdb.Atom
Character string
Position int
Attrs string
}
func listActs(
db *imdb.DB,
r io.ReadCloser,
atoms *atomizer,
added map[imdb.Atom]struct{},
actIns, credIns, nameIns *csql.Inserter,
) (addedActors, addedCredits int) {
bunkName, bunkTitles := []byte("Name"), []byte("Titles")
bunkLines1, bunkLines2 := []byte("----"), []byte("------")
listAttrRows(r, atoms, func(line, idstr, row []byte) {
if bytes.Equal(idstr, bunkName) && bytes.Equal(row, bunkTitles) {
return
}
if bytes.Equal(idstr, bunkLines1) && bytes.Equal(row, bunkLines2) {
return
}
var a imdb.Actor
existed, err := parseId(atoms, idstr, &a.Id)
if err != nil {
csql.Panic(err)
}
if !existed {
if !parseActorName(idstr, &a) {
logf("Could not parse actor name '%s' in '%s'.", idstr, line)
return
}
// We only add a name when we've added an atom.
if err := nameIns.Exec(a.Id, a.FullName); err != nil {
csql.Panic(ef("Could not add actor name '%s' from '%s': %s",
idstr, line, err))
}
}
// If we haven't seen this actor before, then insert into actor table.
if _, ok := added[a.Id]; !ok {
if len(a.FullName) == 0 {
if !parseActorName(idstr, &a) {
logf("Could not get actor name '%s' in '%s'.", idstr, line)
return
}
}
if err := actIns.Exec(a.Id, a.Sequence); err != nil {
csql.Panic(ef("Could not add actor info '%#v' from '%s': %s",
a, line, err))
}
added[a.Id] = struct{}{}
addedActors++
}
// Reading this list always refreshes the credits.
var c credit
c.ActorId = a.Id
if !parseCredit(atoms, row, &c) {
// messages are emitted in parseCredit if something is worth
// reporting
return
}
err = credIns.Exec(c.ActorId, c.MediaId,
c.Character, c.Position, c.Attrs)
if err != nil {
csql.Panic(ef("Could not add credit '%s' for '%s': %s",
row, idstr, err))
}
addedCredits++
})
return
}
func parseActorName(idstr []byte, a *imdb.Actor) bool {
var name, sequence []byte
if idstr[len(idstr)-1] == ')' {
fields := bytes.Fields(idstr)
last := fields[len(fields)-1]
if last[0] == '(' && last[len(last)-1] == ')' {
name = bytes.Join(fields[0:len(fields)-1], []byte{' '})
sequence = last[1 : len(last)-1]
} else {
name = idstr
}
} else {
name = idstr
}
sep := bytes.IndexByte(name, ',')
if sep > -1 {
var flipped []byte
l, f := bytes.TrimSpace(name[0:sep]), bytes.TrimSpace(name[sep+1:])
flipped = append(flipped, f...)
flipped = append(flipped, ' ')
flipped = append(flipped, l...)
name = flipped
}
a.FullName = unicode(name)
a.Sequence = unicode(sequence)
return true
}
func parseCredit(atoms *atomizer, row []byte, c *credit) bool {
pieces := bytes.Split(row, []byte{' ', ' '})
ent := bytes.TrimSpace(pieces[0])
if id, ok := atoms.atomOnlyIfExist(ent); !ok {
warnf("Could not find media id for '%s'. Skipping.", ent)
return false
} else {
c.MediaId = id
}
for _, f := range pieces[1:] {
f = bytes.TrimSpace(f)
if len(f) < 3 {
continue
}
switch {
case f[0] == '<' && f[len(f)-1] == '>':
if err := parseInt(f[1:len(f)-1], &c.Position); err != nil {
pef("Could not parse '%s' as integer in '%s': %s", f, row, err)
return false
}
case f[0] == '[' && f[len(f)-1] == ']':
c.Character = unicode(bytes.TrimSpace(f[1 : len(f)-1]))
case f[0] == '(' && f[len(f)-1] == ')':
c.Attrs = unicode(f)
}
}
return true
}