Skip to content

Commit

Permalink
fixed metadata extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeichhorn committed Jan 19, 2025
1 parent 7d8cb12 commit 51a1619
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Sources/YouTubeKit/InnerTube.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ class InnerTube {

struct VideoDetails: Decodable {
let videoId: String
let title: String
let shortDescription: String
let title: String?
let shortDescription: String?
let thumbnail: Thumbnail

struct Thumbnail: Decodable {
Expand Down
20 changes: 18 additions & 2 deletions Sources/YouTubeKit/Models/YouTubeMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,26 @@ public struct YouTubeMetadata: Sendable {
@available(iOS 13.0, watchOS 6.0, tvOS 13.0, macOS 10.15, *)
static func metadata(from videoDetails: InnerTube.VideoInfo.VideoDetails) -> Self {
YouTubeMetadata(
title: videoDetails.title,
description: videoDetails.shortDescription,
title: videoDetails.title ?? "",
description: videoDetails.shortDescription ?? "",
thumbnail: videoDetails.thumbnail.thumbnails.map { YouTubeMetadata.Thumbnail(url: $0.url) }.last
)
}

/// Initialize YouTubeMetadata from multiple video details - choosing the first available information each.
///
/// - Parameters:
/// - videoDetails: The video details from InnerTube.VideoInfo.VideoDetails.
/// - Returns: A YouTubeMetadata instance.
@available(iOS 13.0, watchOS 6.0, tvOS 13.0, macOS 10.15, *)
static func metadata(from videoDetails: [InnerTube.VideoInfo.VideoDetails]) -> Self? {
guard let title = videoDetails.lazy.compactMap({ $0.title }).first else { return nil }

return YouTubeMetadata(
title: title,
description: videoDetails.lazy.compactMap { $0.shortDescription }.first ?? "",
thumbnail: videoDetails.first(where: { !$0.thumbnail.thumbnails.isEmpty })?.thumbnail.thumbnails.map { YouTubeMetadata.Thumbnail(url: $0.url) }.last
)
}

}
8 changes: 2 additions & 6 deletions Sources/YouTubeKit/YouTube.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,9 @@ public class YouTube {
}

/// Video details from video info.
var videoDetails: InnerTube.VideoInfo.VideoDetails {
var videoDetails: [InnerTube.VideoInfo.VideoDetails] {
get async throws {
if let videoDetails = try await videoInfos.lazy.compactMap({ $0.videoDetails }).first {
return videoDetails
} else {
throw YouTubeKitError.extractError
}
try await videoInfos.compactMap { $0.videoDetails }
}
}

Expand Down

0 comments on commit 51a1619

Please sign in to comment.