-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (185 loc) · 5.93 KB
/
index.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
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
var logger = require('./logger.js')
var Promise = require('bluebird')
var rp = require('request-promise');
var search = Promise.promisify(require('youtube-search'));
var Youtube = require("youtube-api");
var Playlists = Promise.promisifyAll(Youtube.playlists);
var PlaylistItems = Promise.promisifyAll(Youtube.playlistItems)
var readJson = require("r-json")
var opn = require('opn')
var cheerio = require('cheerio')
Promise.config({
longStackTraces: true,
})
function exitHandler() {
process.exit(1)
}
Promise.onPossiblyUnhandledRejection(function(error) {
logger.fatal('unhandled rejection: ', error);
setTimeout(() => exitHandler(), 5000)
});
process.on('uncaughtException', function(err) {
logger.fatal('Caught uncaughtException: ' + err + '\nstack: ' + err.stack + '\nstopping app...');
setTimeout(() => exitHandler(), 5000)
});
logger.debug('hello from musicmedley')
var CREDENTIALS = readJson(`${__dirname}/credentials.json`);
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello from musicmedley');
});
var authPromise = new Promise(function(resolve, reject) {
app.get('/oauth2callback', function(req, res){
var code = req.query.code;
oauth.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
oauth.setCredentials(tokens);
resolve();
} else {
reject(err)
}
});
res.send('tokens saved');
});
})
app.listen(3000)
var oauth = Youtube.authenticate({
type: "oauth"
, client_id: CREDENTIALS.web.client_id
, client_secret: CREDENTIALS.web.client_secret
, redirect_url: CREDENTIALS.web.redirect_uris[0]
})
opn(oauth.generateAuthUrl({
access_type: "offline"
, scope: ["https://www.googleapis.com/auth/youtube"]
}))
var createPlaylistIfNotExists = function(title, description, privacy) {
return Playlists.listAsync({
'part': 'snippet,contentDetails',
'mine' : 'true'
}).then((response) => {
for(var i = 0; i < response.items.length; ++i) {
if(response.items[i].snippet.title == title && response.items[i].snippet.description == description) {
logger.debug('playlist already exists');
return response.items[i];
}
}
}).then((playlist) => {
if(playlist)
return playlist;
return Youtube.playlists.insertAsync({
part: 'snippet,status',
resource: {
snippet: {
title: title,
description: description
},
status: {
privacyStatus: privacy
}
}
}).then((result) => {
logger.debug('playlist created');
return result;
})
})
}
var getMusicBandInfo = function(musicBand, url) {
var localUrl = url;
var bandInfo = { title: musicBand, url: url, discography: [] };
return rp.get(url).then((response) => {
var $ = cheerio.load(response);
var discographyItems = $('div.custom-artist-releases-text').find('strong');
logger.debug('got info for', localUrl)
discographyItems.each(function(i, element) {
if(element.parent.parent.attribs['class'] == 'first')
return true;
var album = $(this).text()
bandInfo.discography.push(album)
})
return bandInfo;
})
}
var getMusicBands = function() {
var requestUrl = 'https://api.import.io/store/connector/2c9f1256-d5ad-4624-8af6-9e9e026c2b39/_query?input=webpage/url:http%3A%2F%2Fwww.summer-breeze.de%2Fen%2Fbands%2Findex.html&&_apikey=d661281789fc42a18889853b03aa2770a09bc3488cb1c7d418ab9a0f5572ef78e5e34c59e6cea878e73c6e5839390eb0f163535c3b1bee20e210025aa7f4c7b1fe18aa4a76119de3aa9e8f9dd8e1673c'
var getInfoPromises = [];
return rp.get(requestUrl).then((response) => {
var jsonResponse = JSON.parse(response)
jsonResponse.results.some(function(element) {
var musicBand = element['link/_text']
getInfoPromises.push(getMusicBandInfo(musicBand, element.link));
// return true; // debug
}, this);
return Promise.all(getInfoPromises);
})
}
var searchMusicBands = function(musicBands) {
var opts = {
maxResults: 5,
order: 'relevance',
type: 'video',
safeSearch: 'none',
videoDuration: 'short',
key: readJson(`${__dirname}/apikey.json`).key
};
var searches = [];
musicBands.forEach((musicBand) => {
searches.push(search(musicBand.title, opts).then((res) => {
logger.debug('got results for: ', musicBand)
return res;
}))
})
return Promise.all(searches)
}
var skipped = 0;
var addToPlaylist = function(playlistId, kind, videoId) {
logger.debug('adding: ', videoId)
console.log('adding: ', videoId);
return PlaylistItems.insertAsync({
part: 'snippet',
resource: {
snippet: {
playlistId: playlistId,
resourceId: {
kind: kind,
videoId: videoId
}
}
}
}).then((i) => {
logger.debug('added: ', videoId);
console.log('added: ', videoId);
return i;
}).catch((e) => {
logger.error('failed to add: ', videoId, 'skipping... ');
skipped++;
})
}
authPromise.then(() => {
logger.info('authentication succeed');
return createPlaylistIfNotExists('summer-breeze v1.01', 'summer-breeze bands medley v2', 'public')
}).then((playlist) => {
logger.info('playlist resolved');
return getMusicBands().then(searchMusicBands).then((searchResults) => {
var addToPlaylistItems = []
searchResults.forEach((bandResults) => {
logger.info(bandResults)
bandResults.forEach((result) => {
addToPlaylistItems.push({playlistId: playlist.id, kind: result.kind, id: result.id})
})
})
return addToPlaylistItems;
})
}).then((addToPlaylistItems) => {
return Promise.each(addToPlaylistItems, function(item) {
return Promise.delay(500).then(() => {
return addToPlaylist(item.playlistId, item.kind, item.id);
})
})
}).then((results) => {
logger.info('done!', 'skipped: ', skipped);
}).catch((err) => {
logger.error('error: ', err)
})