/
update.go
155 lines (132 loc) · 3.37 KB
/
update.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
package cmd
import (
"fmt"
"math"
"os"
"path"
"strconv"
"sync"
"time"
"github.com/myzWILLmake/bgmgo/parser"
"github.com/spf13/viper"
"github.com/gen2brain/beeep"
"github.com/spf13/cobra"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update your subscriptions and download.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
update()
},
}
func init() {
rootCmd.AddCommand(updateCmd)
}
func notifyUpdate(updatedSubItems chan *SubItem) {
title := fmt.Sprintf("Bgmgo Updated %d Subcription(s)", len(updatedSubItems))
msg := ""
for {
if subItem, ok := <-updatedSubItems; ok {
if subItem.Progress == math.Floor(subItem.Progress) {
msg += fmt.Sprintf("%s updated to EP.%d.\n", subItem.Name, int(subItem.Progress))
} else {
msg += fmt.Sprintf("%s updated to EP.%.1f.\n", subItem.Name, subItem.Progress)
}
} else {
break
}
}
beeep.Notify(title, msg, "")
}
func doUpdateSubItem(subItem *SubItem, wg *sync.WaitGroup, updatedSubItems chan *SubItem) {
defer wg.Done()
source := subItem.Source
if parser.ParserCtor[source] == nil {
fmt.Println("Not valid data source: ", source)
return
}
web := parser.ParserCtor[source]()
maxEp := subItem.Progress
err := web.Request([]string{subItem.Pattern})
if err != nil {
fmt.Println("Error: failed to request", source)
fmt.Println(err)
return
}
filterMap := map[string]int{
"no": 0,
"title": 1,
}
infos := web.ShowFindResult(filterMap, 2)
newEpNumMap := make(map[float64]bool)
selectNums := []int{}
for _, info := range infos {
title := info[len(info)-1]
ep := parseEpisodeFromTitle(title)
if ep > subItem.Progress && !newEpNumMap[ep] {
newEpNumMap[ep] = true
if ep > maxEp {
maxEp = ep
}
idx, err := strconv.Atoi(info[0])
if err == nil {
selectNums = append(selectNums, idx)
}
}
}
if len(selectNums) > 0 {
fmt.Println("Subcription updated:", subItem.Name, "updated to EP.", maxEp)
magnets := web.GetMagnets(selectNums)
needTrimMagnet := viper.GetBool("enable-trim-magnet")
if needTrimMagnet {
trimMagnets(magnets)
}
useNameAsFolder := viper.GetBool("use-name-as-subscription-folder")
dir := viper.GetString("default-download-dir")
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Println("Cannot create Download folder:", err)
return
}
}
if useNameAsFolder && subItem.Name != "" {
dirTest := path.Join(dir, subItem.Name)
if _, err := os.Stat(dirTest); os.IsNotExist(err) {
if err := os.Mkdir(dirTest, 0755); err == nil {
dir = dirTest
}
} else {
dir = dirTest
}
}
err := downloadMagnets(magnets, dir)
if err != nil {
fmt.Println("Cannot connect to aria2:", err)
return
}
subItem.Progress = maxEp
subItem.Time = time.Now().Unix()
updatedSubItems <- subItem
}
}
func update() {
var wg sync.WaitGroup
wg.Add(len(globalData.Sublist))
updatedSubItems := make(chan *SubItem, len(globalData.Sublist))
for _, subItem := range globalData.Sublist {
go doUpdateSubItem(subItem, &wg, updatedSubItems)
}
fmt.Println("Checking update...")
wg.Wait()
close(updatedSubItems)
fmt.Println("Update completed:", len(updatedSubItems), "subscription(s) updated.")
if len(updatedSubItems) > 0 {
notifyUpdate(updatedSubItems)
}
err := writeData()
if err != nil {
fmt.Println("Cannot update subscription progress", err)
return
}
}