Repository navigation
Expand file tree
/
Copy pathhackfetch.js
More file actions
141 lines (127 loc) · 5.64 KB
/
Copy pathhackfetch.js
File metadata and controls
141 lines (127 loc) · 5.64 KB
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
var hnews = function() {
if (typeof require === 'function') {
var fetch = require('node-fetch');
var exports = module.exports = {};
var fetchReq = function(url) {
return fetch(url)
.then(function(response) {
return response.json();
})
.catch(function(err) {
console.log('err: ' + err);
});
};
/**
* Requests up to 500 of the current top items
* @return returns the a Promise with the stories in an array by id
* @example arr = [1337905, 137906, 11376777, ...];
*/
exports.getTopItems = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/topstories.json');
};
/**
* Requests up to 500 of the current new items
* @return returns the a Promise with the stories in an array by id
* @example arr = [1337905, 137906, 11376777, ...];
*/
exports.getNewItems = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/newstories.json');
};
/**
* Requests up to 500 of the current show items
* @return returns the a Promise with the stories in an array by id
* @example arr = [1337905, 137906, 11376777, ...];
*/
exports.getShowItems = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/showstories.json');
};
/**
* Requests up to 500 of the current ask items
* @return returns the a Promise with the stories in an array by id
* @example arr = [1337905, 137906, 11376777, ...];
*/
exports.getAskItems = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/askstories.json');
}
/**
* Requests up to 500 of the current job items
* @return returns the a Promise with the stories in an array by id
* @example arr = [1337905, 137906, 11376777, ...];
*/
exports.getJobItems = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/jobstories.json');
};
/**
* Requests an array of ids from one of 3 categories
* @return returns a complete array with story information from a selected category
* @param {String} category - The category to get stories from.
* This is an optional param if no category is passed then automatically get topstories.json
* @param {Int} numToGet - The number of items you'd like to receive starting from 0 -> Int
* This is an optional param if no number is passed then return all results
* this may be slow...
* @example arr = [1337905, 137906, 11376777, ...];
*/
exports.getIdsInCategory = function(category, numToGet) {
numToGet = numToGet || 500;
if (arguments.length === 2) {
var num;
if (numToGet === 500) {
console.warn('Gather information for 500 stories is very slow. Consider lowering the number to return');
num = numToGet.toString();
console.log(category);
return fetchReq('https://hacker-news.firebaseio.com/v0/' + category + '.json?orderBy="$key"&limitToFirst=' + num);
} else {
num = numToGet.toString();
return fetchReq('https://hacker-news.firebaseio.com/v0/' + category + '.json?orderBy="$key"&limitToFirst=' + num);
}
} else {
throw Error('You must pass 2 arguments to getIdsInCategory. Please see http://hackdot.co/developers#getDataForCategory');
}
};
/*
* @param {Int} item_id - The story id details to be requested
* @return returns the a Promise with the request users details
* @example returned object
* {
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671]
"score" : 111,
"time" : 1175714200,
"title" : "My YC app: Dropbox - Throw away your USB drive",
"type" : "story",
"url" : "http://www.getdropbox.com/u/2/screencast.html"
}
*/
exports.getItemById = function(item_id) {
var url = 'https://hacker-news.firebaseio.com/v0/item/' + item_id + '.json';
return fetchReq(url);
};
/**
* Request a username by with a case-sensitive id. Only users that have public activity (comments or story submissions) on the site are available through the API.
* @param {String} username - The users id
* @return returns the a Promise with the request users details
*/
exports.getUser = function(username) {
var url = 'https://hacker-news.firebaseio.com/v0/user/' + username + '.json';
return fetchReq(url);
};
/**
* The current largest item id
* @return returns the item as a Promise
*/
exports.getMaxItem = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/maxitem.json');
};
/**
* The item and profile changes
* @return returns two arrays. One for item changes and another for profile changes
* @example 'items': [1, 2, 3, ..., 5]; 'profiles': ['pg', 'colealanr'];
*/
exports.getUpdates = function() {
return fetchReq('https://hacker-news.firebaseio.com/v0/updates.json?print=pretty');
};
return exports;
}
}();