-
Notifications
You must be signed in to change notification settings - Fork 7
/
search.js
171 lines (147 loc) · 4.48 KB
/
search.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
var request = require('superagent')
, _ = require('lodash')
, q = require('q')
, Review = require('./review.js')
, util = require('util')
, async = require('async')
, EventEmitter = require('events').EventEmitter
, cheerio = require('cheerio');
// global constants
var VERSION = require('./package').version
, USER_AGENT = 'omardelarosa/pitchfork-npm-v'+VERSION
, BASE_URL = "http://pitchfork.com/search/?query="
, CONNECTION_ERR = new Error("Failed to connect to Pitchfork!");
// extracts the first review inside of
function get_review_objects(responseBody){
var review_objects = [];
_.forEach(responseBody, function(obj){
var obj
var label = obj["label"];
if (label == "Reviews") {
obj["objects"].forEach(function(review){
review_objects.push(review);
})
}
})
return review_objects;
}
// normalize search response
function normalize_search_response(htmlString) {
var $ = cheerio.load(htmlString);
var titles = $('#result-albumreviews .review .review__title-album')
.map(function(idx, a) {
return _.get(a, 'children[0].data', null);
})
.filter(function (idx, a) { return !!a });
if (_.isEmpty(titles)) {
return [];
}
var links = $('#result-albumreviews .review .review__link')
.map(function(idx, a) {
return a.attribs['href'];
});
var artist = $('#result-albumreviews .review .review__title-artist li')
.map(function(idx, a) {
return a.children[0].data;
})[0];
return titles.map(function (idx, a) {
return {
name: artist + ' - ' + titles[idx],
url: links[idx]
}
});
}
/**
* Conducts a new search
*
* @constructor extends {EventEmitter}
*
* @param {String} artist - a musical artist likely to be reviewed on Pitchfork
* @param {String} album - an album by the given artist
* @param {Function} cb - a callback that fires when search is complete
* returns {Promise}
*/
function Search(artist, album, cb){
// future attributes of album
var attributes = {};
// a container for search results
this.results = [];
// replace spaces with %20
this.query = {
artist: artist,
album: album,
cb: cb
};
// run search;
this.promise = this.init();
}
// extends EventEmitter
util.inherits(Search, EventEmitter);
/**
* Runs a search and emits a "ready" event.
* @public
* @type {Promise}
*/
Search.prototype.init = function(){
var self = this
, queryArr = [self.query.artist]
, dfd = q.defer();
if (self.query.album) {
queryArr.push("%20");
queryArr.push(self.query.album);
}
query = queryArr.join("").replace(/\s+/,"%20")
// create a deferred obj
request.get(BASE_URL+query)
// .set('User-Agent', USER_AGENT)
.end(function(res) {
if (res.statusCode != 200) {
self.emit("error", CONNECTION_ERR)
cb(CONNECTION_ERR)
return dfd.reject(CONNECTION_ERR);
} else {
var reviews = normalize_search_response(res.text);
// if there are no matches...
if (reviews.length === 0) {
// ...return no results
self.results = [];
self.emit("ready", self.results);
if (self.query.cb) { self.query.cb(results); }
return dfd.resolve(self.results);
// if there are some matches and an album was entered
} else if (reviews.length > 0 && self.query.album) {
// return a single review object with that album
var r = new Review(reviews[0]);
// create reference to search in review.
r.search = self;
self.results = [r];
self.emit("ready", self.results);
if (self.query.cb) { self.query.cb(results); }
return dfd.resolve(self.results);
// if there are multiple matches and no album was entered
} else {
// return an array of albums
self.results = [];
var tasks = []
_.each(reviews, function(review){
var rev = review
tasks.push(function(done){
var r = new Review(rev)
r.search = self;
r.promise.then(function(revObj){
self.results.push(r)
done(null, revObj);
})
})
});
async.parallel(tasks, function(){
self.emit("ready", self.results);
if (self.query.cb) { self.query.cb(results); }
return dfd.resolve(self.results);
})
}
}
})
return dfd.promise;
}
module.exports = Search;