-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
133 lines (111 loc) · 2.81 KB
/
background.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
var oauth = null;
var playlist_key = 0; // Holds the Rdio playlist ID
// Called when the extension is first loaded.
document.addEventListener('DOMContentLoaded', function () {
oauth = ChromeExOAuth.initBackgroundPage({
'request_url': 'http://api.rdio.com/oauth/request_token',
'authorize_url': 'https://www.rdio.com/oauth/authorize',
'access_url': 'http://api.rdio.com/oauth/access_token',
'consumer_key': 'XXXXXXXXXXXXXXXXXXXXXXXX', // Your Rdio API key
'consumer_secret': 'XXXXXXXXXX' // Your Rdio API shared secret
});
// Grab an Rdio oAuth token when the extension is first loaded. Stored in
// localStorage so that we don't have to ask for authorization again.
if (!oauth.hasToken())
{
oauth.authorize(function() {});
}
});
function formEncode(params) {
var encoded = [];
for (prop in params)
{
if (!params.hasOwnProperty(prop)) continue;
encoded.push(encodeURIComponent(prop) + '=' + encodeURIComponent(params[prop]))
}
return encoded.join('&');
};
function rdio(params, onSuccess) {
var rdioCall = function() {
oauth.sendSignedRequest('http://api.rdio.com/1/', onSuccess, {
'method': 'POST',
'body': formEncode(params)
});
};
if (oauth.hasToken())
{
rdioCall();
}
else {
oauth.authorize(rdioCall);
}
};
function addToPlaylist(artist, title) {
// Search for the track in Rdio and grab its track key
var params = {
'method': 'search',
'query': artist + ' ' + title,
'types': 'Track',
'count': 1
};
rdio(params, function(data) {
res = JSON.parse(data)
track_key = res['result']['results'][0]['key'];
// Add the track if we already know the playlist key
if (playlist_key != 0)
{
addTrack(track_key);
}
else {
// Otherwise find the playlist first and then add the track
findPlaylist(track_key);
}
});
};
function addTrack(track_key) {
var params = {
'method': 'addToPlaylist',
'playlist': playlist_key,
'tracks': track_key
};
rdio(params, function(data) {
console.log('Track added');
});
};
function findPlaylist(track_key) {
rdio({'method': 'getPlaylists'}, function(data) {
res = JSON.parse(data);
playlists = res['result']['owned']
for (var i = 0; i < playlists.length; i++)
{
playlist = playlists[i];
if (playlist['name'] == 'Pandora Favorites')
{
playlist_key = playlist['key'];
}
}
// If the playlist doesn't exist, create it.
if (playlist_key == 0)
{
var params = {
'method': 'createPlaylist',
'name': 'Pandora Favorites',
'description': 'Favorite songs from Pandora',
'tracks': track_key
};
rdio(params, function(data) {
res = JSON.parse(data);
playlist_key = res['result']['key'];
});
}
else {
addTrack(track_key);
}
});
};
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse)
{
addToPlaylist(request.artist, request.title);
}
);