Socket
Socket
Sign inDemoInstall

yc-api

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yc-api - npm Package Compare versions

Comparing version 1.0.5 to 2.0.0

11

package.json
{
"name": "yc-api",
"version": "1.0.5",
"version": "2.0.0",
"description": "This is a wrapper module around the Hacker News API. You can filter the news in order to show you only the stories with a minimum score.",
"main": "yc-api.js",
"dependencies": {
"async": "^2.0.0-rc.3",
"request": "^2.72.0"
"axios": "^0.19.0"
},
"devDependencies": {
"babel-cli": "^6.7.5",
"babel-preset-es2015": "^6.6.0"
},
"devDependencies": {},
"scripts": {
"build": "babel --presets es2015 ./src/app.js -o yc-api.js && node yc-api.js",
"test": "echo \"Error: no test specified\" && exit 1"

@@ -17,0 +12,0 @@ },

@@ -1,49 +0,45 @@

What is it?
===========
# What is it?
This is a wrapper module around the [Hacker News](https://news.ycombinator.com/) API. You can filter the news in order to show you only the stories with a minimum score.
Installation
============
As simple as `npm install yc-api`
# async/await support
Tha last version of this library support async/await.
Usages
------
# Installation
Import the module with `var yc-api = require('yc-api')`.
As simple as `npm install yc-api`
In the new es6 fashon `import yc-pi from yc-api`.
## Usages
Import the module with `const yc-api = require('yc-api')`.
###get\_top\_stories\_id(cb)
### get_top_stories_id()
This take the top stories (IDs) from YC and return them into the callback.
This take the top stories (IDs) from YC and return them.
### get_story(url)
Takes an _url_ as argument and return a **JSON** object containing the story.
###get_story(url,cb)
Takes an *url* as argument and return into the callback a **JSON** object containing the story.
The returned object looks like this:
{
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671, 8940, 9067, 8908, 9055, 8865, 8881, 8872, 8873, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8878, 8870, 8980, 8934, 8876 ],
"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"
}
{
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671, 8940, 9067, 8908, 9055, 8865, 8881, 8872, 8873, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8878, 8870, 8980, 8934, 8876 ],
"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"
}
### get_stories()
###get_stories(cb)
Return into a callback an array of **JSON** object containing the stories.
Return an array of **JSON** object containing the stories.
### stories_with_score(score)
###stories\_with\_score(score,cb)
Same as **get_stories** but return into the object only the stories with the minimum score of **score**.

@@ -53,24 +49,15 @@

stories_with_score(200,function (stories){
console.log(stories);
});
stories_with_score(200).then(stories => console.log(stories));
# Example
Example
=======
var yc_api = require('yc-api');
var async = require('async');
var yc_api = require('yc-api');
var async = require('async');
var yc = new yc_api.API;
var yc = new yc_api.API;
yc.stories_with_score(200).then(stories => console.log(stories));
yc.stories_with_score(500, function (stories) {
async.each(stories,function (story, cb) {
console.log(story.title);
cb();
}, function (err, res) {
if (err) throw err;
console.log('done');
});
});
// using async
const stories = await yc.stories_with_score(200);
console.log(stories)
"use strict";
const axios = require("axios");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.API = undefined;
class API {
constructor() {
this.top_stories_url =
"https://hacker-news.firebaseio.com/v0/topstories.json";
this.story_url = "https://hacker-news.firebaseio.com/v0/item";
}
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
// get the top stories on YC and parse them as json to a cb
async get_top_stories_id() {
const stories = await axios.get(this.top_stories_url);
return stories.data;
}
var _request = require("request");
async get_story(url) {
const story = await axios.get(url);
return story.data;
}
var _request2 = _interopRequireDefault(_request);
// get the top stories cycle them into the get_story function
// pass trougth a cb an array of stories
async get_stories() {
let promises = [];
const ids = await this.get_top_stories_id();
for (let id of ids) {
let url = `${this.story_url}/${id}.json`;
const story = this.get_story(url);
promises.push(story);
}
var _async = require("async");
const stories = await Promise.all(promises);
return stories;
}
var _async2 = _interopRequireDefault(_async);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var API = exports.API = function () {
function API() {
_classCallCheck(this, API);
this.top_stories_url = 'https://hacker-news.firebaseio.com/v0/topstories.json';
this.story_url = 'https://hacker-news.firebaseio.com/v0/item';
// Get all the stories from the top one's
// And filter them passing only the story with a minimun score
// pass an array of story into the cb
async stories_with_score(score) {
let result = [];
const stories = await this.get_stories();
for (let story of stories) {
if (story.score >= score) result.push(story);
}
return result;
}
}
// get the top stories on YC and parse them as json to a cb
_createClass(API, [{
key: "get_top_stories_id",
value: function get_top_stories_id(cb) {
_request2.default.get(this.top_stories_url, function (err, resp, body) {
if (err) throw err;
cb(JSON.parse(body));
});
}
// get a single story and parse them into a cb as json
// Example of story
// {
// "by" : "dhouston",
// "descendants" : 71,
// "id" : 8863,
// "kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671, 8940, 9067, 8908, 9055, 8865, 8881, 8872, 8873, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8878, 8870, 8980, 8934, 8876 ],
// "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"
// }
}, {
key: "get_story",
value: function get_story(url, cb) {
_request2.default.get(url, function (err, resp, body) {
if (err) throw err;
cb(JSON.parse(body));
});
}
// get the top stories cycle them into the get_story function
// pass trougth a cb an array of stories
}, {
key: "get_stories",
value: function get_stories(cb) {
var _this = this;
var result = [];
this.get_top_stories_id(function (stories_id) {
_async2.default.each(stories_id, function (story_id, callback) {
var url = _this.story_url + "/" + story_id + ".json";
_this.get_story(url, function (data) {
result.push(data);
callback(null);
});
}, function (err) {
if (err) throw err;
cb(result);
});
});
}
// Get all the stories from the top one's
// And filter them passing only the story with a minimun score
// pass an array of story into the cb
}, {
key: "stories_with_score",
value: function stories_with_score(score, cb) {
var result = [];
this.get_stories(function (stories) {
_async2.default.each(stories, function (story, callback) {
if (story.score >= score) {
result.push(story);
}
callback(null);
}, function (err) {
if (err) throw err;
cb(result);
});
});
}
// takes the API url of mailgun
// sender email
// to email
// subject and body
// execute a cb
// example of email object:
// let email = {
// apikey: '',
// server: '',
// sender: '',
// to: '',
// subject: '',
// body: ''
// };
// send_email(email, cb) {
// let msg = new mailgun.Mailgun(email.apikey);
// msg.sendText(email.sender, email.to,
// email.subject,
// email.body,
// email.server,
// 'noreply@example.com',
// function(err) {
// if (err) {
// console.log(err);
// throw err;
// }
// cb();
// });
// }
}]);
return API;
}();
const api = new API();
api.stories_with_score(100).then(data => console.log(data));
module.exports = API;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc