Skip to content

Instantly share code, notes, and snippets.

@sonhanguyen
Last active April 1, 2017 11:54
Show Gist options
  • Save sonhanguyen/2a4fdd2e036f86eb0bd0663bc9ba8810 to your computer and use it in GitHub Desktop.
Save sonhanguyen/2a4fdd2e036f86eb0bd0663bc9ba8810 to your computer and use it in GitHub Desktop.
_ = require 'lodash'
imdb = require 'imdb-api'
rarbg = require 'rarbgto-api'
fetch = require 'isomorphic-fetch'
cache = require('memoize-fs') cachePath: './memoize-fs'
memoize = ({ store , invalidator }, func) ->
options = { cacheId: store, noBody: true }
atempt = ->
result = func arguments...
# if miss and the value is invalid, throw so that it won't be cached
return result unless invalidator? arguments, result
throw result
(args...) ->
fromCache = (fn, force) ->
cache.fn fn, Object.assign { force }, options
.then (f) -> f args...
fromCache atempt
.then (result) ->
return result unless invalidator? args, result
# if hit and the value is invalid, force re-evaluation
fromCache func, invalidate = true
.catch _.identity # return invalid value from atempt
getList = memoize store: 'getList',
(url) ->
fetch 'http://gdom.graphene-python.org/graphql',
method: 'POST'
headers: 'Content-Type': 'application/json'
body: JSON.stringify
query: """
{
page(url: "#{url}") {
titles: query(selector: ".post-content strong") { text }
}
}
"""
.then _.method 'json'
getImdbInfo = memoize store: 'imdb', imdb.get
getDownloadLinks = memoize store: 'rarbg', rarbg.search
getMovieInfo = (title) ->
Promise.all [ getImdbInfo(title), getDownloadLinks(title) ]
.then ([imdb, rarbg]) -> { imdb, rarbg }
module.exports = getList 'http://screenanarchy.com/2017/03/hong-kong-goes-west---when-hong-kong-film-makers-attempt-to-break-the-western-market---part-6-contrib'
.then ({ data } ) ->
_(data.page.titles)
.map 'text'
.uniq()
.map getMovieInfo
.thru Promise.all.bind Promise
.value()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment