-
Notifications
You must be signed in to change notification settings - Fork 6
/
iresults.js
227 lines (208 loc) · 6.82 KB
/
iresults.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
216
217
218
219
220
221
222
223
224
225
226
/*
* Object to hold iTunes search query results.
* The result from apple is JSON format. This class will support pulling out
* specific attributes.
*/
var Album = require('./album').Album;
var Artist = require('./artist').Artist;
var Track = require('./track').Track;
var iError = require('./ierror').iError;
//Track total and completed instances of the class for debugging purposes.
var total = 0;
var complete = 0;
function iResults() {
this.glob = '';
this.numGlobs = 0;
this.data = '';
this.rawJSON = '';
total++;
};
exports.iResults = iResults;
//Debugging function.
iResults.prototype.stats = function () {
return {"complete":complete, "total":total}
};
//Builds the intermediary string of results returned by the iTunes web service in pieces.
iResults.prototype.capture = function (data) {
this.numGlobs++;
this.glob += data;
};
/*
iTunes returns a JSON that contains two elements:
a count of the results
a single element array containing a JSON of the result set.
This function sets the self.data to the actual itunes data set and returns 1 if parsing was successful, 0 otherwise.
In this way, the function can be used in an if statement.
e.g. if (self.parse) {
//analyze & process the data
} else {
//raise a warning
}
*/
iResults.prototype.parse = function () {
var self = this;
complete++;
// self.stats();
var success = 1;
try
{
self.rawJSON= JSON.parse(self.glob);
self.data = self.rawJSON.results;
self.hits= self.rawJSON.resultCount;
}
catch (err)
{
success = 0;
}
if (self.data == 'undefined') {
console.log('undefined data');
}
return success;
};
/*
Removes an initial 'The' from strings during search. In most cases 'The' should not prevent a match.
*/
noThe = function(title) {
title = title.toLowerCase();
var artArray = title.split(' ');
var normal = title;
if (artArray[0] == 'the') {
artArray.reverse();
artArray.pop();
artArray.reverse();
}
normal = artArray.join(' ');
return normal
}
/*
Extracts a few items from the iTunes results for an artist search and returns them in an object.
*/
iResults.prototype.getArtist = function(target,callback) {
var artist = null;
var error = null;
var i = 0;
var found = 0;
normTargetArtist = noThe(target.artist);
var userd = {input:
{artist:normTargetArtist}};
//console.log(userd);
while (this.data.length > i && found == 0) {
var item = this.data[i];
if (item.wrapperType == 'artist') {
normItemArtist = noThe(item.artistName);
var resultSet = {reply:
{artist:normItemArtist}};
//console.log(resultSet);
if (item.artistName == target.artist ||
normItemArtist == normTargetArtist) {
var artist = new Artist(item.artistLinkUrl,
item.amgArtistId,
item.artistId,
item.artistName);
found = 1;
};
};
i++;
};
if (found == 0) {
error = new iError(11);
}
callback(error,artist);
};
/*
Extracts a few items from the iTunes results for an album search and returns them in an object.
*/
iResults.prototype.getTrack = function(target,callback) {
var track = null;
var error = null;
var i = 0;
var found = 0;
var resultSet = '';
normTargetArtist = noThe(target.artist);
normTargetTrack = noThe(target.track);
var userd = {input:
{Track:normTargetTrack,
Artist:normTargetArtist}};
//console.log(userd);
while (this.data.length > i && found == 0) {
var item = this.data[i];
if (item.wrapperType == 'track' &&
item.kind == 'song' ) {
normItemArtist = noThe(item.artistName);
normItemTrack = noThe(item.trackName);
resultSet = {reply:
{Track:normItemTrack,
Artist:normItemArtist}};
// console.log(resultSet);
/* console.log(
normTargetArtist + ' ' +
normItemArtist + ' ' +
normTargetTrack + ' ' +
normItemTrack + ' '
);*/
if ((item.artistName == target.artist ||
normTargetArtist == normItemArtist) &&
(item.trackName == target.track ||
normTargetTrack == normItemTrack )) {
var track = new Track(item.trackName,
item.trackId,
item.trackViewUrl,
item.artistId,
item.artworkUrl60,
item.artworkUrl100,
item.artistName);
found = 1;
};
};
i++;
};
if (found == 0) {
error = new iError(9);
};
callback(error,track);
};
/*
Extracts a few items from the iTunes results for an album search and returns them in an object.
*/
iResults.prototype.getAlbum = function(target,callback) {
var album = null;
var error = null;
var i = 0;
var found = 0;
normTargetAlbum = noThe(target.album );
normTargetArtist = noThe(target.artist);
var userd = {input:
{Track:normTargetAlbum,
Artist:normTargetAlbum}};
//console.log(userd);
while (this.data.length > i && found == 0) {
var item = this.data[i];
if (item.wrapperType == 'collection' &&
item.collectionType == 'Album') {
normItemAlbum = noThe(item.collectionName);
normItemArtist = noThe(item.artistName);
resultSet = {reply:
{Track:normItemAlbum,
Artist:normItemArtist}};
//console.log(resultSet);
if ((item.artistName == target.artist ||
normTargetArtist == normItemArtist) &&
(item.collectionName == target.album ||
normTargetAlbum == normItemAlbum )) {
album = new Album(item.collectionViewUrl,
item.amgArtistId,
item.artistId,
item.collectionName,
item.artistName,
item.artworkUrl60,
item.artworkUrl100);
found = 1;
}
};
i++;
};
if (found == 0) {
error = new iError(10);
};
callback(error,album);
};