-
Notifications
You must be signed in to change notification settings - Fork 15
/
data.go
217 lines (149 loc) · 3.65 KB
/
data.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
// Update : Update data from Schedules Direct and create the XMLTV file
func (sd *SD) Update(filename string) (err error) {
Config.File = strings.TrimSuffix(filename, filepath.Ext(filename))
_, err = ioutil.ReadFile(fmt.Sprintf("%s.yaml", Config.File))
if err != nil {
return
}
err = Config.Open()
if err != nil {
return
}
err = sd.Init()
if err != nil {
return
}
if len(sd.Token) == 0 {
err = sd.Login()
if err != nil {
return
}
}
sd.GetData()
runtime.GC()
err = CreateXMLTV(filename)
if err != nil {
ShowErr(err)
return
}
Cache.CleanUp()
runtime.GC()
return
}
// GetData : Get data from Schedules Direct
func (sd *SD) GetData() {
var err error
var wg sync.WaitGroup
var count = 0
err = Cache.Open()
if err != nil {
ShowErr(err)
return
}
Cache.Init()
// Channel list
sd.Status()
Cache.Channel = make(map[string]G2GCache)
var lineup []string
for _, l := range sd.Resp.Status.Lineups {
lineup = append(lineup, l.Lineup)
}
for _, id := range lineup {
sd.Req.Parameter = fmt.Sprintf("/%s", id)
sd.Req.Type = "GET"
sd.Lineups()
Cache.AddStations(&sd.Resp.Body, id)
}
// Schedule
showInfo("G2G", fmt.Sprintf("Download Schedule: %d Day(s)", Config.Options.Schedule))
var limit = 5000
var days = make([]string, 0)
var channels = make([]interface{}, 0)
for i := 0; i < Config.Options.Schedule; i++ {
var nextDay = time.Now().Add(time.Hour * time.Duration(24*i))
days = append(days, nextDay.Format("2006-01-02"))
}
for i, channel := range Config.Station {
count++
channel.Date = days
channels = append(channels, channel)
if count == limit || i == len(Config.Station)-1 {
sd.Req.Data, err = json.Marshal(channels)
if err != nil {
ShowErr(err)
return
}
sd.Schedule()
wg.Add(1)
go func() {
Cache.AddSchedule(&sd.Resp.Body)
wg.Done()
}()
count = 0
}
}
wg.Wait()
// Program and Metadata
count = 0
sd.Req.Data = []byte{}
var types = []string{"programs", "metadata"}
var programIds = Cache.GetRequiredProgramIDs()
var allIDs = Cache.GetAllProgramIDs()
var programs = make([]interface{}, 0)
showInfo("G2G", fmt.Sprintf("Download Program Informations: New: %d / Cached: %d", len(programIds), len(allIDs)-len(programIds)))
for _, t := range types {
switch t {
case "metadata":
sd.Req.URL = fmt.Sprintf("%smetadata/programs", sd.BaseURL)
sd.Req.Call = "metadata"
programIds = Cache.GetRequiredMetaIDs()
limit = 500
showInfo("G2G", fmt.Sprintf("Download missing Metadata: %d ", len(programIds)))
case "programs":
sd.Req.URL = fmt.Sprintf("%sprograms", sd.BaseURL)
sd.Req.Call = "programs"
limit = 5000
}
for i, p := range programIds {
count++
programs = append(programs, p)
if count == limit || i == len(programIds)-1 {
sd.Req.Data, err = json.Marshal(programs)
if err != nil {
ShowErr(err)
return
}
err := sd.Program()
if err != nil {
ShowErr(err)
}
wg.Add(1)
switch t {
case "metadata":
go Cache.AddMetadata(&sd.Resp.Body, &wg)
case "programs":
go Cache.AddProgram(&sd.Resp.Body, &wg)
}
count = 0
programs = make([]interface{}, 0)
wg.Wait()
}
}
}
err = Cache.Save()
if err != nil {
ShowErr(err)
return
}
}