forked from XiovV/Golty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
195 lines (172 loc) · 5.08 KB
/
helpers.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
log "github.com/sirupsen/logrus"
)
func GetAll(target string) ([]DownloadTarget, error) {
var db []DownloadTarget
var databaseName string
if target == "channels" {
databaseName = "channels.json"
} else if target == "playlists" {
databaseName = "playlists.json"
}
log.Info("getting all channels from channels.json")
jsonFile, err := os.Open(CONFIG_ROOT + databaseName)
if err != nil {
log.Error("From GetAll()", err)
return []DownloadTarget{}, fmt.Errorf("From GetAll(): %v", err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &db)
if err != nil {
log.Error("From GetAll()", err)
return []DownloadTarget{}, fmt.Errorf("From GetAll(): %v", err)
}
log.Info("successfully read all channels")
log.Info(db)
return reverseTargets(db), nil
}
func getAllTargets(target string) ([]DownloadTarget, string) {
var allInDb []DownloadTarget
var targetType string
if target == "channels" {
log.Info("checking for all channels")
allInDb, _ = GetAll("channels")
targetType = "Channel"
return allInDb, targetType
} else if target == "playlists" {
log.Info("checking for all playlists")
allInDb, _ = GetAll("playlists")
targetType = "Playlist"
return allInDb, targetType
}
return nil, ""
}
func checkAllTargets(targets []DownloadTarget, targetType string) (Response, error) {
var foundFor []string
var preferredExtension string
var err error
for _, item := range targets {
target := DownloadTarget{URL: item.URL, Type: targetType}
target, err = target.GetFromDatabase()
if err != nil {
return Response{Type: "Error", Key: "GETTING_FROM_DATABASE_ERROR", Message: "There was an error getting the channel from database" + err.Error()}, fmt.Errorf("CheckAll: %s", err)
}
newVideoUploaded, videoId, err := target.CheckNow()
if err != nil {
return Response{Type: "Error", Key: "CHECKING_ERROR", Message: "There was an error checking for new uploads" + err.Error()}, fmt.Errorf("CheckAll: %s", err)
}
if newVideoUploaded == true {
log.Info("new video detected for: ", item.URL)
foundFor = append(foundFor, item.URL)
if target.DownloadMode == "Audio Only" {
preferredExtension = target.PreferredExtensionForAudio
} else if target.DownloadMode == "Video And Audio" {
preferredExtension = target.PreferredExtensionForVideo
}
go target.Download("best", preferredExtension, false)
target.UpdateLatestDownloaded(videoId)
} else {
log.Info("no new videos found for: ", item.URL)
}
}
if len(foundFor) == 0 {
return Response{Type: "Success", Key: "NO_NEW_VIDEOS", Message: "No new videos found."}, nil
}
return Response{Type: "Success", Key: "NEW_VIDEOS", Message: "New videos detected for: " + strings.Join(foundFor, ",")}, nil
}
func CheckAll(target string) (Response, error) {
return checkAllTargets(getAllTargets(target))
}
func (target DownloadTarget) CheckNow() (bool, string, error) {
var allInDb []DownloadTarget
var err error
log.Info("checking for new videos")
if target.Type == "Channel" {
allInDb, err = GetAll("channels")
if err != nil {
return false, "", fmt.Errorf("From p.CheckNow(): %v", err)
}
} else if target.Type == "Playlist" {
allInDb, err = GetAll("playlists")
if err != nil {
return false, "", fmt.Errorf("From p.CheckNow(): %v", err)
}
}
targets, err := target.GetFromDatabase()
if err != nil {
return false, "", fmt.Errorf("CheckNow: %s", err)
}
targetURL := target.URL
targetMetadata, err := targets.GetMetadata()
if err != nil {
return false, "", fmt.Errorf("CheckNow: %s", err)
}
err = target.UpdateLastChecked()
if err != nil {
return false, "", fmt.Errorf("CheckNow: %s", err)
}
for _, target := range allInDb {
if target.URL == targetURL {
if target.LatestDownloaded == targetMetadata.ID {
log.Info("no new videos found for: ", targetURL)
return false, "", nil
} else {
log.Info("new video found for: ", targetURL)
return true, targetMetadata.ID, nil
}
}
}
log.Error("CheckNow: Something went terribly wrong")
return false, "", fmt.Errorf("CheckNow: something went wrong ")
}
func RemoveAtIndex(s []DownloadTarget, index int) []DownloadTarget {
return append(s[:index], s[index+1:]...)
}
func ReturnResponse(w http.ResponseWriter, res Response) {
log.Info("returning response: ", res)
json.NewEncoder(w).Encode(res)
}
func Log(err error) error {
if err != nil {
log.Error(err)
return err
}
return nil
}
func CreateDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
}
func createDir(dir string) {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
f, _ := os.Create(dir)
defer f.Close()
f.WriteString("[]")
f.Sync()
}
}
func reverseTargets(s []DownloadTarget) []DownloadTarget {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func reverseVideos(s []DownloadVideoPayload) []DownloadVideoPayload {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}