|
| 1 | +// |
| 2 | +// EndScreenElement.swift |
| 3 | +// YouTubeKit |
| 4 | +// |
| 5 | +// Created by Antoine Bollengier on 11.01.2026. |
| 6 | +// Copyright © 2026 Antoine Bollengier (github.com/b5i). All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +public struct EndScreenElement { |
| 12 | + public var type: ElementType |
| 13 | + |
| 14 | + /// Start time in milliseconds where the element appears. |
| 15 | + public var startTime: Int |
| 16 | + |
| 17 | + /// End time in milliseconds where the element appears. |
| 18 | + public var endTime: Int |
| 19 | + |
| 20 | + /// Position of the element on the screen, all values are in percentage (0.0 - 1.0) relative to the video size. |
| 21 | + /// |
| 22 | + /// For example, an origin.x of 0.3 means the element starts at 30% of the video width from the left. |
| 23 | + public var position: CGRect |
| 24 | + |
| 25 | + public enum ElementType { |
| 26 | + case video(video: YTVideo) |
| 27 | + case playlist(playlist: YTPlaylist) |
| 28 | + |
| 29 | + /// - Note: If `subscribeButton` is true, that means a subscribe button is shown instead of the description. |
| 30 | + case channel(channel: YTChannel, subscribeButton: Bool, description: String?) |
| 31 | + case link(link: Link) |
| 32 | + |
| 33 | + public struct Link { |
| 34 | + public var url: URL |
| 35 | + public var title: String? |
| 36 | + public var thumbnail: [YTThumbnail] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public init?(fromEndscreenElementRenderer json: JSON){ |
| 41 | + guard let style = json["style"].string, |
| 42 | + let x = json["left"].double, |
| 43 | + let y = json["top"].double, |
| 44 | + let width = json["width"].double, |
| 45 | + let aspectRatio = json["aspectRatio"].double, |
| 46 | + let startTime = Int(json["startMs"].stringValue), |
| 47 | + let endTime = Int(json["endMs"].stringValue), |
| 48 | + let type: ElementType = { |
| 49 | + switch style { |
| 50 | + case "VIDEO": |
| 51 | + guard let videoId = json["endpoint", "watchEndpoint", "videoId"].string else { return nil } |
| 52 | + |
| 53 | + var video = YTVideo(videoId: videoId) |
| 54 | + video.title = json["title", "runs"].arrayValue.map { $0["text"].stringValue }.joined() |
| 55 | + video.viewCount = json["metadata", "runs", 0, "text"].string |
| 56 | + video.timeLength = json["thumbnailOverlays", 0, "thumbnailOverlayTimeStatusRenderer", "text", "runs", 0, "text"].string |
| 57 | + YTThumbnail.appendThumbnails(json: json["image"], thumbnailList: &video.thumbnails) |
| 58 | + |
| 59 | + return .video(video: video) |
| 60 | + case "PLAYLIST": |
| 61 | + guard let playlistId = json["endpoint", "watchEndpoint", "playlistId"].string else { return nil } |
| 62 | + |
| 63 | + var playlist = YTPlaylist(playlistId: playlistId) |
| 64 | + playlist.title = json["title", "runs"].arrayValue.map { $0["text"].stringValue }.joined() |
| 65 | + playlist.videoCount = json["playlistLength", "runs", 0, "text"].string |
| 66 | + YTThumbnail.appendThumbnails(json: json["image"], thumbnailList: &playlist.thumbnails) |
| 67 | + |
| 68 | + return .playlist(playlist: playlist) |
| 69 | + case "CHANNEL": |
| 70 | + guard let channelId = json["endpoint", "browseEndpoint", "browseId"].string else { return nil } |
| 71 | + |
| 72 | + var channel = YTChannel(channelId: channelId) |
| 73 | + channel.name = json["title", "runs"].arrayValue.map { $0["text"].stringValue }.joined() |
| 74 | + YTThumbnail.appendThumbnails(json: json["image"], thumbnailList: &channel.thumbnails) |
| 75 | + channel.subscriberCount = json["subscribersText", "runs", 0, "text"].string ?? json["metadata", "runs", 0, "text"].string |
| 76 | + |
| 77 | + let subscribeButton = json["isSubscribe"].boolValue |
| 78 | + |
| 79 | + return .channel(channel: channel, subscribeButton: subscribeButton, description: subscribeButton ? nil : json["metadata", "runs"].arrayValue.map { $0["text"].stringValue }.joined()) |
| 80 | + case "WEBSITE": |
| 81 | + guard let bloatedUrl = json["endpoint", "urlEndpoint", "url"].url, |
| 82 | + let urlString = URLComponents(url: bloatedUrl, resolvingAgainstBaseURL: false)?.queryItems?.first(where: {$0.name == "q"})?.value, |
| 83 | + let url = URL(string: urlString) |
| 84 | + else { return nil } |
| 85 | + let title = json["title", "runs"].arrayValue.map { $0["text"].stringValue }.joined() |
| 86 | + let thumbnail = YTThumbnail.getThumbnails(json: json["image"]) |
| 87 | + return .link(link: .init(url: url, title: title, thumbnail: thumbnail)) |
| 88 | + default: |
| 89 | + return nil |
| 90 | + } |
| 91 | + }() |
| 92 | + |
| 93 | + else { return nil } |
| 94 | + |
| 95 | + self.type = type |
| 96 | + self.position = CGRect(x: x, y: y, width: width, height: width / (aspectRatio + 0.001)) |
| 97 | + self.startTime = startTime |
| 98 | + self.endTime = endTime |
| 99 | + } |
| 100 | +} |
0 commit comments