-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetch.go
363 lines (322 loc) · 8.86 KB
/
fetch.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
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
package main
import (
"database/sql"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"sort"
"strconv"
"sync"
"time"
)
type Genre struct {
Name string `json:"name" dynamodbav:"genre_name"`
Count int `json:"count"`
}
type Entity struct {
ID int `json:"id"`
FoundTracks int `json:"found_tracks"`
ThumbnailMedium sql.NullString `json:"thumbnail_medium"`
AddedDatetime sql.NullTime `json:"added_datetime"`
PlaylistId string `json:"playlist_id"`
CountFollowers int `json:"count_followers"`
Genres []Genre `json:"genres"`
LastFoundTime time.Time `json:"last_found_time"`
CountTracks int `json:"count_tracks"`
}
type YoutubeChannel struct {
ChannelId string `json:"channel_id"`
ChannelName string `json:"channel_name"`
TerminatedDatetime sql.NullTime `json:"terminated_datetime"`
LastUploadDatetime time.Time `json:"last_upload_datetime"`
UploadPlaylistId string `json:"upload_playlist_id"`
Entity
}
type DiscogsLabel struct {
LabelId string `json:"label_id"`
LabelName string `json:"label_name"`
Entity
}
type Event struct {
Host string `json:"host"`
Timestamp string `json:"timestamp"`
Added int `json:"added"`
SpotifyPlaylist string `json:"spotify_playlist" dynamodbav:"spotify_playlist"`
EntityID string `json:"entity_id" dynamodbav:"entity_id"`
EntityName string `json:"entity_name" dynamodbav:"entity_name"`
}
func (c *Client) getYoutubeChannels(orderBy, order string, limit, limitGenres int) (res []YoutubeChannel, err error) {
query := fmt.Sprintf(`
SELECT
c.id, c.channel_id, c.channel_name, c.count_tracks as count_tracks,
c.last_upload_datetime, c.thumbnail_medium, c.upload_playlist_id,
c.terminated_datetime, c.added_datetime,
p.spotify_playlist, p.found_tracks, p.count_followers as count_followers,
p.last_found_time as last_found_time
FROM
yt_channels as c
INNER JOIN
yt_playlists p on c.channel_id = p.channel_id
GROUP BY id ORDER BY %s %s LIMIT ?`, orderBy, order)
selDB, err := c.SQLDriver.Query(query, limit)
if err != nil {
fmt.Println(err.Error())
return res, err
}
var ch YoutubeChannel
for selDB.Next() {
err = selDB.Scan(
&ch.ID,
&ch.ChannelId,
&ch.ChannelName,
&ch.CountTracks,
&ch.LastUploadDatetime,
&ch.ThumbnailMedium,
&ch.UploadPlaylistId,
&ch.TerminatedDatetime,
&ch.AddedDatetime,
&ch.PlaylistId,
&ch.FoundTracks,
&ch.CountFollowers,
&ch.LastFoundTime)
if err != nil {
return res, err
}
genres, err := c.getEntityGenres(ch.ID, limitGenres)
if err != nil {
return res, err
}
ch.Genres = genres
res = append(res, ch)
}
return res, nil
}
func (c *Client) getYoutubeChannelsTerminated(orderBy, order string, limit, limitGenres int) (res []YoutubeChannel, err error) {
query := fmt.Sprintf(`
SELECT
c.id, c.channel_id, c.channel_name, c.count_tracks as count_tracks,
c.last_upload_datetime, c.thumbnail_medium, c.upload_playlist_id,
c.terminated_datetime, c.added_datetime,
p.spotify_playlist, p.found_tracks, p.count_followers as count_followers,
p.last_found_time as last_found_time
FROM
yt_channels as c
INNER JOIN
yt_playlists p on c.channel_id = p.channel_id
WHERE
terminated_datetime IS NOT NULL
GROUP BY id ORDER BY %s %s LIMIT ?`, orderBy, order)
selDB, err := c.SQLDriver.Query(query, limit)
if err != nil {
fmt.Println(err.Error())
return res, err
}
var ch YoutubeChannel
for selDB.Next() {
err = selDB.Scan(
&ch.ID,
&ch.ChannelId,
&ch.ChannelName,
&ch.CountTracks,
&ch.LastUploadDatetime,
&ch.ThumbnailMedium,
&ch.UploadPlaylistId,
&ch.TerminatedDatetime,
&ch.AddedDatetime,
&ch.PlaylistId,
&ch.FoundTracks,
&ch.CountFollowers,
&ch.LastFoundTime)
if err != nil {
return res, err
}
genres, err := c.getEntityGenres(ch.ID, limitGenres)
if err != nil {
return res, err
}
ch.Genres = genres
res = append(res, ch)
}
return res, nil
}
func (c *Client) getEntityGenres(entityId int, limitGenres int) ([]Genre, error) {
query := fmt.Sprintf(`
SELECT genre_name, count
FROM yt_genres
WHERE yt_channel_id = ?
ORDER BY count DESC
LIMIT ?`)
db, err := c.SQLDriver.Query(query, entityId, limitGenres)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
var g Genre
var genres []Genre
for db.Next() {
err = db.Scan(&g.Name, &g.Count)
if err != nil {
return nil, err
}
genres = append(genres, g)
}
return genres, nil
}
func (c *Client) getTableCount(table string) (count *int64, err error) {
describeTable := &dynamodb.DescribeTableInput{
TableName: aws.String(table),
}
res, err := c.DynamoDB.DescribeTable(describeTable)
if err != nil {
return nil, err
}
return res.Table.ItemCount, nil
}
func (c *Client) getYoutubeChannel(channelId string) (ch YoutubeChannel, err error) {
selDB := c.SQLDriver.QueryRow(fmt.Sprintf(`
SELECT
c.id, c.channel_id, c.channel_name, c.count_tracks, c.last_upload_datetime,
c.thumbnail_medium, c.upload_playlist_id, c.terminated_datetime, c.added_datetime,
p.spotify_playlist, p.found_tracks
FROM yt_channels as c
INNER JOIN yt_playlists p on c.channel_id = p.channel_id
WHERE c.channel_id = ?`), channelId)
err = selDB.Scan(
&ch.ID,
&ch.ChannelId,
&ch.ChannelName,
&ch.CountTracks,
&ch.LastUploadDatetime,
&ch.ThumbnailMedium,
&ch.UploadPlaylistId,
&ch.TerminatedDatetime,
&ch.AddedDatetime,
&ch.PlaylistId,
&ch.FoundTracks)
return ch, err
}
func (c *Client) getDiscogsLabels(orderBy, order string, limit, limitGenres int) (res []DiscogsLabel, err error) {
query := fmt.Sprintf(`
SELECT
l.id, l.label_id, l.label_name, l.count_tracks as count_tracks,
l.thumbnail_medium, l.added_datetime,
p.spotify_playlist, p.found_tracks, p.count_followers as count_followers,
p.last_found_time as last_found_time
FROM
dg_labels as l
INNER JOIN
dg_playlists p on l.label_id = p.label_id
GROUP BY id ORDER BY %s %s LIMIT ?`, orderBy, order)
selDB, err := c.SQLDriver.Query(query, limit)
if err != nil {
fmt.Println(err.Error())
return res, err
}
var la DiscogsLabel
for selDB.Next() {
err = selDB.Scan(
&la.ID,
&la.LabelId,
&la.LabelName,
&la.FoundTracks,
//&la.LastUploadDatetime,
&la.ThumbnailMedium,
&la.AddedDatetime,
&la.PlaylistId,
&la.CountTracks,
&la.CountFollowers,
&la.LastFoundTime)
if err != nil {
return res, err
}
genres, err := c.getEntityGenres(la.ID, limitGenres)
if err != nil {
return res, err
}
la.Genres = genres
res = append(res, la)
}
return res, nil
}
func (c *Client) getDiscogsLabel(labelId string) (la DiscogsLabel, err error) {
selDB := c.SQLDriver.QueryRow(fmt.Sprintf(`
SELECT
l.id, l.label_id, l.label_name, l.count_tracks, l.thumbnail_medium, l.added_datetime,
p.spotify_playlist, p.found_tracks
FROM dg_labels as l
INNER JOIN dg_playlists p on l.label_id = p.label_id
WHERE l.label_id = ?`), labelId)
err = selDB.Scan(
&la.ID,
&la.LabelId,
&la.LabelName,
&la.FoundTracks,
&la.ThumbnailMedium,
&la.AddedDatetime,
&la.PlaylistId,
&la.CountTracks)
return la, err
}
func (c *Client) getEventsFromType(count int, entity string, ch chan []Event, wg *sync.WaitGroup) error {
defer (*wg).Done()
oneDayAgo := time.Now().Local().Add(-time.Hour * 24).Unix()
var ev []Event
queryInput := &dynamodb.QueryInput{
KeyConditions: map[string]*dynamodb.Condition{
"host": {
ComparisonOperator: aws.String("EQ"),
AttributeValueList: []*dynamodb.AttributeValue{
{
S: aws.String(entity),
},
},
},
"timestamp": {
ComparisonOperator: aws.String("GE"),
AttributeValueList: []*dynamodb.AttributeValue{
{
N: aws.String(strconv.FormatInt(oneDayAgo, 10)),
},
},
},
},
Limit: aws.Int64(int64(count)),
ScanIndexForward: aws.Bool(false),
TableName: aws.String(c.DynamoDBEventsTable),
}
result, err := c.DynamoDB.Query(queryInput)
if err != nil {
fmt.Println("Query API call failed:")
fmt.Println(err.Error())
return err
}
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &ev)
if err != nil {
fmt.Println("Got error unmarshalling events")
fmt.Println(err.Error())
return err
}
ch <- ev
return nil
}
func (c *Client) getEvents(count int) (events []Event, err error) {
ch := make(chan []Event)
var wg sync.WaitGroup
wg.Add(1)
go c.getEventsFromType(count, "yt", ch, &wg)
wg.Add(1)
go c.getEventsFromType(count, "dg", ch, &wg)
go func() {
wg.Wait()
close(ch)
}()
for entityEvents := range ch {
events = append(events, entityEvents...)
}
// Mix both YT and DG events
sort.Slice(events, func(i, j int) bool {
return events[i].Timestamp > events[j].Timestamp
})
return events, nil
}