UNPKG

2.06 kBtext/coffeescriptView Raw
1cheerio = require 'cheerio'
2request = require 'request'
3async = require 'async'
4{ waterfall } = async
5{ map, compact } = require 'underscore'
6details = require './details'
7
8class Cineteca
9
10 constructor: ({entry, today} = {}) ->
11 @entry_url = entry or 'http://www.cinetecanacional.net'
12 @today_path = today or '/controlador.php?opcion=carteleraDia'
13
14 get: (url, cb) ->
15 request { url, encoding: 'binary' }, (err, res, body) ->
16 if res and res.statusCode
17 unless 200 <= res.statusCode < 300 or res.statusCode is 304
18 err = new Error "Response status code was #{res.statusCode} successful"
19 cb err, body
20
21 # movie_urls()
22 # movie_urls() loads the initial showtimes
23 # for the day page, it then passes control
24 # to movie_details() to extract information
25 # about individual movies.
26 #
27 # @param <String> url: Starting point to load
28 # @param <Function> callback: Function to call
29 # when finished
30 movie_urls: (url, cb) =>
31 urls = []
32 @get url, (err, body) ->
33 return cb err if err
34 $ = cheerio.load body
35 cb null, map $('[id=botonVer]'), (btn) ->
36 "/" + $(btn).attr('onclick').split('\'')[1]
37
38 # movie_details()
39 # movie_details() loads the passed movie url
40 # and extracts it's details, including title,
41 # trailer url, synopsis, etc.
42 #
43 # @param <String> url: The movie url to load
44 # @param <Function> callback: Function to call
45 # when finished
46 movie_details: (url, cb) =>
47 url = @entry_url + url
48 @get url, (err, body) ->
49 return cb err if err
50 $ = cheerio.load body
51 cb null, details $, url
52
53 # today(fn) starts the scraping process
54 # to get the movie listings for the day
55 # The callback is called with an array of
56 # movie listings for the day when complete.
57 #
58 # @param callback
59 today: (cb) =>
60 waterfall [
61 (cb) => @movie_urls @entry_url + @today_path, cb
62 (urls, cb) => async.map urls, @movie_details, cb
63 (movies, cb) => cb null, compact movies
64 ], cb
65
66
67module.exports = (opts) -> new Cineteca(opts)
\No newline at end of file