Skip to content

Commit

Permalink
fix: fix RecommendedVideosContinuation's extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
b5i committed Jan 27, 2025
1 parent 9ea1bd0 commit e0b76eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Sources/YouTubeKit/BaseStructs/YTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct YTVideo: YTSearchResult, YouTubeVideo, Codable, Sendable {
}

public static func canBeDecoded(json: JSON) -> Bool {
return json["videoId"].string != nil || json["onTap"]["innertubeCommand"]["reelWatchEndpoint"]["videoId"].string != nil
return json["videoId"].string != nil || json["onTap"]["innertubeCommand"]["reelWatchEndpoint"]["videoId"].string != nil || json["contentType"].string == "LOCKUP_CONTENT_TYPE_VIDEO"
}

public static func decodeJSON(json: JSON) -> YTVideo? {
Expand Down Expand Up @@ -84,14 +84,16 @@ public struct YTVideo: YTSearchResult, YouTubeVideo, Codable, Sendable {
var video = YTVideo(videoId: videoId)

video.title = json["metadata"]["lockupMetadataViewModel"]["title"]["content"].string

if let channelJSON = json["metadata"]["lockupMetadataViewModel"]["metadata"]["contentMetadataViewModel"]["metadataRows"].array?.first(where: { $0["metadataParts"].array?.first?["text"]["commandRuns"].array?.first?["onTap"]["innertubeCommand"]["commandMetadata"]["webCommandMetadata"]["webPageType"].string == "WEB_PAGE_TYPE_CHANNEL" }), let channelId = channelJSON["metadataParts"].array?.first?["text"]["commandRuns"].array?.first?["onTap"]["innertubeCommand"]["browseEndpoint"]["browseId"].string {

let metadataRows = json["metadata"]["lockupMetadataViewModel"]["metadata"]["contentMetadataViewModel"]["metadataRows"]

if let channelJSON = metadataRows.array?.first(where: { $0["metadataParts"].array?.first?["text"]["commandRuns"].array?.first?["onTap"]["innertubeCommand"]["commandMetadata"]["webCommandMetadata"]["webPageType"].string == "WEB_PAGE_TYPE_CHANNEL" }) ?? metadataRows.array?.first {
let channelId = channelJSON["metadataParts"].array?.first?["text"]["commandRuns"].array?.first?["onTap"]["innertubeCommand"]["browseEndpoint"]["browseId"].string ?? ""
video.channel = YTLittleChannelInfos(channelId: channelId, name: channelJSON["metadataParts"].array?.first?["text"]["content"].string)
YTThumbnail.appendThumbnails(json: json["metadata"]["lockupMetadataViewModel"]["image"]["decoratedAvatarViewModel"]["avatar"]["avatarViewModel"], thumbnailList: &video.channel!.thumbnails)

}

let viewCountAndDateJSON = json["metadata"]["lockupMetadataViewModel"]["metadata"]["contentMetadataViewModel"]["metadataRows"].array?.first(where: { $0["metadataParts"].array?.first?["text"]["commandRuns"].array?.first?["onTap"]["innertubeCommand"]["commandMetadata"]["webCommandMetadata"]["webPageType"].string != "WEB_PAGE_TYPE_CHANNEL" })
let viewCountAndDateJSON = metadataRows.array?.filter({ $0["metadataParts"].array?.first?["text"]["commandRuns"].array?.first?["onTap"]["innertubeCommand"]["commandMetadata"]["webCommandMetadata"]["webPageType"].string != "WEB_PAGE_TYPE_CHANNEL" || $0["badges"].exists() }).last

video.viewCount = viewCountAndDateJSON?["metadataParts"].array?.first?["text"]["content"].string
video.timePosted = viewCountAndDateJSON?["metadataParts"].array?.last?["text"]["content"].string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ public struct MoreVideoInfosResponse: YouTubeResponse {
for element in action["appendContinuationItemsAction"]["continuationItems"].arrayValue {
if element["compactVideoRenderer"]["videoId"].exists(), let decodedVideo = YTVideo.decodeJSON(json: element["compactVideoRenderer"]) {
toReturn.results.append(decodedVideo)
} else if element["lockupViewModel"]["contentType"].string == "LOCKUP_CONTENT_TYPE_VIDEO", let decodedVideo = YTVideo.decodeLockupJSON(json: element["lockupViewModel"]) {
toReturn.results.append(decodedVideo)
} else if let continuationToken = element["continuationItemRenderer"]["continuationEndpoint"]["continuationCommand"]["token"].string {
toReturn.continuationToken = continuationToken
}
Expand Down

0 comments on commit e0b76eb

Please sign in to comment.