-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_tracks.js
134 lines (129 loc) · 4.46 KB
/
add_tracks.js
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
const ents = require('./ents/listings')
const tools = require('./tools')
const database_file = './db/gig_playlists.db'
const db = tools.getSqlLiteDB(database_file)
const logger = tools.getLogger('add_tracks')
async function get_gigs_ents24 () {
const axios_client = await tools.createEntsClient();
const today = new Date()
var month_city_playlists = await new Promise((resolve, reject) => {
db.all('select * from uk_city_playlist', [], (err, rows) => {
if (err) reject(err)
resolve(rows)
})
});
for (const month_city of month_city_playlists) {
var postcode = month_city.postcode
var month_index = tools.getMonthNumber(month_city.month)
// iterate through months Jan-Dec of given Location
// If date is in past, find tracks for same date next year
var days
var day_in_month = new Date(today.getUTCFullYear(), month_index, 1) // start from 1st
if (month_index === today.getUTCMonth()) {
days = tools.getDaysInMonth(
today.getDate(),
month_index,
day_in_month.getUTCFullYear()
)
} else if (day_in_month < today) {
// if date in past get next year
days = tools.getDaysInMonth(
0,
month_index,
day_in_month.getUTCFullYear() + 1
)
} // date in question is in future
else {
days = tools.getDaysInMonth(0, month_index, day_in_month.getUTCFullYear())
}
for (const day of days) {
logger.info(`Processing gigs from ents on ${day} in ${postcode}.`)
var gigs = await ents.getListings(axios_client, day, postcode);
console.log(gigs);
if (gigs?.length > 0) {
let gig_date = tools.formatDate(today)
// each day has its own metadata. crush into one to get all artist names that appear in that month's playlist.
//await gigs_to_spotify_track(gigs, month_city, gig_date)
}
}
}
}
async function gigs_to_spotify_track (gigs, month_city, gig_date) {
var spotifyApi = await tools.getSpotifyAPI()
spotifyApi = await tools.refreshSpotifyToken(spotifyApi)
// don't want same artist showing up multiple times in one playlist.
var song_uris = []
for (const gig of gigs) {
if (!gig.title) {
var artist_name = gig.headline
var songs = await spotifyApi.searchTracks(`artist:${gig.headline}`).then(
function (data) {
if (data.body.tracks.items) {
return data.body.tracks.items
}
},
function (err) {
logger.error(`error getting tracks for ${gig.headline}`, err);
}
)
var top_song = false
let index_song = 0
if (songs?.length > 0) {
outerLoopSongs: while (index_song < songs.length) {
var artists = songs[index_song]?.artists ?? []
for (const artist of artists) {
// eliminate possibility of tribute acts, misnomers etc
if (artist.name == artist_name) {
top_song = songs[index_song]
break outerLoopSongs
}
}
index_song += 1
}
if (top_song) {
try {
await new Promise((resolve, reject) => {
db.all(
'insert into event_track(uk_city_playlist_id, artist_name, date_collected, end_date, genre, start_date, track_name, track_uri, web_link, venue_name) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
month_city.uk_city_playlist_id,
artist_name,
gig_date,
gig.endDate,
gig.genre.join('_'),
gig.startDate,
top_song.name,
top_song.uri,
gig.webLink,
gig.venue.name
],
(err, rows) => {
if (err) reject(err)
resolve(rows)
}
)
})
song_uris.push(top_song.uri)
await tools.sleep(1000)
} catch (error) {
if (error.errno != 19 && error.code != 'SQLITE_CONSTRAINT') {
throw new Error(
`unexpected error, expecting insert fail on constraint, received instead ${error}`
)
}
}
}
}
}
}
await tools.sleep(1000)
if (song_uris.length > 0) {
spotifyApi.addTracksToPlaylist(month_city.playlist_id, song_uris).then(
function (data) {},
function (err) {
logger.error('Something went wrong!', err)
}
)
}
}
get_gigs_ents24();