Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/entities/channel-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ChannelSection {
/**
* The parts to request for this entity.
*/
public static part = 'snippet,contentDetails'
public static part = 'contentDetails,snippet'

/**
* The fields to request for this entity.
Expand Down
7 changes: 4 additions & 3 deletions src/entities/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class Channel {
/**
* The parts to request for this entity.
*/
public static part = 'snippet,contentDetails,statistics,status,brandingSettings,localizations'
public static part =
'brandingSettings,contentDetails,localizations,snippet,statistics,status'

/**
* The fields to request for this entity.
Expand Down Expand Up @@ -322,8 +323,8 @@ export class Channel {
*/
public async unsubscribe (subscriptionResolvable?: SubscriptionResolvable, myId?: string) {
const subscription = subscriptionResolvable ?
await this.youtube._resolutionService.resolve(subscriptionResolvable, Subscription) :
await this.youtube._subscriptionService.getSubscriptionByChannels(
await this.youtube._services.resolution.resolve(subscriptionResolvable, Subscription) :
await this.youtube.getSubscriptionByChannels(
myId ?? (await this.youtube.oauth.getMe()).id, this.id
)

Expand Down
1 change: 1 addition & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export * from './video'
export * from './video-rating'
export * from './channel'
export * from './playlist'
export * from './comment'
Expand Down
24 changes: 15 additions & 9 deletions src/entities/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Playlist {
/**
* The parts to request for this entity.
*/
public static part = 'snippet,contentDetails,player,status'
public static part = 'contentDetails,player,snippet,status'

/**
* The fields to request for this entity.
Expand Down Expand Up @@ -199,7 +199,7 @@ export class Playlist {
* @param note A note on the video.
*/
public async addVideo (videoResolvable: VideoResolvable, position?: number, note?: string) {
const videoId = await this.youtube._resolutionService.resolve(videoResolvable, Video)
const videoId = await this.youtube._services.resolution.resolve(videoResolvable, Video)
const playlistItem = await this.youtube.oauth.playlists.addPlaylistItem(
this, videoId, position, note)

Expand All @@ -218,8 +218,8 @@ export class Playlist {
* @param itemId The playlist item ID if you have it.
*/
public async updateVideo (videoResolvable: VideoResolvable, position?: number, note?: string, itemId?: string) {
const video = await this.youtube._resolutionService.resolve(videoResolvable, Video)
const playlistItemId = itemId ?? (await this.youtube._genericService.getPaginatedItems({
const video = await this.youtube._services.resolution.resolve(videoResolvable, Video)
const playlistItemId = itemId ?? (await this.youtube._services.retrieval.getPaginatedItems({
type: PaginatedItemType.PlaylistItems,
mine: false,
id: this.id,
Expand All @@ -238,16 +238,22 @@ export class Playlist {
* @param videoResolvable The URL, ID, or (not recommended) search query of the video.
*/
public async removeVideo (videoResolvable: VideoResolvable) {
const video = await this.youtube._resolutionService.resolve(videoResolvable, Video)
const playlistItemId = (this.youtube._genericService.getPaginatedItems({
const video = await this.youtube._services.resolution.resolve(videoResolvable, Video)
const matchingItems = (await this.youtube._services.retrieval.getPaginatedItems<Video>({
type: PaginatedItemType.PlaylistItems,
mine: false,
id: this.id,
maxPerPage: 1,
subId: typeof video === 'string' ? video : video.id
}))[0].id
})).items

if (!matchingItems.length) {
return Promise.reject(
new Error('The requested video was not found in the playlist')
)
}

return this.removeItem(playlistItemId)
// Remove by playlist item ID, not video ID
return this.removeItem(matchingItems[0].data.id)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/entities/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Subscription {
/**
* The parts to request for this entity.
*/
public static part = 'snippet,contentDetails,subscriberSnippet'
public static part = 'contentDetails,snippet,subscriberSnippet'

/**
* The fields to request for this entity.
Expand Down
79 changes: 79 additions & 0 deletions src/entities/video-rating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { YouTube } from '..'

/**
* A YouTube [Video](./Library_Exports.Video) rating (like/dislike).
*/
export class VideoRating {
/**
* The name of the endpoint used for this entity.
*/
public static endpoint = 'videos/getRating'

/**
* The parts to request for this entity.
*/
public static part = ''

/**
* The fields to request for this entity.
*/
public static fields = 'items(videoId,rating)'

/**
* The YouTube object that created this channel section object.
*/
public youtube: YouTube

/**
* Whether or not this a full channel section object.
*/
public full = true

/**
* The raw data of this channel section.
*/
public data: any

/**
* The ID of the video this rating is for.
*/
public videoId: string

/**
* The rating left on this video.
*/
public rating: 'like' | 'dislike' | 'none' | 'unspecified'

constructor (youtube: YouTube, data: any, full = true) {
this.youtube = youtube
this.data = data

this._init(data)
}

/**
* @ignore
*/
private _init (data: any) {
// Currently broken in the API
/* if (data.kind !== 'youtube#videoGetRatingResponse') {
throw new Error(`Invalid video rating type: ${data.kind}`)
} */

if (!data.videoId || !data.rating) {
throw new Error('Invalid video rating')
}

this.videoId = data.videoId
this.rating = data.rating
}

/**
* Fetches this video rating from the API and reassigns this object to the new rating object.
* Only useful if you want updated rating info.
*/
public async fetch () {
const rating = await this.youtube.oauth.videos.getMyRatings(this.videoId)
return Object.assign(this, rating)
}
}
7 changes: 4 additions & 3 deletions src/entities/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class Video {
/**
* The parts to request for this entity.
*/
public static part = 'snippet,contentDetails,statistics,status,recordingDetails,localizations'
public static part =
'contentDetails,localizations,recordingDetails,snippet,statistics,status'

/**
* The fields to request for this entity.
Expand Down Expand Up @@ -311,8 +312,8 @@ export class Video {
* Must be using an access token with correct scopes.
*/
public async getRating (): Promise<'like' | 'dislike' | 'none' | 'unspecified'> {
const response = await this.youtube.oauth.videos.getMyRatings([ this.id ])
return response[0].rating
const response = await this.youtube.oauth.videos.getMyRatings(this.id)
return response.rating
}

/**
Expand Down
Loading