description and source-codeclass showtimes {
constructor (location, options) {
this.userAgent = 'showtimes (http://github.com/erunion/showtimes)'
this.baseUrl = 'http://google.com/movies'
this.location = location
// Handle available options
if (typeof options === 'undefined') {
options = {}
}
this.date = typeof options.date !== 'undefined' ? options.date : 0
this.lang = typeof options.lang !== 'undefined' ? options.lang : 'en'
this.pageLimit = typeof options.pageLimit !== 'undefined' ? options.pageLimit : 999
}
/**
* Parse and pull back an object of movie theaters for the currently configured location and date.
* @param {string=} query Query string which works as a way to filter the theaters.
* @param {Function} cb Callback function to run after generating an object of theaters.
* @param {number=} [page=1] Paginated page to pull theaters from. Hidden API, and is only used internally.
* @param {object=} [theaters=[]] Currently generated theaters. Hidden API, and is only used internally.
* @return void
*/
getTheaters () {
this.page = 1
this.theaters = []
var query = (typeof arguments[0] !== 'function') ? arguments[0] : null
var cb = (typeof arguments[0] === 'function') ? arguments[0] : arguments[1]
var extraIdx = (typeof arguments[0] === 'function') ? 1 : 2
if (arguments.length > extraIdx) {
this.page = arguments[extraIdx]
this.theaters = arguments[extraIdx + 1]
}
var api = this
this._request({q: query}, cb, (response) => {
if (api.lang === 'tr') {
response = iconv.decode(response, 'latin5')
}
var $ = cheerio.load(response)
if ($('.theater').length === 0) {
cb($('#results').text())
return
}
$('.theater').each((i, theater) => {
var theaterData = api._parseTheater($, $(theater))
if (theaterData.name.length === 0) {
return true
}
api.theaters.push(theaterData)
})
// No pages to paginate, so return the theaters back.
if ($('#navbar td a:contains("Next")').length === 0 || api.page === api.pageLimit) {
cb(null, api.theaters)
return
}
// Use the hidden API of getTheaters to pass in the next page and current theaters.
api.getTheaters(query, cb, ++api.page, api.theaters)
})
}
/**
* Parse and pull back a standardized object for a given movie.
* @param {string} theaterId Theater ID for the theater you want to query. This can be obtained via getTheaters(),
* or getMovies()
* @param {Function} cb Callback function to run after generating a standardized object for this theater.
* @return {void}
*/
getTheater (theaterId, cb) {
var api = this
this._request({tid: theaterId}, cb, (response) => {
var $ = cheerio.load(response)
if (!$('.showtimes')) {
cb($('#results').text())
return
}
var theater = $('.theater')
var theaterData = api._parseTheater($, theater, false, theaterId)
cb(null, theaterData)
})
}
/**
* Parse and pull back an object of movies for the currently configured location and date.
* @param {string=} query Query string which works as a way to filter the movies.
* @param {Function} cb Callback function to run after generating an object of movies.
* @param {number=} [page=1] Paginated page to pull movies from. Hidden API, and is only used internally.
* @param {object=} [movies=[]] Currently generated movies. Hidden API, and is only used internally.
* @return void
*/
getMovies () {
this.page = 1
this.movies = []
var query = (typeof arguments[0] !== 'function') ? arguments[0] : null
var cb = (typeof arguments[0] === 'function') ...