From 7e3e393c6531776ae525e350bbcd04dd5b447eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Mon, 20 May 2024 20:11:41 +0200 Subject: [PATCH] Invidious: add images to chapters Invidious, by design, has no images attached to chapters, in contrast to Piped. Since the majority of videos with chapters don't have chapter-specific images and only use the videos' thumbnail, there is no difference here when compared to Piped's native thumbnail support. --- Model/Applications/InvidiousAPI.swift | 18 +++++++++++++++++- Shared/Player/Video Details/ChapterView.swift | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Model/Applications/InvidiousAPI.swift b/Model/Applications/InvidiousAPI.swift index 6d23a3f4..d0a94b41 100644 --- a/Model/Applications/InvidiousAPI.swift +++ b/Model/Applications/InvidiousAPI.swift @@ -502,7 +502,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { keywords: json["keywords"].arrayValue.compactMap { $0.string }, streams: extractStreams(from: json), related: extractRelated(from: json), - chapters: extractChapters(from: description), + chapters: createChapters(from: description, thumbnails: json), captions: extractCaptions(from: json) ) } @@ -575,6 +575,22 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { } } + private func createChapters(from description: String, thumbnails: JSON) -> [Chapter] { + var chapters = extractChapters(from: description) + + if !chapters.isEmpty { + let thumbnailsData = extractThumbnails(from: thumbnails) + let thumbnailURL = thumbnailsData.first { $0.quality == .medium }?.url + + for chapter in chapters.indices { + if let url = thumbnailURL { + chapters[chapter].image = url + } + } + } + return chapters + } + private static var contentItemsKeys = ["items", "videos", "latestVideos", "playlists", "relatedChannels"] private func extractChannelPage(from json: JSON, forceNotLast: Bool = false) -> ChannelPage { diff --git a/Shared/Player/Video Details/ChapterView.swift b/Shared/Player/Video Details/ChapterView.swift index 695807d4..f4b9c831 100644 --- a/Shared/Player/Video Details/ChapterView.swift +++ b/Shared/Player/Video Details/ChapterView.swift @@ -65,7 +65,7 @@ import SwiftUI } static var thumbnailHeight: Double { - thumbnailWidth / 1.7777 + thumbnailWidth / (16 / 9) } }