Skip to content

Instantly share code, notes, and snippets.

@tannerwelsh
Last active July 10, 2017 23:16
Show Gist options
  • Save tannerwelsh/008bd8480a011dc24260430d7ee06ae0 to your computer and use it in GitHub Desktop.
Save tannerwelsh/008bd8480a011dc24260430d7ee06ae0 to your computer and use it in GitHub Desktop.
const rp = require('request-promise')
const cheerio = require('cheerio')
const buildURL = (searchQuery) => {
return `http://www.imdb.com/find?q=${searchQuery}&s=tt&ref_=fn_al_tt_mr`
}
const parseResults = ($) => {
return $('td.result_text')
.map((i, el) => $(el).text().replace(/ - /g, "\n - "))
.get()
}
const loadWithCheerio = (body) => cheerio.load(body)
const searchIMDB = (query) => {
const uri = buildURL(query)
const options = { uri, transform: loadWithCheerio }
return rp(options).then(parseResults)
}
if (!module.parent) {
const queryTerm = process.argv[2]
searchIMDB(queryTerm).then((results) => {
console.log(results.join("\n"))
})
}
{
"name": "movie-search-cli",
"description": "Search for movies on IMDB w/ CLI",
"main": "imdb.js",
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"request": "^2.81.0",
"request-promise": "^4.2.1"
},
"author": "Tanner Welsh"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment