Created
October 6, 2018 03:20
-
-
Save rizdaprasetya/e77f031f7e61a35e55d5b3471f0c6d72 to your computer and use it in GitHub Desktop.
IMDB movie to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Go to movie page in IMDB using browser. | |
Paste this code to Browser JS console to turn movie page to JSON of the movie | |
JSON compatible with: https://github.com/jsonmc/jsonmc */ | |
var ImdbMovieParser = (function(){ | |
var parsePageForMovie = function(){ | |
var nameAndYear = document.querySelector("[class='title_wrapper']").children[0].innerText.split('('); | |
var name = nameAndYear[0].trim(); | |
var year = nameAndYear[1].split(')')[0]; | |
year = parseInt(year); | |
var runtime = document.querySelectorAll('time')[1].innerText.match(/\d+/g)[0]; | |
runtime = parseInt(runtime); | |
var categoriesAndDate = Array.from( | |
document.querySelectorAll('[class=subtext]')[0] | |
.querySelectorAll('a') | |
); | |
var categories = categoriesAndDate | |
.slice(0, -1) | |
.map( | |
e=>e.innerText | |
.toLowerCase() | |
.trim() | |
.replace(/\s/g,'') | |
); | |
var release_date = (new Date( | |
categoriesAndDate.pop().innerText.split('(')[0].trim() | |
)) | |
.toISOString().split('T')[0]; | |
var director = document.querySelector("[class='credit_summary_item']").children[1].innerText; | |
var writerParentNode = document.querySelectorAll('[class=credit_summary_item]')[1]; | |
var writerNodes = Array.from(writerParentNode.querySelectorAll('a')); | |
if(writerParentNode.innerText.includes('more credit')){ | |
writerNodes = writerNodes.slice(0, -1); | |
} | |
var writer = writerNodes.map(e=>e.innerText); | |
var actors = Array.from( | |
document.querySelectorAll('[class=cast_list]')[0].querySelectorAll('tr') | |
) | |
.slice(1) | |
.map( | |
e=>e.querySelectorAll('td')[1].innerText | |
); | |
var storyline = document.querySelectorAll('.inline.canwrap')[0].innerText; | |
return { | |
"name" : name, | |
"year" : year, | |
"runtime" : runtime, | |
"categories" : categories, | |
"release-date" : release_date, | |
"director" : director, | |
"writer" : writer, | |
"actors" : actors, | |
"storyline" : storyline | |
}; | |
} | |
var prettyPrintJson = function(movie){ | |
return JSON.stringify(movie, null, 2); | |
} | |
var ImdbMovieParser = { | |
__movie : null, | |
parsePageForMovie : function(){ | |
ImdbMovieParser.__movie = parsePageForMovie(); | |
return ImdbMovieParser; | |
}, | |
getMovie : function(){ | |
return ImdbMovieParser.__movie; | |
}, | |
getMovieFormatted : function(){ | |
return prettyPrintJson(ImdbMovieParser.__movie); | |
}, | |
getFileName : function(){ | |
return (ImdbMovieParser.__movie.name.toLowerCase().replace(/\s/g,'-'))+'.json'; | |
} | |
} | |
return ImdbMovieParser; | |
})(); | |
ImdbMovieParser.parsePageForMovie(); | |
console.log(ImdbMovieParser.getFileName()); | |
console.log(ImdbMovieParser.getMovieFormatted()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment