forked from Threadfin/Threadfin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxepg.go
More file actions
1484 lines (1119 loc) · 38.8 KB
/
xepg.go
File metadata and controls
1484 lines (1119 loc) · 38.8 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package src
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"os"
"path"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"time"
"unicode"
"threadfin/src/internal/imgcache"
_ "time/tzdata"
)
// Provider XMLTV Datei überprüfen
func checkXMLCompatibility(id string, body []byte) (err error) {
var xmltv XMLTV
var compatibility = make(map[string]int)
err = xml.Unmarshal(body, &xmltv)
if err != nil {
return
}
compatibility["xmltv.channels"] = len(xmltv.Channel)
compatibility["xmltv.programs"] = len(xmltv.Program)
setProviderCompatibility(id, "xmltv", compatibility)
return
}
var buildXEPGCount int
// XEPG Daten erstellen
func buildXEPG(background bool) {
xepgMutex.Lock()
defer func() {
xepgMutex.Unlock()
}()
if System.ScanInProgress == 1 {
return
}
System.ScanInProgress = 1
var err error
Data.Cache.Images, err = imgcache.New(System.Folder.ImagesCache, fmt.Sprintf("%s://%s/images/", System.ServerProtocol.WEB, System.Domain), Settings.CacheImages)
if err != nil {
ShowError(err, 0)
}
if Settings.EpgSource == "XEPG" {
switch background {
case true:
go func() {
createXEPGMapping()
createXEPGDatabase()
mapping()
cleanupXEPG()
createXMLTVFile()
createM3UFile()
showInfo("XEPG:" + fmt.Sprintf("Ready to use"))
if Settings.CacheImages && System.ImageCachingInProgress == 0 {
go func() {
systemMutex.Lock()
System.ImageCachingInProgress = 1
systemMutex.Unlock()
showInfo(fmt.Sprintf("Image Caching:Images are cached (%d)", len(Data.Cache.Images.Queue)))
Data.Cache.Images.Image.Caching()
Data.Cache.Images.Image.Remove()
showInfo("Image Caching:Done")
createXMLTVFile()
createM3UFile()
systemMutex.Lock()
System.ImageCachingInProgress = 0
systemMutex.Unlock()
}()
}
systemMutex.Lock()
System.ScanInProgress = 0
systemMutex.Unlock()
// Cache löschen
/*
Data.Cache.XMLTV = make(map[string]XMLTV)
Data.Cache.XMLTV = nil
*/
runtime.GC()
}()
case false:
createXEPGMapping()
createXEPGDatabase()
mapping()
cleanupXEPG()
createXMLTVFile()
createM3UFile()
go func() {
if Settings.CacheImages && System.ImageCachingInProgress == 0 {
go func() {
systemMutex.Lock()
System.ImageCachingInProgress = 1
systemMutex.Unlock()
showInfo(fmt.Sprintf("Image Caching:Images are cached (%d)", len(Data.Cache.Images.Queue)))
Data.Cache.Images.Image.Caching()
Data.Cache.Images.Image.Remove()
showInfo("Image Caching:Done")
createXMLTVFile()
createM3UFile()
systemMutex.Lock()
System.ImageCachingInProgress = 0
systemMutex.Unlock()
}()
}
showInfo("XEPG:" + fmt.Sprintf("Ready to use"))
systemMutex.Lock()
System.ScanInProgress = 0
systemMutex.Unlock()
// Cache löschen
//Data.Cache.XMLTV = make(map[string]XMLTV)
//Data.Cache.XMLTV = nil
runtime.GC()
}()
}
} else {
getLineup()
System.ScanInProgress = 0
}
}
// XEPG Daten aktualisieren
func updateXEPG(background bool) {
if System.ScanInProgress == 1 {
return
}
System.ScanInProgress = 1
if Settings.EpgSource == "XEPG" {
switch background {
case false:
createXEPGDatabase()
mapping()
cleanupXEPG()
go func() {
createXMLTVFile()
createM3UFile()
showInfo("XEPG:" + fmt.Sprintf("Ready to use"))
System.ScanInProgress = 0
}()
case true:
System.ScanInProgress = 0
}
} else {
System.ScanInProgress = 0
}
// Cache löschen
//Data.Cache.XMLTV = nil //make(map[string]XMLTV)
//Data.Cache.XMLTV = make(map[string]XMLTV)
return
}
// Mapping Menü für die XMLTV Dateien erstellen
func createXEPGMapping() {
Data.XMLTV.Files = getLocalProviderFiles("xmltv")
Data.XMLTV.Mapping = make(map[string]interface{})
var tmpMap = make(map[string]interface{})
var friendlyDisplayName = func(channel Channel) (displayName string) {
var dn = channel.DisplayName
if len(dn) > 0 {
switch len(dn) {
case 1:
displayName = dn[0].Value
default:
displayName = fmt.Sprintf("%s (%s)", dn[0].Value, dn[1].Value)
}
}
return
}
if len(Data.XMLTV.Files) > 0 {
for i := len(Data.XMLTV.Files) - 1; i >= 0; i-- {
var file = Data.XMLTV.Files[i]
var err error
var fileID = strings.TrimSuffix(getFilenameFromPath(file), path.Ext(getFilenameFromPath(file)))
showInfo("XEPG:" + "Parse XMLTV file: " + getProviderParameter(fileID, "xmltv", "name"))
//xmltv, err = getLocalXMLTV(file)
var xmltv XMLTV
err = getLocalXMLTV(file, &xmltv)
if err != nil {
Data.XMLTV.Files = append(Data.XMLTV.Files, Data.XMLTV.Files[i+1:]...)
var errMsg = err.Error()
err = errors.New(getProviderParameter(fileID, "xmltv", "name") + ": " + errMsg)
ShowError(err, 000)
}
// XML Parsen (Provider Datei)
if err == nil {
var imgc = Data.Cache.Images
// Daten aus der XML Datei in eine temporäre Map schreiben
var xmltvMap = make(map[string]interface{})
for _, c := range xmltv.Channel {
var channel = make(map[string]interface{})
channel["id"] = c.ID
channel["display-name"] = friendlyDisplayName(*c)
channel["icon"] = imgc.Image.GetURL(c.Icon.Src, Settings.HttpThreadfinDomain, Settings.Port, Settings.ForceHttps, Settings.HttpsPort, Settings.HttpsThreadfinDomain)
channel["active"] = c.Active
xmltvMap[c.ID] = channel
}
tmpMap[getFilenameFromPath(file)] = xmltvMap
Data.XMLTV.Mapping[getFilenameFromPath(file)] = xmltvMap
}
}
Data.XMLTV.Mapping = tmpMap
tmpMap = make(map[string]interface{})
} else {
if System.ConfigurationWizard == false {
showWarning(1007)
}
}
// Auswahl für den Dummy erstellen
var dummy = make(map[string]interface{})
var times = []string{"30", "60", "90", "120", "180", "240", "360", "PPV"}
for _, i := range times {
var dummyChannel = make(map[string]string)
if i == "PPV" {
dummyChannel["display-name"] = "PPV Event"
dummyChannel["id"] = "PPV"
} else {
dummyChannel["display-name"] = i + " Minutes"
dummyChannel["id"] = i + "_Minutes"
}
dummyChannel["icon"] = ""
dummy[dummyChannel["id"]] = dummyChannel
}
Data.XMLTV.Mapping["Threadfin Dummy"] = dummy
return
}
// XEPG Datenbank erstellen / aktualisieren
func createXEPGDatabase() (err error) {
var allChannelNumbers = make([]float64, 0, System.UnfilteredChannelLimit)
Data.Cache.Streams.Active = make([]string, 0, System.UnfilteredChannelLimit)
Data.XEPG.Channels = make(map[string]interface{}, System.UnfilteredChannelLimit)
Data.Cache.Streams.Active = make([]string, 0, System.UnfilteredChannelLimit)
Settings = SettingsStruct{}
Data.XEPG.Channels, err = loadJSONFileToMap(System.File.XEPG)
if err != nil {
ShowError(err, 1004)
return err
}
settings, err := loadJSONFileToMap(System.File.Settings)
if err != nil || len(settings) == 0 {
return
}
settings_json, _ := json.Marshal(settings)
json.Unmarshal(settings_json, &Settings)
var createNewID = func() (xepg string) {
var firstID = 0 //len(Data.XEPG.Channels)
newXEPGID:
if _, ok := Data.XEPG.Channels["x-ID."+strconv.FormatInt(int64(firstID), 10)]; ok {
firstID++
goto newXEPGID
}
xepg = "x-ID." + strconv.FormatInt(int64(firstID), 10)
return
}
var getFreeChannelNumber = func(startingNumber float64) (xChannelID string) {
sort.Float64s(allChannelNumbers)
for {
if indexOfFloat64(startingNumber, allChannelNumbers) == -1 {
xChannelID = fmt.Sprintf("%g", startingNumber)
allChannelNumbers = append(allChannelNumbers, startingNumber)
return
}
startingNumber++
}
}
showInfo("XEPG:" + "Update database")
// Kanal mit fehlenden Kanalnummern löschen. Delete channel with missing channel numbers
for id, dxc := range Data.XEPG.Channels {
var xepgChannel XEPGChannelStruct
err = json.Unmarshal([]byte(mapToJSON(dxc)), &xepgChannel)
if err != nil {
return
}
if len(xepgChannel.XChannelID) == 0 {
delete(Data.XEPG.Channels, id)
}
if xChannelID, err := strconv.ParseFloat(xepgChannel.XChannelID, 64); err == nil {
allChannelNumbers = append(allChannelNumbers, xChannelID)
}
}
// Make a map of the db channels based on their previously downloaded attributes -- filename, group, title, etc
var xepgChannelsValuesMap = make(map[string]XEPGChannelStruct, System.UnfilteredChannelLimit)
for _, v := range Data.XEPG.Channels {
var channel XEPGChannelStruct
err = json.Unmarshal([]byte(mapToJSON(v)), &channel)
if err != nil {
return
}
if channel.TvgName == "" {
channel.TvgName = channel.Name
}
channelHash := channel.TvgName + channel.FileM3UID
if channel.Live {
hash := md5.Sum([]byte(channel.URL + channel.FileM3UID))
channelHash = hex.EncodeToString(hash[:])
}
xepgChannelsValuesMap[channelHash] = channel
}
for _, dsa := range Data.Streams.Active {
var channelExists = false // Entscheidet ob ein Kanal neu zu Datenbank hinzugefügt werden soll. Decides whether a channel should be added to the database
var channelHasUUID = false // Überprüft, ob der Kanal (Stream) eindeutige ID's besitzt. Checks whether the channel (stream) has unique IDs
var currentXEPGID string // Aktuelle Datenbank ID (XEPG). Wird verwendet, um den Kanal in der Datenbank mit dem Stream der M3u zu aktualisieren. Current database ID (XEPG) Used to update the channel in the database with the stream of the M3u
var currentChannelNumber string
var m3uChannel M3UChannelStructXEPG
err = json.Unmarshal([]byte(mapToJSON(dsa)), &m3uChannel)
if err != nil {
return
}
if m3uChannel.TvgName == "" {
m3uChannel.TvgName = m3uChannel.Name
}
// Try to find the channel based on matching all known values. If that fails, then move to full channel scan
m3uChannelHash := m3uChannel.TvgName + m3uChannel.FileM3UID
if m3uChannel.LiveEvent == "true" {
hash := md5.Sum([]byte(m3uChannel.URL + m3uChannel.FileM3UID))
m3uChannelHash = hex.EncodeToString(hash[:])
}
Data.Cache.Streams.Active = append(Data.Cache.Streams.Active, m3uChannelHash)
if val, ok := xepgChannelsValuesMap[m3uChannelHash]; ok {
channelExists = true
currentXEPGID = val.XEPG
currentChannelNumber = val.TvgChno
if len(m3uChannel.UUIDValue) > 0 {
channelHasUUID = true
}
} else {
// XEPG Datenbank durchlaufen um nach dem Kanal zu suchen. Run through the XEPG database to search for the channel (full scan)
for _, dxc := range xepgChannelsValuesMap {
if m3uChannel.FileM3UID == dxc.FileM3UID && !isInInactiveList(dxc.URL) {
dxc.FileM3UID = m3uChannel.FileM3UID
dxc.FileM3UName = m3uChannel.FileM3UName
// Vergleichen des Streams anhand einer UUID in der M3U mit dem Kanal in der Databank. Compare the stream using a UUID in the M3U with the channel in the database
if len(dxc.UUIDValue) > 0 && len(m3uChannel.UUIDValue) > 0 {
if dxc.UUIDValue == m3uChannel.UUIDValue {
channelExists = true
channelHasUUID = true
currentXEPGID = dxc.XEPG
currentChannelNumber = dxc.TvgChno
break
}
}
}
}
}
switch channelExists {
case true:
// Bereits vorhandener Kanal
var xepgChannel XEPGChannelStruct
err = json.Unmarshal([]byte(mapToJSON(Data.XEPG.Channels[currentXEPGID])), &xepgChannel)
if err != nil {
return
}
if xepgChannel.Live && xepgChannel.ChannelUniqueID == m3uChannelHash {
if xepgChannel.TvgName == "" {
xepgChannel.TvgName = xepgChannel.Name
}
xepgChannel.XChannelID = currentChannelNumber
xepgChannel.TvgChno = currentChannelNumber
// Streaming URL aktualisieren
xepgChannel.URL = m3uChannel.URL
if m3uChannel.LiveEvent == "true" {
xepgChannel.Live = true
}
// Kanalname aktualisieren, nur mit Kanal ID's möglich
if channelHasUUID {
programData, _ := getProgramData(xepgChannel)
if xepgChannel.XUpdateChannelName || strings.Contains(xepgChannel.TvgID, "threadfin-") || (m3uChannel.LiveEvent == "true" && len(programData.Program) <= 3) {
xepgChannel.XName = m3uChannel.Name
}
}
// Kanallogo aktualisieren. Wird bei vorhandenem Logo in der XMLTV Datei wieder überschrieben
if xepgChannel.XUpdateChannelIcon {
var imgc = Data.Cache.Images
xepgChannel.TvgLogo = imgc.Image.GetURL(m3uChannel.TvgLogo, Settings.HttpThreadfinDomain, Settings.Port, Settings.ForceHttps, Settings.HttpsPort, Settings.HttpsThreadfinDomain)
}
}
Data.XEPG.Channels[currentXEPGID] = xepgChannel
case false:
// Neuer Kanal
var firstFreeNumber float64 = Settings.MappingFirstChannel
// Check channel start number from Group Filter
filters := []FilterStruct{}
for _, filter := range Settings.Filter {
filter_json, _ := json.Marshal(filter)
f := FilterStruct{}
json.Unmarshal(filter_json, &f)
filters = append(filters, f)
}
for _, filter := range filters {
if m3uChannel.GroupTitle == filter.Filter {
start_num, _ := strconv.ParseFloat(filter.StartingNumber, 64)
firstFreeNumber = start_num
}
}
var xepg = createNewID()
var xChannelID string
if m3uChannel.TvgChno == "" {
xChannelID = getFreeChannelNumber(firstFreeNumber)
} else {
xChannelID = m3uChannel.TvgChno
}
var newChannel XEPGChannelStruct
newChannel.FileM3UID = m3uChannel.FileM3UID
newChannel.FileM3UName = m3uChannel.FileM3UName
newChannel.FileM3UPath = m3uChannel.FileM3UPath
newChannel.Values = m3uChannel.Values
newChannel.GroupTitle = m3uChannel.GroupTitle
newChannel.Name = m3uChannel.Name
newChannel.TvgID = m3uChannel.TvgID
newChannel.TvgLogo = m3uChannel.TvgLogo
newChannel.TvgName = m3uChannel.TvgName
newChannel.URL = m3uChannel.URL
newChannel.Live, _ = strconv.ParseBool(m3uChannel.LiveEvent)
for file, xmltvChannels := range Data.XMLTV.Mapping {
channelsMap, ok := xmltvChannels.(map[string]interface{})
if !ok {
continue
}
if channel, ok := channelsMap[m3uChannel.TvgID]; ok {
filters := []FilterStruct{}
for _, filter := range Settings.Filter {
filter_json, _ := json.Marshal(filter)
f := FilterStruct{}
json.Unmarshal(filter_json, &f)
filters = append(filters, f)
}
for _, filter := range filters {
if newChannel.GroupTitle == filter.Filter {
category := &Category{}
category.Value = filter.Category
category.Lang = "en"
newChannel.XCategory = filter.Category
}
}
chmap, okk := channel.(map[string]interface{})
if !okk {
continue
}
if channelID, ok := chmap["id"].(string); ok {
newChannel.XmltvFile = file
newChannel.XMapping = channelID
newChannel.XActive = true
// Falls in der XMLTV Datei ein Logo existiert, wird dieses verwendet. Falls nicht, dann das Logo aus der M3U Datei
if icon, ok := chmap["icon"].(string); ok {
if len(icon) > 0 {
newChannel.TvgLogo = icon
}
}
break
}
}
}
programData, _ := getProgramData(newChannel)
if newChannel.Live && len(programData.Program) <= 3 {
newChannel.XmltvFile = "Threadfin Dummy"
newChannel.XMapping = "PPV"
newChannel.XActive = true
}
if len(m3uChannel.UUIDKey) > 0 {
newChannel.UUIDKey = m3uChannel.UUIDKey
newChannel.UUIDValue = m3uChannel.UUIDValue
} else {
newChannel.UUIDKey = ""
newChannel.UUIDValue = ""
}
newChannel.XName = m3uChannel.Name
newChannel.XGroupTitle = m3uChannel.GroupTitle
newChannel.XEPG = xepg
newChannel.TvgChno = xChannelID
newChannel.XChannelID = xChannelID
newChannel.ChannelUniqueID = m3uChannelHash
Data.XEPG.Channels[xepg] = newChannel
xepgChannelsValuesMap[m3uChannelHash] = newChannel
}
}
showInfo("XEPG:" + "Save DB file")
err = saveMapToJSONFile(System.File.XEPG, Data.XEPG.Channels)
if err != nil {
return
}
return
}
// Kanäle automatisch zuordnen und das Mapping überprüfen
func mapping() (err error) {
showInfo("XEPG:" + "Map channels")
for xepg, dxc := range Data.XEPG.Channels {
var xepgChannel XEPGChannelStruct
err = json.Unmarshal([]byte(mapToJSON(dxc)), &xepgChannel)
if err != nil {
return
}
if xepgChannel.TvgName == "" {
xepgChannel.TvgName = xepgChannel.Name
}
if (xepgChannel.XBackupChannel1 != "" && xepgChannel.XBackupChannel1 != "-") || (xepgChannel.XBackupChannel2 != "" && xepgChannel.XBackupChannel2 != "-") || (xepgChannel.XBackupChannel3 != "" && xepgChannel.XBackupChannel3 != "-") {
for _, stream := range Data.Streams.Active {
var m3uChannel M3UChannelStructXEPG
err = json.Unmarshal([]byte(mapToJSON(stream)), &m3uChannel)
if err != nil {
return err
}
if m3uChannel.TvgName == "" {
m3uChannel.TvgName = m3uChannel.Name
}
backup_channel1 := strings.Trim(xepgChannel.XBackupChannel1, " ")
if m3uChannel.TvgName == backup_channel1 {
xepgChannel.BackupChannel1 = &BackupStream{PlaylistID: m3uChannel.FileM3UID, URL: m3uChannel.URL}
}
backup_channel2 := strings.Trim(xepgChannel.XBackupChannel2, " ")
if m3uChannel.TvgName == backup_channel2 {
xepgChannel.BackupChannel2 = &BackupStream{PlaylistID: m3uChannel.FileM3UID, URL: m3uChannel.URL}
}
backup_channel3 := strings.Trim(xepgChannel.XBackupChannel3, " ")
if m3uChannel.TvgName == backup_channel3 {
xepgChannel.BackupChannel3 = &BackupStream{PlaylistID: m3uChannel.FileM3UID, URL: m3uChannel.URL}
}
}
}
// Automatische Mapping für neue Kanäle. Wird nur ausgeführt, wenn der Kanal deaktiviert ist und keine XMLTV Datei und kein XMLTV Kanal zugeordnet ist.
if !xepgChannel.XActive {
// Werte kann "-" sein, deswegen len < 1
if len(xepgChannel.XmltvFile) < 1 {
var tvgID = xepgChannel.TvgID
xepgChannel.XmltvFile = "-"
xepgChannel.XMapping = "-"
Data.XEPG.Channels[xepg] = xepgChannel
for file, xmltvChannels := range Data.XMLTV.Mapping {
channelsMap, ok := xmltvChannels.(map[string]interface{})
if !ok {
continue
}
if channel, ok := channelsMap[tvgID]; ok {
filters := []FilterStruct{}
for _, filter := range Settings.Filter {
filter_json, _ := json.Marshal(filter)
f := FilterStruct{}
json.Unmarshal(filter_json, &f)
filters = append(filters, f)
}
for _, filter := range filters {
if xepgChannel.GroupTitle == filter.Filter {
category := &Category{}
category.Value = filter.Category
category.Lang = "en"
xepgChannel.XCategory = filter.Category
}
}
chmap, okk := channel.(map[string]interface{})
if !okk {
continue
}
if channelID, ok := chmap["id"].(string); ok {
xepgChannel.XmltvFile = file
xepgChannel.XMapping = channelID
xepgChannel.XActive = true
// Falls in der XMLTV Datei ein Logo existiert, wird dieses verwendet. Falls nicht, dann das Logo aus der M3U Datei
if icon, ok := chmap["icon"].(string); ok {
if len(icon) > 0 {
xepgChannel.TvgLogo = icon
}
}
Data.XEPG.Channels[xepg] = xepgChannel
break
}
}
}
}
}
// Überprüfen, ob die zugeordneten XMLTV Dateien und Kanäle noch existieren.
if xepgChannel.XActive && !xepgChannel.XHideChannel {
var mapping = xepgChannel.XMapping
var file = xepgChannel.XmltvFile
if file != "Threadfin Dummy" && !xepgChannel.Live {
if value, ok := Data.XMLTV.Mapping[file].(map[string]interface{}); ok {
if channel, ok := value[mapping].(map[string]interface{}); ok {
filters := []FilterStruct{}
for _, filter := range Settings.Filter {
filter_json, _ := json.Marshal(filter)
f := FilterStruct{}
json.Unmarshal(filter_json, &f)
filters = append(filters, f)
}
for _, filter := range filters {
if xepgChannel.GroupTitle == filter.Filter {
category := &Category{}
category.Value = filter.Category
category.Lang = "en"
if xepgChannel.XCategory == "" {
xepgChannel.XCategory = filter.Category
}
}
}
// Kanallogo aktualisieren
if logo, ok := channel["icon"].(string); ok {
if xepgChannel.XUpdateChannelIcon && len(logo) > 0 {
var imgc = Data.Cache.Images
xepgChannel.TvgLogo = imgc.Image.GetURL(logo, Settings.HttpThreadfinDomain, Settings.Port, Settings.ForceHttps, Settings.HttpsPort, Settings.HttpsThreadfinDomain)
}
}
}
}
} else {
// Loop through dummy channels and assign the filter info
filters := []FilterStruct{}
for _, filter := range Settings.Filter {
filter_json, _ := json.Marshal(filter)
f := FilterStruct{}
json.Unmarshal(filter_json, &f)
filters = append(filters, f)
}
for _, filter := range filters {
if xepgChannel.GroupTitle == filter.Filter {
category := &Category{}
category.Value = filter.Category
category.Lang = "en"
if xepgChannel.XCategory == "" {
xepgChannel.XCategory = filter.Category
}
}
}
}
if len(xepgChannel.XmltvFile) == 0 {
xepgChannel.XmltvFile = "-"
xepgChannel.XActive = true
}
if len(xepgChannel.XMapping) == 0 {
xepgChannel.XMapping = "-"
xepgChannel.XActive = true
}
Data.XEPG.Channels[xepg] = xepgChannel
}
}
err = saveMapToJSONFile(System.File.XEPG, Data.XEPG.Channels)
if err != nil {
return
}
return
}
// XMLTV Datei erstellen
func createXMLTVFile() (err error) {
// Image Cache
// 4edd81ab7c368208cc6448b615051b37.jpg
var imgc = Data.Cache.Images
Data.Cache.ImagesFiles = []string{}
Data.Cache.ImagesURLS = []string{}
Data.Cache.ImagesCache = []string{}
files, err := os.ReadDir(System.Folder.ImagesCache)
if err == nil {
for _, file := range files {
if indexOfString(file.Name(), Data.Cache.ImagesCache) == -1 {
Data.Cache.ImagesCache = append(Data.Cache.ImagesCache, file.Name())
}
}
}
if len(Data.XMLTV.Files) == 0 && len(Data.Streams.Active) == 0 {
Data.XEPG.Channels = make(map[string]interface{})
return
}
showInfo("XEPG:" + fmt.Sprintf("Create XMLTV file (%s)", System.File.XML))
var xepgXML XMLTV
xepgXML.Generator = System.Name
if System.Branch == "main" {
xepgXML.Source = fmt.Sprintf("%s - %s", System.Name, System.Version)
} else {
xepgXML.Source = fmt.Sprintf("%s - %s.%s", System.Name, System.Version, System.Build)
}
var tmpProgram = &XMLTV{}
for _, dxc := range Data.XEPG.Channels {
var xepgChannel XEPGChannelStruct
err := json.Unmarshal([]byte(mapToJSON(dxc)), &xepgChannel)
if err == nil {
if xepgChannel.TvgName == "" {
xepgChannel.TvgName = xepgChannel.Name
}
if xepgChannel.XName == "" {
xepgChannel.XName = xepgChannel.TvgName
}
if xepgChannel.XActive && !xepgChannel.XHideChannel {
if (Settings.XepgReplaceChannelTitle && xepgChannel.XMapping == "PPV") || xepgChannel.XName != "" {
// Kanäle
var channel Channel
channel.ID = xepgChannel.XChannelID
channel.Icon = Icon{Src: imgc.Image.GetURL(xepgChannel.TvgLogo, Settings.HttpThreadfinDomain, Settings.Port, Settings.ForceHttps, Settings.HttpsPort, Settings.HttpsThreadfinDomain)}
channel.DisplayName = append(channel.DisplayName, DisplayName{Value: xepgChannel.XName})
channel.Active = xepgChannel.XActive
channel.Live = xepgChannel.Live
xepgXML.Channel = append(xepgXML.Channel, &channel)
}
// Programme
*tmpProgram, err = getProgramData(xepgChannel)
if err == nil {
xepgXML.Program = append(xepgXML.Program, tmpProgram.Program...)
}
}
} else {
showDebug("XEPG:"+fmt.Sprintf("Error: %s", err), 3)
}
}
var content, _ = xml.MarshalIndent(xepgXML, " ", " ")
var xmlOutput = []byte(xml.Header + string(content))
writeByteToFile(System.File.XML, xmlOutput)
showInfo("XEPG:" + fmt.Sprintf("Compress XMLTV file (%s)", System.Compressed.GZxml))
err = compressGZIP(&xmlOutput, System.Compressed.GZxml)
xepgXML = XMLTV{}
return
}
// Programmdaten erstellen (createXMLTVFile)
func getProgramData(xepgChannel XEPGChannelStruct) (xepgXML XMLTV, err error) {
var xmltvFile = System.Folder.Data + xepgChannel.XmltvFile
var channelID = xepgChannel.XMapping
var xmltv XMLTV
if strings.Contains(xmltvFile, "Threadfin Dummy") {
xmltv = createDummyProgram(xepgChannel)
} else {
if xepgChannel.XmltvFile != "" {
err = getLocalXMLTV(xmltvFile, &xmltv)
if err != nil {
return
}
}
}
for _, xmltvProgram := range xmltv.Program {
if xmltvProgram.Channel == channelID {
var program = &Program{}
// Channel ID
program.Channel = xepgChannel.XChannelID
program.Start = xmltvProgram.Start
program.Stop = xmltvProgram.Stop
// Title
if len(xmltvProgram.Title) > 0 {
if !Settings.EnableNonAscii {
xmltvProgram.Title[0].Value = strings.TrimSpace(strings.Map(func(r rune) rune {
if r > unicode.MaxASCII {
return -1
}
return r
}, xmltvProgram.Title[0].Value))
}
program.Title = xmltvProgram.Title
}
filters := []FilterStruct{}
for _, filter := range Settings.Filter {
filter_json, _ := json.Marshal(filter)
f := FilterStruct{}
json.Unmarshal(filter_json, &f)
filters = append(filters, f)
}
// Category (Kategorie)
getCategory(program, xmltvProgram, xepgChannel, filters)
// Sub-Title
program.SubTitle = xmltvProgram.SubTitle
// Description
program.Desc = xmltvProgram.Desc
// Credits : (Credits)
program.Credits = xmltvProgram.Credits
// Rating (Bewertung)
program.Rating = xmltvProgram.Rating
// StarRating (Bewertung / Kritiken)
program.StarRating = xmltvProgram.StarRating