Skip to content

Instantly share code, notes, and snippets.

@tusharmath
Created August 15, 2017 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tusharmath/ef979bc45d04a0911c26dcc493df0e70 to your computer and use it in GitHub Desktop.
Save tusharmath/ef979bc45d04a0911c26dcc493df0e70 to your computer and use it in GitHub Desktop.
Scrapes all the download urls of color sublime themes
const R = require('ramda')
const O = require('observable-air')
const axios = require('axios')
const { JSDOM } = require('jsdom')
const makeRequest = url =>
axios.get(url).then(response => response.data).then(html => new JSDOM(html))
const fetchDOM = url => O.multicast(O.fromPromise(() => makeRequest(url)))
const fromArray = R.compose(O.fromArray, Array.from)
const findE = R.curry((sel, dom) =>
fromArray(dom.window.document.querySelectorAll(sel))
)
const findDownloadURLS = R.compose(
O.map(R.prop('href')),
O.join,
O.map(findE('.download'))
)
const findPages = R.compose(
O.filter(R.test(/http/)),
O.map(R.prop('href')),
O.join,
O.map(findE('.pagination a'))
)
const unique = (def, fn) => {
const set = new Set()
return p => {
if (set.has(p)) return def()
set.add(p)
return fn(p)
}
}
const main = unique(O.empty, url => {
const dom$ = fetchDOM(url)
const paginationURL$ = findPages(dom$)
const downloadURL$ = findDownloadURLS(dom$)
return O.merge(downloadURL$, O.flatMap(main, paginationURL$))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment