-
-
Save bansicloud/cc976411da744b54177796e4c345c263 to your computer and use it in GitHub Desktop.
Taro examples — https://www.gettaro.com/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment