yt-trending-scraper
Advanced tools
Comparing version 1.1.3 to 2.0.0
{ | ||
"name": "yt-trending-scraper", | ||
"version": "1.1.3", | ||
"version": "2.0.0", | ||
"description": "Identifies the currently trending videos on YouTube and returns all trending site information about every video without accessing the YouTube API.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# YouTube Trending Videos Scraper NodeJS Documentation | ||
This NodeJS library scrapes the trending page of YouTube without any API usage. It is developed for and tailored towards easy usage in the [FreeTube](https://github.com/FreeTubeApp/FreeTube-Vue) rewrite but can be used with any other project as well. | ||
This NodeJS library can scrape all available trending pages of YouTube without any API usage. It is developed for and tailored towards easy usage in the [FreeTube](https://github.com/FreeTubeApp/FreeTube-Vue) rewrite but can be used with any other project as well. | ||
@@ -16,10 +16,33 @@ Therefore, this library does not require any API keys, with the attached maximum quotas, but instead might take longer to receive the required data. | ||
## API | ||
**scrape_trending_page(_geoLocation_, _parseCreatorOnRise_)** | ||
**scrape_trending_page(_parameters_)** | ||
Returns a list of objects containing all the information of the trending videos. | ||
The parameters object can contain the following options: | ||
` | ||
geoLocation: alpha2 Country Code, | ||
parseCreatorOnRise: Boolean | ||
page: String | ||
` | ||
__geoLocation__ is an optional parameter to change the country (e.g. JP for Japan) of the trending page. | ||
__parseCreatorOnRise__ is an optional parameter which allows the parser to process any horizontal video list, which usually is a creator on the rise. But this is not always available, so the scraper will process as usual even when the parameter is set to true. Defaults to **false** | ||
__parseCreatorOnRise__ is an optional parameter which allows the parser to process any horizontal video list, which usually is a creator on the rise. But this is not always available, so the scraper will process as usual even when the parameter is set to true. Defaults to **false** | ||
__page__ is an optional parameter which allows to choose one of the 4 trending pages below. | ||
- `default` | ||
- `music` | ||
- `gaming` | ||
- `movies` | ||
### Example usage | ||
```javascript | ||
ytrend.scrape_trending_page('JP', true).then((data) =>{ | ||
const parameters = { | ||
geoLocation: 'JP', | ||
parseCreatorOnRise: false, | ||
page: 'music' | ||
} | ||
ytrend.scrape_trending_page(parameters).then((data) =>{ | ||
console.log(data); | ||
@@ -26,0 +49,0 @@ }).catch((error)=>{ |
const axios = require("axios") | ||
const trending_page = "https://youtube.com/feed/trending" | ||
const trendingPageBase = "https://youtube.com/feed/trending" | ||
const pageAdditions = { | ||
'music': '4gINGgt5dG1hX2NoYXJ0cw%3D%3D', | ||
'gaming': '4gIcGhpnYW1pbmdfY29ycHVzX21vc3RfcG9wdWxhcg%3D%3D', | ||
'movies': '4gIKGgh0cmFpbGVycw%3D%3D' | ||
} | ||
class TrendingRequester { | ||
static async requestTrendingPage(geoLocation = null) { | ||
static async requestTrendingPage(geoLocation = null, page) { | ||
const config = { | ||
@@ -14,7 +19,15 @@ headers: { | ||
try { | ||
const params = {} | ||
if (geoLocation !== null) { | ||
return await axios.get(trending_page+`?persist_gl=1&gl=${geoLocation}`, config) | ||
} else { | ||
return await axios.get(trending_page, config) | ||
params['persist_gl'] = 1 | ||
params['gl'] = geoLocation | ||
} | ||
if (page !== 'default') { | ||
try { | ||
params['bp'] = pageAdditions[page] | ||
} catch (error) { | ||
console.error("Fallback to default trending page because no valid page name was provided:", page) | ||
} | ||
} | ||
return await axios.get(trendingPageBase, {params}) | ||
@@ -21,0 +34,0 @@ } catch (e) { |
@@ -6,4 +6,16 @@ const requester = require("./TrendingRequester") | ||
//starting point | ||
static async scrape_trending_page(geoLocation= null, parseCreatorOnRise=false) { | ||
const request_data = await requester.requestTrendingPage(geoLocation); | ||
static async scrape_trending_page(parameters) { | ||
let geoLocation = null | ||
let page = 'default' | ||
let parseCreatorOnRise = false | ||
if('geoLocation' in parameters) { | ||
geoLocation = parameters.geoLocation | ||
} | ||
if('page' in parameters) { | ||
page = parameters.page | ||
} | ||
if('parseCreatorOnRise' in parameters) { | ||
parseCreatorOnRise = parameters.parseCreatorOnRise | ||
} | ||
const request_data = await requester.requestTrendingPage(geoLocation, page); | ||
return this.parse_new_html(request_data.data, parseCreatorOnRise); | ||
@@ -10,0 +22,0 @@ } |
51549
235
85