-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_litres_authors.go
135 lines (106 loc) · 3.2 KB
/
get_litres_authors.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
package cli
import (
"fmt"
"github.com/beauxarts/fedorov/data"
"github.com/beauxarts/scrinium/litres_integration"
"github.com/boggydigital/coost"
"github.com/boggydigital/dolo"
"github.com/boggydigital/kvas"
"github.com/boggydigital/kvas_dolo"
"github.com/boggydigital/nod"
"golang.org/x/exp/maps"
"net/url"
"strings"
)
func GetLitResAuthorsHandler(u *url.URL) error {
var ids []string
if idstr := u.Query().Get("id"); idstr != "" {
ids = strings.Split(idstr, ",")
}
var authorTypesStr []string
if ats := u.Query().Get("author-type"); ats != "" {
authorTypesStr = strings.Split(ats, ",")
}
authorTypes := make([]litres_integration.AuthorType, 0, len(authorTypesStr))
if allAuthorTypes := u.Query().Has("all-author-types"); allAuthorTypes {
authorTypes = litres_integration.AllAuthorTypes()
} else {
for _, ats := range authorTypesStr {
authorTypes = append(authorTypes, litres_integration.ParseAuthorType(ats))
}
}
force := u.Query().Has("force")
return GetLitResAuthors(authorTypes, force, ids...)
}
func GetLitResAuthors(authorTypes []litres_integration.AuthorType, force bool, ids ...string) error {
glaa := nod.NewProgress("getting litres authors...")
defer glaa.End()
if len(ids) == 0 {
persons := make(map[string]interface{})
rdx, err := data.NewReduxReader(data.ArtsHistoryOrderProperty, data.PersonsIdsProperty)
if err != nil {
return glaa.EndWithError(err)
}
if artsIds, ok := rdx.GetAllValues(data.ArtsHistoryOrderProperty, data.ArtsHistoryOrderProperty); ok {
for _, id := range artsIds {
if personsIds, sure := rdx.GetAllValues(data.PersonsIdsProperty, id); sure {
for _, pid := range personsIds {
persons[pid] = nil
}
}
}
}
ids = maps.Keys(persons)
}
glaa.TotalInt(len(ids))
absCookiesFilename, err := data.AbsCookiesFilename()
if err != nil {
return glaa.EndWithError(err)
}
hc, err := coost.NewHttpClientFromFile(absCookiesFilename)
if err != nil {
return glaa.EndWithError(err)
}
dc := dolo.NewClient(hc, dolo.Defaults())
for _, at := range authorTypes {
if err := getSetAuthorType(dc, at, force, ids...); err != nil {
return glaa.EndWithError(err)
}
}
glaa.EndWithResult("done")
return nil
}
func getSetAuthorType(dc *dolo.Client, at litres_integration.AuthorType, force bool, ids ...string) error {
gsat := nod.NewProgress(" %s...", at)
defer gsat.End()
absAuthorTypeDir, err := data.AbsAuthorTypeDir(at)
if err != nil {
return gsat.EndWithError(err)
}
kv, err := kvas.ConnectLocal(absAuthorTypeDir, kvas.JsonExt)
if err != nil {
return gsat.EndWithError(err)
}
newIds := make([]string, 0, len(ids))
for _, id := range ids {
if !force && kv.Has(id) {
continue
}
newIds = append(newIds, id)
}
indexSetter := kvas_dolo.NewIndexSetter(kv, newIds...)
urls := make([]*url.URL, 0, len(newIds))
for _, id := range newIds {
urls = append(urls, litres_integration.AuthorUrl(at, id))
}
result := "done"
if errs := dc.GetSet(urls, indexSetter, gsat, force); len(errs) > 0 {
errIds := make([]string, 0, len(errs))
for ii := range errs {
errIds = append(errIds, newIds[ii])
}
result = fmt.Sprintf("GetSet error ids: %s", strings.Join(errIds, ","))
}
gsat.EndWithResult(result)
return nil
}