-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
232 lines (196 loc) · 5.39 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
'use strict'
var querystring = require('querystring')
var request = require('request')
// Constants
/**
* Max number of hits (results) for a query - set by Algolia
*/
var MAX_HITS_PER_PAGE = '1000'
/**
* Request types
*/
var TYPE_ITEM = 'items'
var TYPE_USER = 'users'
var TYPE_SEARCH = 'search'
var TYPE_SEARCH_BY_DATE = 'search_by_date'
// Helper functions
/**
* Generate the numeric filter
*
* @param caller What is the name of the calling function?
* @param marker What is the date range marker?
*/
function numericFilters (caller, marker) {
var sym = '='
switch (caller) {
case 'before':
sym = '<' + sym
break
case 'since':
sym = '>' + sym
break
}
// Don't set a timestamp incase of forever, better performance
var nf = (marker === 'forever') ? ''
: 'created_at_i' + sym + timestamp(marker)
return nf
}
/**
* Generate Unix timestamp based on date range marker. Based on Algolia's own
* hnsearh.js. See repo for more: https://github.com/algolia/hn-search
*/
function timestamp (marker) {
var now = new Date()
var nowUtc = Date.UTC(now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds()) / 1000
switch (marker) {
case 'past_24h':
return (nowUtc - (24 * 60 * 60))
case 'past_week':
return (nowUtc - (7 * 24 * 60 * 60))
case 'past_month':
return (nowUtc - (30 * 24 * 60 * 60))
}
}
// module.exports
var Hn = function () {
this.type = TYPE_SEARCH
this.params = { hitsPerPage: '', tags: [] }
this.tags_or = []
this.tags_and = []
// Make HTTP request
this.call = function (cb) {
this.params.hitsPerPage = MAX_HITS_PER_PAGE
// Build the tags that will be logically OR'ed
if (this.tags_or.length > 0) {
this.params.tags.push('(' + this.tags_or.toString() + ')')
}
// Build the tags that will be logically AND'ed
if (this.tags_and.length > 0) {
this.params.tags.push(this.tags_and.toString())
}
// Final tag param for querystring
this.params.tags = this.params.tags.toString()
// Build querystring
var query = 'https://hn.algolia.com/api/v1/' + this.type
var queryArgs = querystring.stringify(this.params)
if (this.params.tags.length > 0) query += '?' + queryArgs
if (this.type === TYPE_ITEM || this.type === TYPE_USER) {
// In this case, there are no query_args
query = query + '/' + this.id
}
// Reset hn object attributes before request is made
this.type = TYPE_SEARCH
this.params = { hitsPerPage: '', tags: [] }
this.tags_or = []
this.tags_and = []
// Now make acutal HTTP request
request(query, function (error, response, body) {
if (!error && response.statusCode !== 200) {
error = response.statusCode
}
if (typeof body !== 'undefined') {
try {
body = JSON.parse(body)
} catch (ex) {
if (!error) error = ex
}
}
cb(error, body)
})
}
}
module.exports = new Hn()
// Prototype method chaining
var FUNCTIONS_TAG = ['story',
'comment',
'poll',
'pollopt',
'show_hn',
'ask_hn',
'author']
FUNCTIONS_TAG.forEach(function (fName) {
Hn.prototype[fName] = function (id) {
if (arguments.length === 1 && (fName === 'author' || fName === 'story')) {
// Have an arg to deal with, either the author (username) or story id
this.tags_and.push(fName + '_' + id)
} else {
this.tags_or.push(fName)
}
return this
}
})
var FUNCTIONS_FILTER = ['top', 'recent']
FUNCTIONS_FILTER.forEach(function (fName) {
Hn.prototype[fName] = function (cb) {
switch (fName) {
case 'top':
this.type = TYPE_SEARCH
break
case 'recent':
this.type = TYPE_SEARCH_BY_DATE
break
}
if (arguments.length === 1 && typeof cb === 'function') {
// Method chaining has stopped, can execute call
this.call(cb)
} else {
return this
}
}
})
var FUNCTIONS_TIME = ['since', 'before']
FUNCTIONS_TIME.forEach(function (fName) {
Hn.prototype[fName] = function (marker, cb) {
this.type = TYPE_SEARCH_BY_DATE
this.params.numericFilters = numericFilters(fName, marker)
if (arguments.length === 2 && typeof cb === 'function') {
this.call(cb)
} else {
return this
}
}
})
Hn.prototype.search = function (query, cb) {
this.params.query = query
if (arguments.length === 2 && typeof cb === 'function') {
this.call(cb)
} else {
return this
}
}
// Persists from one request to another
Hn.prototype.hitsPerPage = function (n) {
if (typeof n !== 'number') { console.log('n is NaN'); return }
if (n % 1 !== 0) { console.log('n must be an integer'); return }
if (n > 1000 || n < 1) { console.log('1 <= n <= 1000'); return }
MAX_HITS_PER_PAGE = n
return this
}
Hn.prototype.page = function (n) {
if (typeof n !== 'number') { console.log('n is NaN'); return }
if (n % 1 !== 0) { console.log('n must be an integer'); return }
if (n < 0) { console.log('0 <= n'); return }
this.params.page = n
return this
}
// Prototype non-chainable methods
var FUNCTIONS_SINGLE = ['item', 'user']
FUNCTIONS_SINGLE.forEach(function (fName) {
Hn.prototype[fName] = function (id, cb) {
switch (fName) {
case 'item':
this.type = TYPE_ITEM
break
case 'user':
this.type = TYPE_USER
break
}
this.id = id
this.call(cb)
}
})