-
Notifications
You must be signed in to change notification settings - Fork 0
/
epgdata.go
199 lines (167 loc) · 4.98 KB
/
epgdata.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
package epgdata
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/benjaminbear/xmltv-proxy/config"
)
const (
folderPersistence = "persistence"
filePersistence = "epgdata.bin"
fileEPGIncludeChannels = "channels.json"
)
var (
pathPersistence = filepath.Join(folderPersistence, filePersistence)
)
type ShowDay struct {
Date string
Shows []*Show
}
type Show struct {
ID string `json:"id"`
HexID string `json:"hexId"`
AssetID string `json:"assetId"`
SharingID string `json:"sharingId"`
TrackingID string `json:"trackingId"`
BroadcasterID string `json:"broadcasterId"`
BroadcasterName string `json:"broadcasterName"`
Country string `json:"country,omitempty"`
CurrentTopics string `json:"currentTopics,omitempty"`
EpisodeTitle string `json:"episodeTitle,omitempty"`
Genre string `json:"genre"`
Images Images `json:"images"`
IsHDTV bool `json:"isHDTV"`
IsLive bool `json:"isLive"`
IsNew bool `json:"isNew"`
IsPayTv bool `json:"isPayTv"`
IsStereo bool `json:"isStereo"`
IsTipOfTheDay bool `json:"isTipOfTheDay"`
IsTopTip bool `json:"isTopTip"`
LengthNetAndGross string `json:"lengthNetAndGross"`
RepeatHint string `json:"repeatHint,omitempty"`
Subline string `json:"subline,omitempty"`
Text string `json:"text,omitempty"`
IsOriginalText bool `json:"isOriginalText"`
Timeend dateTime `json:"timeend"`
Timestart dateTime `json:"timestart"`
Title string `json:"title"`
OriginalTitle string `json:"originalTitle,omitempty"`
SartID string `json:"sart_id"`
Year int `json:"year"`
Actors []*Actor `json:"actors,omitempty"`
Anchorman *StdListElement `json:"anchorman,omitempty"`
Director *StdListElement `json:"director,omitempty"`
StudioGuests *StdListElement `json:"studio_guests,omitempty"`
EpisodeNumber seriesNumber `json:"episodeNumber,omitempty"`
SeasonNumber seriesNumber `json:"seasonNumber,omitempty"`
Videos []struct {
Title string `json:"title"`
Catchline string `json:"catchline"`
BlockAds int `json:"blockAds"`
StillImage string `json:"stillImage"`
Video []struct {
Type string `json:"type"`
URL string `json:"url"`
Duration int `json:"duration"`
Width int `json:"width"`
Height int `json:"height"`
} `json:"video"`
} `json:"videos,omitempty"`
ThumbID string `json:"thumbId,omitempty"`
ThumbIDNumeric int `json:"thumbIdNumeric,omitempty"`
Fsk int `json:"fsk,omitempty"`
Preview string `json:"preview,omitempty"`
}
func (s *Show) HasCredits() bool {
if len(s.Actors) > 0 {
return true
}
if s.StudioGuests != nil {
if len(s.StudioGuests.List) > 0 {
return true
}
}
if s.Anchorman != nil {
if len(s.Anchorman.List) > 0 {
return true
}
}
if s.Director != nil {
if len(s.Director.List) > 0 {
return true
}
}
return false
}
func NewEPGData() []*Show {
epgData := make([]*Show, 0)
return epgData
}
func ReadEPGFile(path string) ([]*Show, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
epgData := NewEPGData()
err = json.Unmarshal(data, &epgData)
if err != nil {
return nil, err
}
return epgData, nil
}
func ReadEPGDay(date string) ([]*Show, error) {
showDay := make([]*Show, 0)
fileInfos, err := ioutil.ReadDir(filepath.Join(config.FolderEPGData, date))
if err != nil {
return nil, err
}
for _, fileInfo := range fileInfos {
shows, err := ReadEPGFile(filepath.Join(config.FolderEPGData, date, fileInfo.Name()))
if err != nil {
return nil, err
}
showDay = append(showDay, shows...)
}
return showDay, nil
}
func Save(data interface{}) (err error) {
if _, err = os.Stat(pathPersistence); !os.IsNotExist(err) {
err = os.Remove(pathPersistence)
if err != nil {
return err
}
}
if _, err = os.Stat(folderPersistence); os.IsNotExist(err) {
err = os.MkdirAll(folderPersistence, os.ModePerm)
if err != nil {
return err
}
}
f, err := os.Create(pathPersistence)
if err != nil {
return err
}
defer f.Close()
b, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return err
}
_, err = io.Copy(f, bytes.NewReader(b))
return err
}
func Load(showDays []*ShowDay) (err error) {
if _, err = os.Stat(pathPersistence); os.IsNotExist(err) {
return nil
}
fmt.Println("Loading persistent data from disk")
bytes, err := ioutil.ReadFile(pathPersistence)
if err != nil {
return err
}
err = json.Unmarshal(bytes, &showDays)
return err
}