Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bansicloud/cc976411da744b54177796e4c345c263 to your computer and use it in GitHub Desktop.

Select an option

Save bansicloud/cc976411da744b54177796e4c345c263 to your computer and use it in GitHub Desktop.

Revisions

  1. @reichert621 reichert621 revised this gist Jun 1, 2020. No changes.
  2. @reichert621 reichert621 revised this gist May 30, 2020. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions yc-jobs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const moment = require('moment');
    const request = require('superagent');
    const cheerio = require('cheerio');
    const _ = require('lodash');

    const scraper = async () => {
    const url = 'https://news.ycombinator.com/jobs'
    const {text: html} = await request.get(url);
    const $ = cheerio.load(html);
    const links = $('.storylink').map((i, el) => {
    return {href: $(el).attr('href'), text: $(el).text()};
    });
    const ages = $('.age').map((i, el) => $(el).text());
    const formatted = _.zip(links, ages).map(([link, age]) => {
    return {
    text: link.text,
    link: link.href,
    age: age,
    };
    });
    const recent = formatted.filter((item) => {
    // item.age looks like "16 hours ago", "2 days ago", etc.
    const [num, time] = item.age.split(' ');
    const threshold = moment().subtract(24, 'hours');

    return moment().subtract(num, time) > threshold;
    });

    return recent;
    };

    scraper().then(console.log).catch(console.log);
  3. @reichert621 reichert621 revised this gist May 30, 2020. 4 changed files with 57 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions check-item-in-stock.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    const request = require('superagent');

    const checkItemInStock = async () => {
    const {text: html} = await request.get(
    'https://www.bowflex.com/selecttech/552/100131.html'
    );
    const inStock = html.toLowerCase().indexOf('out of stock') === -1;

    return {inStock};
    };

    checkItemInStock().then(console.log).catch(console.log);
    25 changes: 25 additions & 0 deletions hacker-news.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    const request = require('superagent');
    const cheerio = require('cheerio');
    const _ = require('lodash');

    const scraper = async (threshold = 200) => {
    const url = 'https://news.ycombinator.com/'
    const {text: html} = await request.get(url);
    const $ = cheerio.load(html);
    const links = $('.storylink').map((i, el) => {
    return {href: $(el).attr('href'), text: $(el).text()};
    });
    const scores = $('.score').map((i, el) => $(el).text());
    const formatted = _.zip(links, scores).map(([link, score]) => {
    return {
    text: link.text,
    link: link.href,
    score: score && Number(score.replace(/\D/g, '')),
    };
    });
    const top = formatted.filter((item) => item.score > threshold);

    return top;
    };

    scraper().then(console.log).catch(console.log);
    11 changes: 11 additions & 0 deletions inspirational-quote.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    const request = require('superagent');
    const _ = require('lodash');

    const getRandomInspirationalQuote = async () => {
    const {text: json} = await request.get('https://type.fit/api/quotes');
    const quotes = JSON.parse(json);

    return _.sample(quotes);
    };

    getRandomInspirationalQuote().then(console.log).catch(console.log);
    9 changes: 9 additions & 0 deletions ping-server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const request = require('superagent');

    const ping = async () => {
    const res = await request.get('https://taro-beta.herokuapp.com/api/ping');

    return res.body;
    };

    ping().then(console.log).catch(console.log);
  4. @reichert621 reichert621 created this gist May 30, 2020.
    35 changes: 35 additions & 0 deletions reddit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    const request = require('superagent');
    const _ = require('lodash');

    const getTopPosts = async (subreddit, options = {}) => {
    const {count = 10, interval = 'week'} = options;
    const sub = `https://www.reddit.com/r/${subreddit}/top.json?sort=top&t=${interval}`;
    const res = await request.get(sub);
    const {children: posts} = res.body.data;

    return posts.slice(0, count).map((post) => {
    const {title, subreddit, score, url} = post.data;

    return {title, subreddit, score, url};
    });
    };

    const getTopPostsInFavoriteSubreddits = async (
    subreddits,
    options = {}
    ) => {
    const {count = 5, interval = 'week'} = options;
    const promises = subreddits.map((sub) => {
    return getTopPosts(sub, {count, interval});
    });

    return Promise.all(promises).then(_.flatten);
    };

    getTopPostsInFavoriteSubreddits([
    'programming',
    'javascript',
    'learnprogramming',
    ])
    .then(console.log)
    .catch(console.log);