Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from GilgusMaximus/master
Browse files Browse the repository at this point in the history
Rework of the channel url request structure and new performance flag
  • Loading branch information
GilgusMaximus authored Apr 12, 2021
2 parents fecbec1 + 87015f0 commit fcbab0e
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 200 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ import ytch from 'yt-channel-info'

## API

**getChannelInfo(channelId)**
**getChannelInfo(channelId, [channelIdType])**

Returns information about a given channel ID.
The optional argument 'channelIdType' can be provided to get faster results and less network requests if the type of channel id is known.
- `0` = Default value used by the module. It will try all url types in the order channel -> user -> name
- `1` = A channel id that is used with `https://www.youtube.com/channel/channelId` urls
- `2` = A user id that is used with `https://www.youtube.com/user/channelId` urls
- `3` = A name id that is used with `https://www.youtube.com/c/channelId` urls

```javascript
const channelId = 'UCXuqSBlHAE6Xw-yeJA0Tunw'

ytch.getChannelInfo(channelId).then((response) => {
ytch.getChannelInfo(channelId, channelIdType).then((response) => {
console.log(response)
}).catch((err) => {
console.log(err)
Expand All @@ -58,23 +63,26 @@ ytch.getChannelInfo(channelId).then((response) => {
continuation: String // Will return null if there are 12 or fewer related channels. Used with getRelatedChannelsMore()
},
allowedRegions: Array[String],
isVerified: Boolean
isVerified: Boolean,
channelIdType: Number,
}
```

**getChannelVideos(channelId, [sortBy])**
**getChannelVideos(channelId, [sortBy], [channelIdType])**

Grabs videos from a given channel ID.

- `newest` - Grabs videos from a channel sorted by newest / most recently uploaded (Default option if none given)
- `oldest` - Grabs videos from a channel sorted by oldest videos
- `popular` - Grabs videos from a channel sorted by the most popular (Highest amount of views)


- `channelIdType` defined as for `getChannelInfo()`
```javascript
const channelId = 'UCXuqSBlHAE6Xw-yeJA0Tunw'
const sortBy = 'newest'

ytch.getChannelVideos(channelId, sortBy).then((response) => {
ytch.getChannelVideos(channelId, sortBy, channelIdType).then((response) => {
console.log(response)
}).catch((err) => {
console.log(err)
Expand All @@ -83,7 +91,8 @@ ytch.getChannelVideos(channelId, sortBy).then((response) => {
// Response object
{
items: Array[Object],
continuation: String // Will return null if no more results can be found. Used with getChannelVideosMore()
continuation: String, // Will return null if no more results can be found. Used with getChannelVideosMore()
channelIdType: Number,
}
```

Expand All @@ -107,18 +116,21 @@ ytch.getChannelInfoMore(continuation).then((response) => {
}
```

**getChannelPlaylistInfo(channelId, [sortBy])**
**getChannelPlaylistInfo(channelId, [sortBy], [channelIdType])**

Grabs playlist information of a given channel ID.

- `last` - Grabs playlists from a channel sorted by the most recently updated playlist (Default option if none given)
- `newest` - Grabs playlists from a channel sorted by the creation date (newest first)

```javascript

- `channelIdType` defined as for `getChannelInfo()`

```javascript
const channelId = 'UCXuqSBlHAE6Xw-yeJA0Tunw'
const sortBy = 'last'

ytch.getChannelPlaylistInfo(channelId, sortBy).then((response) => {
ytch.getChannelPlaylistInfo(channelId, sortBy, channelIdType).then((response) => {
console.log(response)
}).catch((err) => {
console.log(err)
Expand All @@ -128,6 +140,7 @@ ytch.getChannelPlaylistInfo(channelId, sortBy).then((response) => {
{
items: Array[Object],
continuation: String // Will return null if no more results can be found. Used with getChannelPlaylistsMore()
channelIdType: Number,
}
```

Expand Down Expand Up @@ -212,11 +225,12 @@ ytch.getRelatedChannelsMore(continuation).then((response) => {
}
```

**getChannelCommunityPosts(channelId, authorURL)**
**getChannelCommunityPosts(channelId, [channelIdType])**

Searches for all posts on the community page of a given channelId based on the given query.

- `channelIdType` defined as for `getChannelInfo()`

Searchs for all posts on the community page of a given channelId based on the given query.
While the channelId is required as a fallback, a authorURL can be provided optionally to require one request to YouTube less.
If the author URL fails, then it will fallback to the authorURL and try to access the page normally.

```javascript
const channelId = 'UCXuqSBlHAE6Xw-yeJA0Tunw'
Expand All @@ -231,7 +245,8 @@ ytch.getChannelCommunityPosts(channelId, authorURL='http://www.youtube.com/c/cCh
{
items: Array[Object], // Described below
continuation: String, // Will return null if no more results can be found. Used with searchChannelMore()
innerTubeApi: String
innerTubeApi: String,
channelIdType: Number,
}
```

Expand Down
67 changes: 9 additions & 58 deletions app/fetchers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,22 @@ const helper = require('../helper')

class YoutubeChannelFetcher {
constructor(id, continuation) {
this._url = `https://www.youtube.com/channel/${id}/`
this.continuation = continuation
}

static async getChannelVideosNewest (channelId) {
const channelUrl = `https://www.youtube.com/channel/${channelId}/videos?flow=grid&view=0&pbj=1`
let channelPageResponse = await helper.makeChannelRequest(channelUrl)

if (channelPageResponse.error) {
// Try again as a user channel
const userUrl = `https://www.youtube.com/user/${channelId}/videos?flow=grid&view=0&pbj=1`
channelPageResponse = await helper.makeChannelRequest(userUrl)

if (channelPageResponse.error) {
const cUrl = `https://www.youtube.com/c/${channelId}/videos?flow=grid&view=0&pbj=1`
channelPageResponse = await helper.makeChannelRequest(cUrl)
if (channelPageResponse.error) {
return Promise.reject(channelPageResponse.message)
}
}
}

return await helper.parseChannelVideoResponse(channelPageResponse, channelId)
static async getChannelVideosNewest (channelId, channelIdType) {
const channelPageResponse = await helper.decideUrlRequestType(channelId, 'videos?flow=grid&view=0&pbj=1', channelIdType)
return await helper.parseChannelVideoResponse(channelPageResponse.response, channelId, channelPageResponse.channelIdType)
}

static async getChannelVideosOldest (channelId) {
const channelUrl = `https://www.youtube.com/channel/${channelId}/videos?view=0&sort=da&flow=grid&pbj=1`
let channelPageResponse = await helper.makeChannelRequest(channelUrl)

if (channelPageResponse.error) {
// Try again as a user channel
const userUrl = `https://www.youtube.com/user/${channelId}/videos?view=0&sort=da&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(userUrl)

if (channelPageResponse.error) {
const cUrl = `https://www.youtube.com/c/${channelId}/videos?view=0&sort=da&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(cUrl)
if (channelPageResponse.error) {
return Promise.reject(channelPageResponse.message)
}
}
}

return await helper.parseChannelVideoResponse(channelPageResponse, channelId)
static async getChannelVideosOldest (channelId, channelIdType) {
const channelPageResponse = await helper.decideUrlRequestType(channelId, 'videos?view=0&sort=da&flow=grid&pbj=1', channelIdType)
return await helper.parseChannelVideoResponse(channelPageResponse.response, channelId, channelPageResponse.channelIdType)
}

static async getChannelVideosPopular (channelId) {
const channelUrl = `https://www.youtube.com/channel/${channelId}/videos?view=0&sort=p&flow=grid&pbj=1`
let channelPageResponse = await helper.makeChannelRequest(channelUrl)

if (channelPageResponse.error) {
// Try again as a user channel
const userUrl = `https://www.youtube.com/user/${channelId}/videos?view=0&sort=p&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(userUrl)

if (channelPageResponse.error) {
const cUrl = `https://www.youtube.com/c/${channelId}/videos?view=0&sort=p&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(cUrl)
if (channelPageResponse.error) {
return Promise.reject(channelPageResponse.message)
}
}
}

return await helper.parseChannelVideoResponse(channelPageResponse, channelId)
static async getChannelVideosPopular (channelId, channelIdType) {
const channelPageResponse = await helper.decideUrlRequestType(channelId, 'videos?view=0&sort=p&flow=grid&pbj=1', channelIdType)
return await helper.parseChannelVideoResponse(channelPageResponse.response, channelId, channelPageResponse.channelIdType)
}
}

Expand Down
71 changes: 12 additions & 59 deletions app/fetchers/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,22 @@ class PlaylistFetcher {
this.getOriginalURL = () => _url
}

static async getChannelPlaylistLast (channelId) {
const channelUrl = `https://www.youtube.com/channel/${channelId}/playlists?flow=grid&sort=lad&view=1&pbj=1`
let channelPageResponse = await helper.makeChannelRequest(channelUrl)

if (channelPageResponse.error) {
// Try again as a user channel
const userUrl = `https://www.youtube.com/user/${channelId}/playlists?flow=grid&view=1&pbj=1`
channelPageResponse = await helper.makeChannelRequest(userUrl)

if (channelPageResponse.error) {
const cUrl = `https://www.youtube.com/c/${channelId}/playlists?flow=grid&view=1&pbj=1`
channelPageResponse = await helper.makeChannelRequest(cUrl)
if (channelPageResponse.error) {
return Promise.reject(channelPageResponse.message)
}
}
}

return await this.parseChannelPlaylistResponse(channelPageResponse)
static async getChannelPlaylistLast (channelId, channelIdType) {
const channelPageResponse = await helper.decideUrlRequestType(channelId, 'playlists?flow=grid&sort=lad&view=1&pbj=1', channelIdType)
return await this.parseChannelPlaylistResponse(channelPageResponse.response, channelPageResponse.channelIdType)
}

static async getChannelPlaylistOldest (channelId) {
const channelUrl = `https://www.youtube.com/channel/${channelId}/playlists?view=1&sort=da&flow=grid&pbj=1`
let channelPageResponse = await helper.makeChannelRequest(channelUrl)

if (channelPageResponse.error) {
// Try again as a user channel
const userUrl = `https://www.youtube.com/user/${channelId}/playlists?view=1&sort=da&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(userUrl)

if (channelPageResponse.error) {
const cUrl = `https://www.youtube.com/c/${channelId}/playlists?view=1&sort=da&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(cUrl)
if (channelPageResponse.error) {
return Promise.reject(channelPageResponse.message)
}
}
}

return await this.parseChannelPlaylistResponse(channelPageResponse)
static async getChannelPlaylistOldest (channelId, channelIdType) {
const channelPageResponse = await helper.decideUrlRequestType(channelId, 'playlists?view=1&sort=da&flow=grid&pbj=1', channelIdType)
return await this.parseChannelPlaylistResponse(channelPageResponse.response, channelPageResponse.channelIdType)
}

static async getChannelPlaylistNewest (channelId) {
const channelUrl = `https://www.youtube.com/channel/${channelId}/playlists?view=1&sort=dd&flow=grid&pbj=1`
let channelPageResponse = await helper.makeChannelRequest(channelUrl)

if (channelPageResponse.error) {
// Try again as a user channel
const userUrl = `https://www.youtube.com/user/${channelId}/playlists?view=1&sort=dd&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(userUrl)

if (channelPageResponse.error) {
const cUrl = `https://www.youtube.com/c/${channelId}/playlists?view=1&sort=dd&flow=grid&pbj=1`
channelPageResponse = await helper.makeChannelRequest(cUrl)
if (channelPageResponse.error) {
return Promise.reject(channelPageResponse.message)
}
}
}

return await this.parseChannelPlaylistResponse(channelPageResponse)
static async getChannelPlaylistNewest (channelId, channelIdType) {
const channelPageResponse = await helper.decideUrlRequestType(channelId, 'playlists?view=1&sort=dd&flow=grid&pbj=1', channelIdType)
return await this.parseChannelPlaylistResponse(channelPageResponse.response, channelPageResponse.channelIdType)
}

static async parseChannelPlaylistResponse (response) {
static async parseChannelPlaylistResponse (response, channelIdType) {
const channelMetaData = response.data[1].response.metadata.channelMetadataRenderer
const channelName = channelMetaData.title
const channelId = channelMetaData.externalId
Expand Down Expand Up @@ -110,7 +62,8 @@ class PlaylistFetcher {

return {
continuation: continuation,
items: playlistItems
items: playlistItems,
channelIdType: channelIdType,
}
}
}
Expand Down
Loading

0 comments on commit fcbab0e

Please sign in to comment.