Skip to content

Commit

Permalink
♻️ Better code lecture.
Browse files Browse the repository at this point in the history
Use of functions, separate files for better code read.
  • Loading branch information
gomflo committed Nov 9, 2019
1 parent 26f7940 commit 8308ef4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 88 deletions.
52 changes: 52 additions & 0 deletions cinepolis.js
Original file line number Diff line number Diff line change
@@ -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;
};
107 changes: 19 additions & 88 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
})();
50 changes: 50 additions & 0 deletions pick.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8308ef4

Please sign in to comment.