generated from oklookat/gostarter
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (119 loc) · 3.1 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/oklookat/kp2imdb/cmd"
"github.com/oklookat/kp2imdb/export"
"github.com/oklookat/kp2imdb/imdb"
_ "embed"
)
const (
_linksPath = "./links.json"
)
var (
//go:embed HELP.txt
help string
)
func main() {
var useManualLink bool
flag.BoolVar(&useManualLink, "m", false, "Вручную вводить IMDB id при ошибках или ненаходе тайтла.")
flag.Parse()
kpFile := flag.Arg(0)
if len(kpFile) == 0 {
println(help)
bufio.NewReader(os.Stdin).ReadString('\n')
os.Exit(0)
return
}
links, err := export.LoadLinks(_linksPath)
chk(err)
exported, err := export.LoadKinopoisk(kpFile)
chk(err)
stack := cmd.NewStack(50)
barUid := stack.AddAlwaysBottom("")
setBar := func(i int) {
stack.AlwaysBottom[barUid] = fmt.Sprintf("\n⏳ %d/%d", i+1, len(exported))
stack.Render()
}
var imdbIds []string
for i, ke := range exported {
setBar(i)
imdbId, ok := links[ke.ID]
if ok {
// already linked.
imdbIds = append(imdbIds, imdbId)
continue
}
saveId := func(kpId, imdbId string) {
imdbIds = append(imdbIds, imdbId)
links[kpId] = imdbId
err = export.SaveLinks(_linksPath, links)
chk(err)
}
var found bool
parsed := ke.ParsedName
for i := 0; i < 2; i++ {
searchMsg := func() string {
return fmt.Sprintf("%s (%d)", parsed.Title, parsed.Year)
}
searchUid := stack.Add("🔍 " + searchMsg())
stack.Render()
imdbTitle, err := imdb.SearchTitle(parsed, i == 1)
if err != nil {
stack.Add("⚠️ " + err.Error())
stack.Render()
}
if err != nil || imdbTitle == nil {
stack.Stack[searchUid] = "❌ " + searchMsg()
stack.Render()
if i == 0 && ke.ParsedAltName != nil {
parsed = *ke.ParsedAltName
continue
}
break
}
stack.Stack[searchUid] = fmt.Sprintf("✅ %s ||| %v", searchMsg(), imdbTitle)
stack.Render()
saveId(ke.ID, imdbTitle.ID)
found = true
break
}
if !found && useManualLink {
id, err := manualLink(&stack, &ke)
chk(err)
saveId(ke.ID, id)
}
}
err = export.SaveIds(fmt.Sprintf("%d.txt", time.Now().Unix()), imdbIds)
chk(err)
}
func chk(err error) {
if err == nil {
return
}
println("Ошибка: " + err.Error())
println(`Возможные решения:
1. Убедитесь что у вас есть интернет.
2. Проверьте свой JSON файл.
3. Проверьте права доступа (links.json, создание файлов в текущей директории).
4. Проверьте links.json или удалите его.
Или опишите проблему тут: https://github.com/oklookat/kp2imdb/issues
`)
bufio.NewReader(os.Stdin).ReadString('\n')
os.Exit(1)
}
func manualLink(st *cmd.Stack, ke *export.KpExport) (string, error) {
reader := bufio.NewReader(os.Stdin)
st.Add(fmt.Sprintf(`🟦 Не найдено: %s | %s
🟦 Вставьте IMDB ID (например: tt6263850):`, ke.Name, ke.AltName))
st.Render()
id, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(id), err
}