Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions movies.json

Large diffs are not rendered by default.

681 changes: 590 additions & 91 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
"type": "git",
"url": "https://github.com/assembler-school/node-moviedb-cli.git"
},
"scripts": {},
"scripts": {
"start": "set NODE_ENV=development && node src/moviedb.js"
},
"dependencies": {
"chalk": "^4.1.0",
"chalk": "^4.1.2",
"commander": "^6.1.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"node-notifier": "^8.0.0",
"ora": "^5.1.0"
"ora": "^5.4.1"
},
"devDependencies": {
"dotenv": "^10.0.0"
}
}
1 change: 1 addition & 0 deletions persons.json

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions render/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const log = console.log;
const chalk = require('chalk');

function renderMovies(page, obj) {
const items = obj.results
console.log(`\n${chalk.white('Movie:')}`);
items.forEach(el => {

log(`ID: ${chalk.white(el.id)} `);
log(`Title: ${chalk.bold.blue(el.original_title)} `);
log(`Release Date: ${chalk.white(el.release_date)} `);

if (el.known_for_department) {
log(`${chalk.white(`Department:${chalk.magenta(el.known_for_department)}`)} `);
}
});

log(chalk.white(`\n\n----------------------------------------`));
log(`Page: ${chalk.white(page)} of: ${chalk.white(obj.total_pages)} `);

log(chalk.white(`--------------------------------------------- `));
}

module.exports = {
renderMovies,
}
47 changes: 47 additions & 0 deletions render/person.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const chalk = require('chalk');

function renderPerson(obj) {
console.log(chalk.white("\n----------------------------------------"))
let aggregateString = chalk.white(`\nID: ${obj.id}`)
aggregateString += chalk.white(`\nName: ${chalk.bold.blue(obj.name)}`)
aggregateString += chalk.white(`\nBirthday: ${obj.birthday} ${chalk.grey('|')} ${chalk.white(obj.place_of_birth)}`)
aggregateString += obj.known_for_department === "Acting" ? chalk.white(`\nDepartment: ${chalk.magenta(obj.known_for_department)}`) : "";
aggregateString += chalk.white(`\nBiography: ${chalk.bold.blue(obj.biography)}`);

if (obj.also_known_as) {
aggregateString += chalk.white(`\nAlso known as:\n`);
obj.also_known_as.forEach(element => {
aggregateString += chalk.white(`\n${element}`);
});

} else {
aggregateString += chalk.white(`\n${obj.name} doesn't have any alternate names\n`)
}
return aggregateString;
}


const log = console.log;

function renderData(page, obj) {
const items = obj.results
items.forEach(el => {

log(`Name: ${chalk.blue.bold(el.name)}`);
log(`ID: ${chalk.white(el.id)}`);
if (el.known_for_department) {
log(`${chalk.white(`Department:${chalk.magenta(el.known_for_department)}`)}`);
}
});

log(chalk.white(`\n\n----------------------------------------`));
log(`Page: ${chalk.white(page)} of: ${chalk.white(obj.total_pages)}`);

log(chalk.white(`---------------------------------------------`));

}

module.exports = {
renderPerson,
renderData,
}
64 changes: 64 additions & 0 deletions src/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env node
//-----------------------------------requires--------------------------------------------------------
const { Command, helpOption, option } = require("commander");
const { connectApi } = require("./methods");
const { getPerson } = require("./methods");
const { getMovies } = require("./methods");
const { load } = require("./saveload");
//----------------------------------- ########--------------------------------------------------------
const program = new Command();

program
.version("0.0.1")
.description("moviedb-cli")

program
.command("get-persons <page>")
.requiredOption('-p, --popular', 'Fetch the popular persons')
.requiredOption('--page', 'Fetch the popular persons')
.description("Make a network request to fetch most popular persons")
.option('-s, --save', 'Save the result to a file')
.option('-l, --load', 'Load the result from a file')
.action((page, options) => {
if (options.load) {
load('persons.json')
} else {
connectApi(page, options)
}
});


program

.command("get-person")

.description("Make a network request to fetch the data of a single person")

.requiredOption("-i, --id <id>", "The id of the person")

.action((options) => {
getPerson(options.id);
});




program

.command("get-movies <page>")
.description("Make a network request to fetch most popular movies")
.requiredOption('--page', 'Fetch the popular movies')
.option('-p, --popular', 'Fetch the popular movies')
.option('-n, --now-playing', 'Fetch the now playing movies')
.option('-s, --save', 'Save the movies to a file')
.option('-l, --local', 'Fetch the movies from a local file')
.action((page, options) => {
if (options.local) {
load('movies.json');
}
else {
getMovies(page, options);
}
});

program.parse(process.argv);
108 changes: 108 additions & 0 deletions src/methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//-----------------------------------required-------------------------------------------------------
require('dotenv').config();
const ora = require('ora');
const https = require('https');
const testRender = require('../render/person');
const moviesRender = require('../render/movies');
const saveData = require('./saveload');
const { options } = require('node-notifier');
// -----------------------------------constants-------------------------------------------------------
const BASE_URL = 'https://api.themoviedb.org/3/';
const apiKey = process.env.API_KEY;
const API_KEY = `api_key=${apiKey}`;
//------------------------------------#########-----------------------------------------------------

function connectApi(page, options) {
const url = `https://api.themoviedb.org/3/person/popular?api_key=${apiKey}&page=${page}`;
const req = https.request(url, (res) => {
const spinner = ora("Fetch the popular persons...").start();
let data = "";
res.setEncoding('utf8');
res.on("data", (d) => {
data += d;
});
res.on("end", () => {
let obj = JSON.parse(data);
if (options.save) saveData.savePersons(obj);
testRender.renderData(page, obj)
})
spinner.succeed("completed")
})
req.end()

}

function getPerson(id) {
const requestURL = `${BASE_URL}person/${id}?${API_KEY}`;

const req = https.request(requestURL, (res) => {
const spinner = ora(' Fetching the person data...').start();
res.setEncoding('utf8');
res.on("data", (d) => {
const obj = JSON.parse(d);
// console.log(obj);
console.log(testRender.renderPerson(obj));
spinner.succeed('Successfully fetched data');
});
// spinner.stop();
}).end();
req.on("error", (e) => {
ora.fail(e.message);
})
};

function getMovies(page, options) {

let requestURL = `${BASE_URL}movie/popular?${API_KEY}&page=${page}`;
if (options.nowPlaying) requestURL = `${BASE_URL}movie/now_playing?${API_KEY}&page=${page}`;
const req = https.request(requestURL, (res) => {
const spinner = ora('Fetching the movies...').start();
res.setEncoding('utf8');
let data = '';
res.on("data", (d) => {
data += d;
// const obj = JSON.parse(d);
// moviesRender.renderMovies(page, obj);
// spinner.succeed('Successfully fetched data');
});
res.on("end", () => {
const obj = JSON.parse(data);
moviesRender.renderMovies(page, obj);
if (options.save) saveData.saveMovies(obj);
spinner.succeed('Successfully fetched data');
})
spinner.stop();
}).end();
req.on("error", (e) => {
ora.fail(e.message);
})
}


// function connectApi(page) {
// const url = `https://api.themoviedb.org/3/person/popular?api_key=${apiKey}&page=${page}`;
// const req = https.request(url, (res) => {
// const spinner = ora("Fetch the popular persons...").start();
// let data = "";
// res.setEncoding('utf8');
// res.on("data", (d) => {
// data += d;
// });
// res.on("end", () => {
// let obj = JSON.parse(data);
// testRender.renderData(page, obj)
// })
// spinner.succeed("completed")
// })
// req.end()






module.exports = {
connectApi,
getPerson,
getMovies,
}
38 changes: 0 additions & 38 deletions src/moviedb.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/saveload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require("fs");
const path = require("path");


function savePersons(data) {
const filePath = 'persons.json';
fs.writeFileSync(filePath, JSON.stringify(data), 'utf8');
}

function saveMovies(data) {
const filePath = 'movies.json';
fs.writeFileSync(filePath, JSON.stringify(data), 'utf8');
}


function load(file) {

const data = fs.readFileSync(file, "utf8");
console.log(JSON.parse(data));
}

module.exports = {
savePersons,
saveMovies,
load,
}