forked from LeOndaz/qurandw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
170 lines (134 loc) · 3.61 KB
/
app.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"qurandw/api"
"qurandw/utils"
"slices"
"sync"
)
func main() {
var locale string
var concurrentDownloads int
var reverseOrder bool
var outputDir string
var singleChapterName string
var singleChapterId int
flag.StringVar(&locale, "locale", "ar", "--locale <language>")
flag.IntVar(&concurrentDownloads, "batches", 10, "--batches <amount>")
flag.BoolVar(&reverseOrder, "reverse", false, "--reverse <true/false>")
defaultDir, err := os.Getwd()
flag.StringVar(&outputDir, "output", defaultDir, "--output dir/to/write/into")
flag.StringVar(&singleChapterName, "chapter", "", "--chapter <name>")
flag.IntVar(&singleChapterId, "chapterid", -1, "--chapterid <id>")
flag.Parse()
if err != nil {
log.Fatalf(err.Error())
}
if len(locale) > 2 {
log.Fatalf("invalid language code %s", locale)
}
recitations, err := api.GetRecitations(locale)
if err != nil {
log.Fatalf(err.Error())
}
api.SortRecitationsById(recitations)
api.PrettyPrintRecitationResponse(recitations)
var userInputRecitationId int
var recitation *api.Recitation
for {
print(">> Enter reciter number or exit CTRL/CMD-C")
userInputRecitationId, err = utils.ExpectUint()
if err != nil {
continue
}
// FIXME: improve handling userInputRecitationId by checking if the id is in recitations
if userInputRecitationId > len(recitations) {
continue
}
for _, r := range recitations {
if r.ID == userInputRecitationId {
recitation = &r
break
}
}
break
}
audioFiles, err := api.GetAudioFilesOfRecitation(userInputRecitationId, locale)
if err != nil {
print(err.Error())
println(
"Most of the time, this means you've provided a number that's not in the list",
)
}
if reverseOrder {
slices.SortStableFunc(audioFiles, func(i, j api.AudioFile) int {
return -1
})
}
var chapters []api.Chapter
if len(singleChapterName) > 0 {
var chapter *api.Chapter
chapter, err = api.GetChapterByName(locale, singleChapterName)
if err != nil {
panic(err)
}
tempList := [1]api.Chapter{*chapter}
chapters = tempList[:]
} else if singleChapterId > 0 && singleChapterId <= 114 { // There are 114 chapters in the Quran
var chapter *api.Chapter = &api.Chapter{}
chapter, err = api.GetChapterById(locale, singleChapterId)
if err != nil {
panic(err)
}
tempList := [1]api.Chapter{*chapter}
chapters = tempList[:]
} else {
chapters, err = api.GetAllChapters(locale)
if err != nil {
panic(err)
}
}
wg := sync.WaitGroup{}
progressData := &utils.Progress{
TotalProgress: api.GetTotalAudioFilesSize(audioFiles),
CurrentProgress: 0,
}
for i, audioFile := range audioFiles {
chapter := api.FilterChapterById(chapters, audioFile.ChapterId)
if chapter == nil {
continue
}
fileName := fmt.Sprintf(
"%s.%s",
api.GetLocalChapterName(chapter, locale),
audioFile.Format,
)
filePath := path.Join(outputDir, recitation.TranslatedName.Name, fileName)
err := os.MkdirAll(path.Dir(filePath), os.ModePerm)
if err != nil {
log.Fatal(err.Error())
}
wg.Add(1)
go func(order int, audioFile api.AudioFile) {
defer wg.Done()
fileDownload := &utils.FileDownload{
Url: audioFile.AudioUrl,
OutputPath: filePath,
}
err := utils.DownloadFile(fileDownload, progressData)
if err != nil {
log.Fatalf("failed to download %s\n: %s", audioFile.AudioUrl, err.Error())
}
}(i, audioFile)
// wait for the concurrentDownloads number to download, then proceed
if i%concurrentDownloads == 0 {
wg.Wait()
}
}
wg.Wait()
fmt.Printf("Downloaded in %s", path.Join(outputDir, recitation.ReciterName))
}