Skip to content
Permalink
Browse files

πŸŽ‰ Version 2 rewrite

  • Loading branch information...
grikomsn committed Dec 20, 2018
0 parents commit 96b5c6268e87be9bb0285c8b0286c9ee966ab928
Showing with 513 additions and 0 deletions.
  1. +1 βˆ’0 .gitignore
  2. +21 βˆ’0 LICENSE
  3. +124 βˆ’0 README.md
  4. +6 βˆ’0 examples/scrapeReviews.js
  5. +5 βˆ’0 examples/searchByQuery.js
  6. +5 βˆ’0 index.js
  7. +23 βˆ’0 package.json
  8. +11 βˆ’0 src/defaults.js
  9. +68 βˆ’0 src/scrape.js
  10. +51 βˆ’0 src/search.js
  11. +198 βˆ’0 yarn.lock
@@ -0,0 +1 @@
node_modules
21 LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Nine Twenty One

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
124 README.md
@@ -0,0 +1,124 @@
<div align='center'>

# rotten-reviews

Scrape audience reviews from [Rotten Tomatoes][rotten-tomatoes] πŸ…

[![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)][spectrum]

[![Rotten GIF](https://media.giphy.com/media/101t9QwTM6y5oc/giphy.gif)][spectrum]

</div>

## Description

This Node.js package fetches [Rotten Tomatoes][rotten-tomatoes] audience reviews (movies and TV shows) by scraping pages using [`cheerio`][cheerio] and [`isomorphic-fetch`][isomorphic-fetch]. Notable features such as:

- Search movie or TV show titles
- Fetch whole audience reviews or by defined value

Note that this package doesn't use official packages or API endpoints provided by RT.

## Usage

### Search titles using [`searchByQuery`](examples/searchByQuery.js)

- Script

```js
const { searchByQuery } = require('rotten-reviews')
searchByQuery('venom').then(results => {
console.log(JSON.stringify(results, null, 2))
})
```

- Output (truncated, fetched on December 20, 2018)

```json
[
{
"title": "Venom",
"year": 2018,
"type": "m",
"slug": "m/venom_2018"
},
{
"title": "Venom",
"year": 2016,
"type": "m",
"slug": "m/venom"
},
{
"title": "Venom",
"year": 2013,
"type": "m",
"slug": "m/venom_2013"
},
...
]
```

### Fetch reviews using [`scrapeReviews`](examples/scrapeReviews.js)

- Script

```js
const { scrapeReviews } = require('rotten-reviews')
const slug = 'm/venom_2018'
scrapeReviews(slug).then(results => {
console.log(JSON.stringify(results, null, 2))
})
```

- Output (truncated, fetched on December 20, 2018)

```json
[
{
"reviewer": "Cory L",
"date": "December 19, 2018",
"stars": 4,
"review": "After watching the movie I gotta say it was good. It doesn't need spider Man though I would love to see that interaction. I am looking forward to seeing a sequel."
},
{
"reviewer": "Shawn R",
"date": "December 19, 2018",
"stars": 4,
"review": "Critics be damned, I liked it. Lots of action, and it doesn't take itself too seriously. Venom was given a bit of personality rather than being a glorified sticky suit."
},
{
"reviewer": "Ricardo B",
"date": "December 19, 2018",
"stars": 5,
"review": "Great movie! Eddie was a very relatable character and his relationship with Venom is unique. The film was entertaining throughout. I Can't believe themovie did poorly on rotten tomatoes... I like how Eddie was all over the place with his new identity. Hero or villain, Venom was great and I can't wait for him to eat more heads off in the future. The new Spiderman and Venom would be hysterical together."
},
...
]
```

### CLI usage

Starting from version 2, `rotten-reviews` does not include any runnables. Instead, we made [`rotten-reviews-cli`][rotten-reviews-cli] which is a CLI wrapper for `rotten-reviews`.

## API

Haven't done this part. Do [submit a pull request](https://github.com/ninetwenty-one/rotten-reviews/compare) if you want to contribute.

## Credits

- [`cheeriojs/cheerio`][cheerio]
- [`matthew-andrews/isomorphic-fetch`][isomorphic-fetch]
- [`kevva/strict-uri-encode`][strict-uri-encode]

## License

MIT

[cheerio]: https://github.com/cheeriojs/cheerio
[isomorphic-fetch]: https://github.com/matthew-andrews/isomorphic-fetch
[rotten-reviews-cli]: https://github.com/ninetwenty-one/rotten-reviews-cli
[rotten-tomatoes]: https://www.rottentomatoes.com
[spectrum]: https://spectrum.chat/ninetwenty-one/rotten-reviews
[strict-uri-encode]: https://github.com/kevva/strict-uri-encode
@@ -0,0 +1,6 @@
const { scrapeReviews } = require('..')
const slug = 'm/venom_2018'

scrapeReviews(slug).then(results => {
console.log(JSON.stringify(results, null, 2))
})
@@ -0,0 +1,5 @@
const { searchByQuery } = require('..')

searchByQuery('venom').then(results => {
console.log(JSON.stringify(results, null, 2))
})
@@ -0,0 +1,5 @@
module.exports = {
...require('./src/defaults'),
...require('./src/scrape'),
...require('./src/search'),
}
@@ -0,0 +1,23 @@
{
"name": "rotten-reviews",
"description": "Scrape audience reviews from Rotten Tomatoes πŸ…",
"version": "2.0.0",
"repository": "https://github.com/ninetwenty-one/rotten-reviews.git",
"author": "Griko Nibras <griko@ninetwenty.one> (https://ninetwenty.one)",
"main": "index.js",
"keywords": [
"rotten-reviews",
"rotten-tomatoes",
"scrape-audience-reviews",
"scraper",
"scraping-websites",
"scraping"
],
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"isomorphic-fetch": "^2.2.1",
"strict-uri-encode": "^2.0.0"
},
"devDependencies": {},
"license": "MIT"
}
@@ -0,0 +1,11 @@
const BASE_URL = 'https://www.rottentomatoes.com'
const MAX_REVIEWS_PER_PAGE = 20
const MAX_VISITABLE_PAGE = 51
const MAX_TOTAL_REVIEWS = MAX_VISITABLE_PAGE * MAX_REVIEWS_PER_PAGE

module.exports = {
BASE_URL,
MAX_REVIEWS_PER_PAGE,
MAX_VISITABLE_PAGE,
MAX_TOTAL_REVIEWS,
}
@@ -0,0 +1,68 @@
const {
MAX_REVIEWS_PER_PAGE,
MAX_TOTAL_REVIEWS,
MAX_VISITABLE_PAGE,
} = require('./defaults')

const createUrlFromSlug = (slug, pageNumber = 1) => {
const baseUrl = 'https://www.rottentomatoes.com'
const page = Math.min(Math.max(1, pageNumber), MAX_VISITABLE_PAGE)

return `${baseUrl}/${slug}/reviews/?page=${page}&type=user&sort=`
}

const scrapeFromPageUrl = async url => {
/** @type {{reviewer: String, date: String, stars: Number, review: String}[]} */
const reviews = []

const res = await require('isomorphic-fetch')(url)
const $ = require('cheerio').load(await res.text())

$('.review_table_row').each((i, element) => {
const stars = $(element).find('.glyphicon.glyphicon-star').length
const hasHalfStar = $(element).find('span:contains("Β½")').length ? 0.5 : 0

const [reviewer, date, review] = [
'.bold.unstyled.articleLink',
'.fr.small.subtle',
'.user_review',
].map(classes =>
$(element)
.find(classes)
.text()
.trim()
)

reviews.push({
reviewer,
date,
stars: stars + hasHalfStar,
review,
})
})

return reviews
}

const scrapeReviews = async (slug, reviewCount = MAX_REVIEWS_PER_PAGE) => {
const desiredReviewCount = Math.max(
MAX_REVIEWS_PER_PAGE,
Math.min(reviewCount, MAX_TOTAL_REVIEWS)
)
const pageCount = Math.ceil(desiredReviewCount / MAX_REVIEWS_PER_PAGE)

const pagesReviews = await Promise.all(
Array.from(Array(pageCount), (x, i) => {
const url = createUrlFromSlug(slug, i)
return scrapeFromPageUrl(url)
})
)

return pagesReviews.reduce((a, b) => a.concat(b)).slice(0, reviewCount)
}

module.exports = {
createUrlFromSlug,
scrapeFromPageUrl,
scrapeReviews,
}
@@ -0,0 +1,51 @@
const createSearchUrl = query => {
const { BASE_URL } = require('./defaults')
const encode = require('strict-uri-encode')

return `${BASE_URL}/api/private/v2.0/search?q=${encode(query)}`
}

const searchByQuery = async query => {
const fetch = require('isomorphic-fetch')
const res = await fetch(createSearchUrl(query))

/** @type {{movies: Object[], tvSeries: Object[]}} */
const { movies, tvSeries } = await res.json()

/**
* @param {{year: Number}} a
* @param {{year: Number}} b
*/
const sortByYear = (a, b) => {
if (a.year > b.year) {
return -1
}
if (a.year < b.year) {
return 1
}
return 0
}

/** @type {{title: String, year: Number, type: String, slug: String}[]} */
const movieResults = movies.map(({ name, year, url }) => ({
title: name,
year,
type: url.split('/')[1],
slug: url.substring(1),
}))

/** @type {{title: String, year: Number, type: String, slug: String}[]} */
const tvResults = tvSeries.map(({ title, startYear, url }) => ({
title,
year: startYear,
type: url.split('/')[1],
slug: url.substring(1),
}))

return movieResults.concat(tvResults).sort(sortByYear)
}

module.exports = {
createSearchUrl,
searchByQuery,
}

0 comments on commit 96b5c62

Please sign in to comment.
You can’t perform that action at this time.