-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
395 lines (334 loc) · 11.3 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package main
import (
"math"
"sync"
"fmt"
"flag"
"log"
"time"
"os"
"strings"
"io/fs"
"io/ioutil"
"path/filepath"
"encoding/xml"
"github.com/fsnotify/fsnotify"
)
const (
// Header is a generic XML header suitable for use with the output of Marshal.
// This is not automatically added to any output of this package,
// it is provided as a convenience.
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
)
type Tvshow struct {
XMLName xml.Name `xml:"tvshow"`
Plot string `xml:"plot"`
Outline string `xml:"outline"`
Lockdata string `xml:"lockdata"`
Dateadded string `xml:"dateadded"`
Title string `xml:"title"`
Originaltitle string `xml:"originaltitle"`
Trailer string `xml:"trailer"`
Rating string `xml:"rating"`
Year string `xml:"year"`
Mpaa string `xml:"mpaa"`
ImdbID string `xml:"imdb_id"`
Tmdbid string `xml:"tmdbid"`
Premiered string `xml:"premiered"`
Releasedate string `xml:"releasedate"`
Enddate string `xml:"enddate"`
Runtime string `xml:"runtime"`
Genre []string `xml:"genre"`
Studio string `xml:"studio"`
Tag []string `xml:"tag"`
Tvrageid string `xml:"tvrageid"`
Tvdbid string `xml:"tvdbid"`
Art struct {
Poster string `xml:"poster"`
Fanart string `xml:"fanart"`
} `xml:"art"`
Actor []struct {
Name string `xml:"name"`
Role string `xml:"role"`
Type string `xml:"type"`
Sortorder string `xml:"sortorder"`
Thumb string `xml:"thumb"`
} `xml:"actor"`
ID string `xml:"id"`
Episodeguide struct {
URL struct {
Cache string `xml:"cache,attr"`
} `xml:"url"`
} `xml:"episodeguide"`
Season string `xml:"season"`
Episode string `xml:"episode"`
Status string `xml:"status"`
}
type Movie struct {
XMLName xml.Name `xml:"movie"`
Plot string `xml:"plot"`
Lockdata string `xml:"lockdata"`
Dateadded string `xml:"dateadded"`
Title string `xml:"title"`
Originaltitle string `xml:"originaltitle"`
Director string `xml:"director"`
Writer []string `xml:"writer"`
Credits []string `xml:"credits"`
Trailer []string `xml:"trailer"`
Rating string `xml:"rating"`
Year string `xml:"year"`
Mpaa string `xml:"mpaa"`
Imdbid string `xml:"imdbid"`
Tmdbid string `xml:"tmdbid"`
Premiered string `xml:"premiered"`
Releasedate string `xml:"releasedate"`
Criticrating string `xml:"criticrating"`
Runtime string `xml:"runtime"`
Tagline string `xml:"tagline"`
Country []string `xml:"country"`
Genre []string `xml:"genre"`
Studio []string `xml:"studio"`
Tag []string `xml:"tag"`
Art struct {
Poster string `xml:"poster"`
Fanart string `xml:"fanart"`
} `xml:"art"`
Actor []struct {
Name string `xml:"name"`
Role string `xml:"role"`
Type string `xml:"type"`
Sortorder string `xml:"sortorder"`
Thumb string `xml:"thumb"`
} `xml:"actor"`
ID string `xml:"id"`
Fileinfo struct {
Streamdetails struct {
Video struct {
Codec string `xml:"codec"`
Micodec string `xml:"micodec"`
Bitrate string `xml:"bitrate"`
Width string `xml:"width"`
Height string `xml:"height"`
Aspect string `xml:"aspect"`
Aspectratio string `xml:"aspectratio"`
Framerate string `xml:"framerate"`
Language string `xml:"language"`
Scantype string `xml:"scantype"`
Default string `xml:"default"`
Forced string `xml:"forced"`
Duration string `xml:"duration"`
Durationinseconds string `xml:"durationinseconds"`
} `xml:"video"`
Audio struct {
Codec string `xml:"codec"`
Micodec string `xml:"micodec"`
Bitrate string `xml:"bitrate"`
Language string `xml:"language"`
Scantype string `xml:"scantype"`
Channels string `xml:"channels"`
Samplingrate string `xml:"samplingrate"`
Default string `xml:"default"`
Forced string `xml:"forced"`
} `xml:"audio"`
} `xml:"streamdetails"`
} `xml:"fileinfo"`
}
var watcher *fsnotify.Watcher
func printTime(s string, args ...interface{}) {
fmt.Printf(time.Now().Format("15:04:05.0000")+" "+s+"\n", args...)
}
func updateNfoFile(nfoFilePath string){
// This function will open the specified NFO file and modify the MPAA rating if it starts with GB-
// In situations where this is the case the GB- will be replaced with UK:
// get just the filename
filePathParts := strings.Split(nfoFilePath,"/")
fileName := filePathParts[len(filePathParts)-1]
// open the XML file
xmlFile, err := os.Open(nfoFilePath)
// catch and display any errors
if err != nil {
log.Println(err)
} else {
// if the file was opened successfully then defer the closing of our xmlFile
// so that we can parse it later on
defer xmlFile.Close()
// read our opened xmlFile as a byte array.
byteValue, err := ioutil.ReadAll(xmlFile)
if err != nil{
fmt.Println("An error occurred whilst reading the file.")
fmt.Printf("%v\n",err)
}
if strings.ToLower(fileName) == "tvshow.nfo"{
// unmarshal the file into the struct
var tvshow Tvshow
xml.Unmarshal(byteValue, &tvshow)
// if the mpaa rating starts with GB- then...
if strings.HasPrefix(tvshow.Mpaa,"GB-"){
// note the original MPAA rateing
origMPAA := tvshow.Mpaa
// swap it for UK:
tvshow.Mpaa = strings.ReplaceAll(origMPAA,"GB-","UK:")
// marshall from XML back into a byte array
file, _ := xml.MarshalIndent(tvshow, "", " ")
// write out the XML and the standard header back to the file
_ = ioutil.WriteFile(nfoFilePath, []byte(Header + string(file)), 0644)
log.Printf(" The MPAA rating in the file %s was changed from %s to %s.\n",fileName,origMPAA,tvshow.Mpaa)
}
} else {
// unmarshal the file into the struct
var movie Movie
xml.Unmarshal(byteValue, &movie)
// if the mpaa rating starts with GB- then...
if strings.HasPrefix(movie.Mpaa,"GB-"){
// note the original MPAA rateing
origMPAA := movie.Mpaa
// swap it for UK:
movie.Mpaa = strings.ReplaceAll(origMPAA,"GB-","UK:")
// marshall from XML back into a byte array
file, _ := xml.MarshalIndent(movie, "", " ")
// write out the XML and the standard header back to the file
_ = ioutil.WriteFile(nfoFilePath, []byte(Header + string(file)), 0644)
log.Printf(" The MPAA rating in the file %s was changed from %s to %s.\n",fileName,origMPAA,movie.Mpaa)
}
}
}
}
// main
func main() {
var folders string
// get the folders to be monitored that were specified with the -d option
flag.StringVar(&folders, "d", "", "")
flag.Parse()
// if none were provided then exit with a suitable warning
if strings.Trim(folders," ") == ""{
log.Println("Please specify one or more folders to monitor by using the -d option.")
log.Println("To specify multiple paths please ensure each is seperated by a comma.")
os.Exit(0)
}
// split the directories into a slice
xDirs := strings.Split(folders,",")
// record the current time so we can identify how long th einitial scan takes
startTime := time.Now()
log.Println("The nfo-renamer is starting.....")
var doF = func(xpath string, xinfo fs.DirEntry, xerr error) error {
// first thing to do, check error. and decide what to do about it
if xerr != nil {
log.Printf("error [%v] at a path [%q]\n", xerr, xpath)
return xerr
}
// only process nfo files
if !xinfo.IsDir() {
fileExtension := strings.ToLower(filepath.Ext(xpath))
if fileExtension == ".nfo"{
nfoFilePath := fmt.Sprintf("%v/%v",filepath.Dir(xpath),xinfo.Name())
updateNfoFile(nfoFilePath)
}
}
return nil
}
// scan all files and folders under the starting path
for _,v := range xDirs{
log.Printf(" Processing the folder %s\n",v)
err := filepath.WalkDir(v, doF)
if err != nil {
log.Printf("error walking the path %q: %v\n", v, err)
}
// display the time taken for the initial scan to complete
elapsed := time.Since(startTime)
log.Printf(" Scanning for nfo files took %s\n\n", elapsed)
}
// creates a new file watcher
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
log.Println(" Creating the file system watchers")
//err := watchDir(watcher *fsnotify.Watcher, xDir, reload func()) error {
for _,v := range xDirs{
watcher.Add(v)
}
// starting at the root of the project, walk each file/directory searching for
// directories
//if err := filepath.Walk("/home/pwes/code/nfo-updated/test", watchDir); err != nil {
for _,v := range xDirs{
if err := filepath.Walk(v, watchDir); err != nil {
log.Println("ERROR", err)
}
}
log.Println(" File system watchers now created")
done := make(chan bool)
//
go func() {
var (
// Wait 100ms for new events; each new event resets the timer.
waitFor = 100 * time.Millisecond
// Keep track of the timers, as path → timer.
mu sync.Mutex
timers = make(map[string]*time.Timer)
// Callback we run.
printEvent = func(e fsnotify.Event) {
//printTime(e.String())
// get file path in lower case
filePath := strings.ToLower(e.Name)
// if the update was to an nfo file
if strings.HasSuffix(filePath,".nfo") {
updateNfoFile(e.Name)
}
// Don't need to remove the timer if you don't have a lot of files.
mu.Lock()
delete(timers, e.Name)
mu.Unlock()
}
)
for {
select {
// watch for events
case event := <-watcher.Events:
//log.Printf("EVENT! %#v\n", event)
// get file path in lower case
filePath := strings.ToLower(event.Name)
// if the update was to an nfo file
if strings.HasSuffix(filePath,".nfo") {
if event.Has(fsnotify.Create){
updateNfoFile(event.Name)
}
} else {
if event.Has(fsnotify.Create){
fileInfo, err := os.Stat(event.Name)
if err == nil{
//log.Printf("EVENT! %#v\n", event)
if fileInfo.IsDir(){
watcher.Add(event.Name)
}
}
}
}
// Get timer.
mu.Lock()
t, ok := timers[event.Name]
mu.Unlock()
// No timer yet, so create one.
if !ok {
t = time.AfterFunc(math.MaxInt64, func() { printEvent(event) })
t.Stop()
mu.Lock()
timers[event.Name] = t
mu.Unlock()
}
// Reset the timer for this path, so it will start from 100ms again.
t.Reset(waitFor)
// watch for errors
case err := <-watcher.Errors:
log.Println("ERROR", err)
}
}
}()
<-done
}
// watchDir gets run as a walk func, searching for directories to add watchers to
func watchDir(path string, fi os.FileInfo, err error) error {
// since fsnotify can watch all the files in a directory, watchers only need
// to be added to each nested directory
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}