From 8308ef49bf49a1e06c29155b975325dc742df460 Mon Sep 17 00:00:00 2001 From: Hugo Gomez Date: Sat, 9 Nov 2019 14:15:38 -0600 Subject: [PATCH] :recycle: Better code lecture. Use of functions, separate files for better code read. --- cinepolis.js | 52 +++++++++++++++++++++++++ index.js | 107 +++++++++------------------------------------------ pick.js | 50 ++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 88 deletions(-) create mode 100644 cinepolis.js create mode 100644 pick.js diff --git a/cinepolis.js b/cinepolis.js new file mode 100644 index 0000000..e1a1666 --- /dev/null +++ b/cinepolis.js @@ -0,0 +1,52 @@ +const got = require("got"); + +exports.getCities = async () => { + const url = + "https://cinepolis.com/manejadores/CiudadesComplejos.ashx?EsVIP=false"; + const { body } = await got.post(url, { json: true }); + const cities = []; + + body.map(value => { + let city = {}; + city.message = value.Nombre; + city.name = value.Clave; + city.value = value.Clave; + cities.push(city); + }); + + return cities; +}; + +exports.getShowtimes = async city => { + const url = "https://cinepolis.com/Cartelera.aspx/GetNowPlayingByCity"; + const body = { claveCiudad: city, esVIP: false }; + const options = { body, json: true }; + const response = await got.post(url, options); + const showtimes = response.body.d; + const locations = showtimes.Locations; + return { showtimes, locations }; +}; + +exports.getDates = (cinemas, selectedCinema) => { + const [cinema] = cinemas.filter(value => value.Key === selectedCinema.cinema); + return cinema.Dates; +}; + +exports.getCinemas = async locations => { + const values = Object.keys(locations); + const cinemas = []; + values.forEach(value => { + let o = {}; + o.value = value; + o.message = locations[value]; + cinemas.push(o); + }); + + return cinemas; +}; + +exports.getMovies = (selectedDate, dates) => { + const [moviesArray] = dates.filter(v => v.ShowtimeDate === selectedDate); + + return movies; +}; diff --git a/index.js b/index.js index f346b9a..dfa63f4 100644 --- a/index.js +++ b/index.js @@ -1,92 +1,23 @@ #!/usr/bin/env node - -const got = require("got"); -const { prompt } = require('enquirer'); -const c = require('ansi-colors'); -const log = console.log; -const humanizeString = require('humanize-string'); -const urlCities = "https://cinepolis.com/manejadores/CiudadesComplejos.ashx?EsVIP=false"; -const url = "https://cinepolis.com/Cartelera.aspx/GetNowPlayingByCity"; +const c = require("ansi-colors"); +const cinepolis = require("./cinepolis"); +const pick = require("./pick"); (async () => { - const citiesResponse = await got.post(urlCities, { json: true }); - - // <- ciudades - const citiesArray = []; - citiesResponse.body.map(value => { - let tempCities = {}; - tempCities.message = value.Nombre; - tempCities.name = value.Clave; - tempCities.value = value.Clave; - citiesArray.push(tempCities); - }); - - const citiesQuestions = { - type: "autocomplete", - name: "city", - message: "Selecciona una ciudad", - limit: 10, - choices: citiesArray, - format: value => humanizeString(value) - }; - - const citiesAnswer = await prompt(citiesQuestions); - const params = { claveCiudad: citiesAnswer.city, esVIP: false }; - const response = await got.post(url, { body: params, json: true }); - const responseBody = response.body.d; - const cinemasObject = responseBody.Locations; - const values = Object.keys(cinemasObject); - - const cinemas = []; - values.forEach(value => { - let o = {}; - o.value = value; - o.message = cinemasObject[value]; - cinemas.push(o); - }); - - // <- cines - const cinemasQuestions = { - type: "select", - name: "cinema", - message: "Selecciona un cine", - choices: cinemas, - format: value => humanizeString(value) - }; - - const cinemasAnswer = await prompt(cinemasQuestions); - const [cinema] = responseBody.Cinemas.filter(value => value.Key === cinemasAnswer.cinema); - const datesArray = cinema.Dates.map(value => value.ShowtimeDate); - - // <- fechas - const datesQuestions = { - type: "select", - name: "date", - message: "Seleccciona un día", - choices: datesArray - }; - - const datesAnswer = await prompt(datesQuestions); - const [moviesArray] = cinema.Dates.filter( - value => value.ShowtimeDate === datesAnswer.date - ); - const movies = moviesArray.Movies.map(value => value.Title); - - // <- películas - const moviesQuestions = { - type: "select", - name: "movie", - message: "Seleccciona un película", - pageSize: 20, - choices: movies - }; - - const moviesAnswer = await prompt(moviesQuestions); - const [formatsArray] = moviesArray.Movies.filter( - value => value.Title === moviesAnswer.movie - ); - const [showTimesArray] = formatsArray.Formats; - const showTimes = showTimesArray.Showtimes.map(value => value.ShowtimeAMPM); - log(c.cyanBright(" ·"), showTimes.join(c.cyanBright(" · "))); - + const cities = await cinepolis.getCities(); + const { city } = await pick.city(cities); // <- selecciona una ciudad + const { showtimes, locations } = await cinepolis.getShowtimes(city); + const cinemas = await cinepolis.getCinemas(locations); + const cinemasAnswer = await pick.cinema(cinemas); // <- selecciona un cine + const dates = cinepolis.getDates(showtimes.Cinemas, cinemasAnswer); + const datesAnswer = await pick.date(dates.map(v => v.ShowtimeDate)); // <- selecciona una fecha + + const [{ Movies }] = dates.filter(v => v.ShowtimeDate === datesAnswer.date); + const movies = Movies.map(v => v.Title); + const { movie } = await pick.movie(movies); // <- selecciona una película + + const [{ Formats }] = Movies.filter(v => v.Title === movie); + const [{ Showtimes }] = Formats; + const showTimes = Showtimes.map(v => v.ShowtimeAMPM); + console.log(c.cyanBright(" ·"), showTimes.join(c.cyanBright(" · "))); // <- imprime los horarios })(); diff --git a/pick.js b/pick.js new file mode 100644 index 0000000..ad5d4fc --- /dev/null +++ b/pick.js @@ -0,0 +1,50 @@ +const humanizeString = require("humanize-string"); +const { prompt } = require("enquirer"); + +exports.city = async cities => { + const citiesQuestions = { + type: "autocomplete", + name: "city", + message: "Selecciona una ciudad", + limit: 10, + choices: cities, + format: value => humanizeString(value) + }; + + return prompt(citiesQuestions); +}; + +exports.cinema = async cinemas => { + const cinemasQuestions = { + type: "select", + name: "cinema", + message: "Selecciona un cine", + choices: cinemas, + format: value => humanizeString(value) + }; + + return prompt(cinemasQuestions); +}; + +exports.date = async dates => { + const datesQuestions = { + type: "select", + name: "date", + message: "Seleccciona un día", + choices: dates + }; + + return prompt(datesQuestions); +} + +exports.movie = async movies => { + const moviesQuestions = { + type: "select", + name: "movie", + message: "Seleccciona un película", + pageSize: 20, + choices: movies + }; + + return prompt(moviesQuestions); +} \ No newline at end of file