External Data - bommezijn/Dating-Shreks GitHub Wiki
This topic was worked on by Parvin.
External data
Here I will show you my progress of how I was trying to get data from the IMDB database. But end up using something way different. Want to know my process? Then you should read this:
My process
First off, you need to know that I really wanted to work with IMDB because I have been using their services for over three years and wanted to get to know that well. I was looking for some ways to try that out and found a couple of videos about scraping. So I tried it out using puppeteer. This was somewhat a challenge because it wasn't anything I had seen before, but I thought, yeah, whatever. But later on, when I got to an error that I couldn't fix, I got in contact with Nathan to help me out. He told me that puppeteer is an API that literally scrapes data from any site. So it wasn't IMDB like I thought it was.
After that, he told me I should try it with OMDB, an online database for movies (which works as it should). This was way easier, and it felt legit. What OMDB does is: it stores data about a movie in a link. Let's say you want data about The Godfather. You will need to get the link of The Godfather to get the right information. The link will look like this:
http://www.omdbapi.com/?t=The+Godfather&apikey=8f925772
| This is the site. | This is the movie| This is the API key which is connected to your OMDB account
Going to this adress will look like this:
That means that you can now fetch the data from this link and use it anywhere you would like. 🔥🔥
How to install
- You will need to install node-fetch on your project. Do that by typing in the following in your Terminal:
cd YOUR_PROJECT
Then within your project:
npm install node-fetch
- Require this npm package in your index.js like this
const fetch = require('node-fetch');
- Now you'll need the following code: (comments provide extra information)
const getMovie = async (url) => {
// calling a function with asynch and giving it a parameter: url
try {
url = 'http://www.omdbapi.com/?t=' + encodeURI(user.movieChoice1) + '&apikey=8f925772';
// within try ask give the url a value (user.movieChoice1 is the movie choice of the user.)
//(encodeURI means that spaces etc. get filled with %20 or + so the browser can read the link)
const response = await fetch(url);
// fetching the url for the information that's inside it. the await is because it can take a
// while for it has looked up the data (depends on wifi)
const json = await response.json();
// awaiting the response and putting is inside the const json
res.render('succes', {
movie: json,
});
// rendering the succes page with the data of json with so I can use it.
} catch (error) {
console.log(error);
}
// If there was to be an error, it will console log that.
};
- Now the data comes from OMDB good job. If it works. it looks like this (at the end you see the data from OMDB):
Sources
API for Movie Database. (z.d.). Geraadpleegd op 19 juni 2020, van http://www.omdbapi.com
Async function. (2020, 11 mei). Geraadpleegd op 19 juni 2020, van https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
Cardillo, J. (2018, 31 juli). How to add GIFs to your GitHub README - Joe Cardillo. Geraadpleegd op 19 juni 2020, van https://medium.com/@josephcardillo/how-to-add-gifs-to-your-github-readme-89c74da2ce47
Coding Garden with CJ. (2018, 27 februari). Build a Web Scraper with Node.js and cheerio - IMDB Movie Search. Geraadpleegd op 19 juni 2020, van https://www.youtube.com/watch?v=U0btOGPwrIY&t=244s
Fabian Grohs. (2018, 5 november). Web Scraping with Puppeteer in 10 minutes - IMDB Movie Scraping NodeJs. Geraadpleegd op 19 juni 2020, van https://www.youtube.com/watch?v=4q9CNtwdawA&t=486s
Npm: node-fetch. (2019, 16 mei). Geraadpleegd op 19 juni 2020, van https://www.npmjs.com/package/node-fetch
Puppeteer | Tools for Web Developers | . (z.d.). Geraadpleegd op 19 juni 2020, van https://developers.google.com/web/tools/puppeteer