1 | cheerio = require 'cheerio'
|
2 | request = require 'request'
|
3 | async = require 'async'
|
4 | { waterfall } = async
|
5 | { map, compact } = require 'underscore'
|
6 | details = require './details'
|
7 |
|
8 | class 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 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
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 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
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 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
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 |
|
67 | module.exports = (opts) -> new Cineteca(opts) |
\ | No newline at end of file |