-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
holodex.go
216 lines (174 loc) · 5.47 KB
/
holodex.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
package main
import (
"encoding/json"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/lib/pq"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
func QueueUpcomingStreams(app *Application) {
orgsList := strings.TrimSpace(os.Getenv("HOLODEX_ORGS"))
// skip if no args were selected in the .env file
if len(orgsList) <= 0 {
return
}
orgs := strings.Split(orgsList, ",")
for _, org := range orgs {
streams, err := queryUpcomingStreams(org)
if err != nil {
log.Printf("failed to query upcoming streams for org %s\n", org)
continue
}
log.Printf("found %d streams for %s matching holodex criteria\n", len(streams), org)
for _, stream := range streams {
if len(stream.Id) <= 0 {
log.Printf("%s has no reservation up, skipping\n", stream.Id)
continue
}
tx, err := app.db.Begin()
if err != nil {
sentry.CaptureException(err)
log.Printf("failed to start transaction: %s\n", err)
continue
}
var video Video
err = tx.QueryRow("select * from videos where id = $1 limit 1", stream.Id).Scan(
&video.Id,
pq.Array(&video.Submitters),
&video.Start,
&video.Finished,
&video.Title,
&video.ChannelName,
&video.ChannelId,
&video.Thumbnail,
&video.FileSize,
&video.Length,
&video.Downloads)
// Video already exists in db, skip it
if err == nil {
tx.Rollback()
log.Printf("skipping %s as it already is scheduled to be saved", stream.Id)
continue
}
videoMetadata, err := GetVideoMetadata(stream.Id)
if err != nil {
tx.Rollback()
sentry.CaptureException(err)
log.Printf("failed to get video meta data for %s: %s", stream.Id, err)
continue
}
startTime, err := GetVideoStartTime(videoMetadata)
if err != nil {
tx.Rollback()
sentry.CaptureException(err)
log.Printf("failed to get video start time for %s: %s\n", stream.Id, err)
continue
}
thumbnailUrl, err := SaveThumbnail(stream.Id, FindSuitableThumbnail(videoMetadata.Snippet.Thumbnails))
if err != nil {
tx.Rollback()
sentry.CaptureException(err)
log.Printf("failed to save thumbnail for %s: %s\n", stream.Id, err)
continue
}
statement, err := tx.Prepare("insert into videos (id, submitters, start, title, channel_name, channel_id, thumbnail) values ($1, $2, $3, $4, $5, $6, $7) returning *")
if err != nil {
tx.Rollback()
sentry.CaptureException(err)
log.Printf("failed to create video for %s\n", stream.Id)
continue
}
row := statement.QueryRow(stream.Id, pq.Array([]string{"pomu.app"}), startTime, videoMetadata.Snippet.Title, videoMetadata.Snippet.ChannelTitle, videoMetadata.Snippet.ChannelId, thumbnailUrl)
if err := row.Err(); err != nil {
tx.Rollback()
sentry.CaptureException(err)
log.Printf("failed to create video for %s\n", stream.Id)
continue
}
if err = row.Scan(&video.Id,
pq.Array(&video.Submitters),
&video.Start,
&video.Finished,
&video.Title,
&video.ChannelName,
&video.ChannelId,
&video.Thumbnail,
&video.FileSize,
&video.Length,
&video.Downloads); err != nil {
tx.Rollback()
log.Printf("failed to get video for %s\n", stream.Id)
sentry.CaptureException(err)
continue
}
if err := tx.Commit(); err != nil {
sentry.CaptureException(err)
log.Printf("failed to commit transaction: %s\n", err)
continue
}
err = app.scheduleVideo(videoMetadata, video.Id, VideoRequest{
VideoUrl: fmt.Sprintf("https://youtu.be/%s", video.Id),
// Use 0 to auto-pick best quality
Quality: 0,
})
if err != nil {
log.Printf("failed to automatically schedule video %s: %s\n", video.Id, err)
continue
}
go app.UpsertVideo(video)
log.Printf("Automatically scheduled %s (title: \"%s\") for %s\n", video.Id, video.Title, startTime.Format(time.RFC1123))
}
}
}
type UpcomingStreamChannel struct {
Id string `json:"id"`
Name string `json:"name"`
Organization string `json:"org"`
EnglishName string `json:"english_name"`
}
type UpcomingStream struct {
Id string `json:"id"`
Title string `json:"title"`
StartedScheduled string `json:"start_scheduled"`
Channel UpcomingStreamChannel `json:"channel"`
}
func queryUpcomingStreams(organization string) ([]UpcomingStream, error) {
request, err := http.NewRequest("GET", "https://holodex.net/api/v2/live", nil)
if err != nil {
fmt.Printf("Failed to start new request to holodex: %s", err)
return nil, err
}
request.Header.Set("X-APIKEY", os.Getenv("HOLODEX_API_KEY"))
request.Header.Set("User-Agent", "pomu.app")
query := request.URL.Query()
query.Set("include", "live_info")
query.Set("limit", "50")
query.Set("topic", os.Getenv("HOLODEX_TOPIC"))
query.Set("type", "stream")
query.Set("status", "upcoming")
query.Set("max_upcoming_hours", "24") // We query every hour anyways
query.Set("org", organization)
request.URL.RawQuery = query.Encode()
response, err := http.DefaultClient.Do(request)
defer response.Body.Close()
if err != nil {
fmt.Printf("Failed to send request to holodex: %s", err)
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("Failed to read response from holodex: %s", err)
return nil, err
}
var results []UpcomingStream
if err := json.Unmarshal(body, &results); err != nil {
fmt.Printf("Failed to unmarshal json response from holodex: %s", err)
return nil, err
}
return results, nil
}