Improve data parsers

This commit is contained in:
Arkadiusz Fal 2022-06-18 13:24:23 +02:00
parent 12b18b5d59
commit f367fe8728
3 changed files with 100 additions and 74 deletions

View File

@ -93,7 +93,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
configureTransformer(pathPattern("search"), requestMethods: [.get]) { (content: Entity<JSON>) -> SearchPage in configureTransformer(pathPattern("search"), requestMethods: [.get]) { (content: Entity<JSON>) -> SearchPage in
let results = content.json.arrayValue.compactMap { json -> ContentItem? in let results = content.json.arrayValue.compactMap { json -> ContentItem? in
let type = json.dictionaryValue["type"]?.stringValue let type = json.dictionaryValue["type"]?.string
if type == "channel" { if type == "channel" {
return ContentItem(channel: self.extractChannel(from: json)) return ContentItem(channel: self.extractChannel(from: json))
@ -401,14 +401,14 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
publishedAt: publishedAt, publishedAt: publishedAt,
likes: json["likeCount"].int, likes: json["likeCount"].int,
dislikes: json["dislikeCount"].int, dislikes: json["dislikeCount"].int,
keywords: json["keywords"].arrayValue.map { $0.stringValue }, keywords: json["keywords"].arrayValue.compactMap { $0.string },
streams: extractStreams(from: json), streams: extractStreams(from: json),
related: extractRelated(from: json) related: extractRelated(from: json)
) )
} }
func extractChannel(from json: JSON) -> Channel { func extractChannel(from json: JSON) -> Channel {
var thumbnailURL = json["authorThumbnails"].arrayValue.last?.dictionaryValue["url"]?.stringValue ?? "" var thumbnailURL = json["authorThumbnails"].arrayValue.last?.dictionaryValue["url"]?.string ?? ""
// append protocol to unproxied thumbnail URL if it's missing // append protocol to unproxied thumbnail URL if it's missing
if thumbnailURL.count > 2, if thumbnailURL.count > 2,
@ -467,32 +467,41 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
} }
private func extractFormatStreams(from streams: [JSON]) -> [Stream] { private func extractFormatStreams(from streams: [JSON]) -> [Stream] {
streams.map { streams.compactMap { stream in
SingleAssetStream( guard let streamURL = stream["url"].url else {
avAsset: AVURLAsset(url: $0["url"].url!), return nil
resolution: Stream.Resolution.from(resolution: $0["resolution"].stringValue), }
return SingleAssetStream(
avAsset: AVURLAsset(url: streamURL),
resolution: Stream.Resolution.from(resolution: stream["resolution"].string ?? ""),
kind: .stream, kind: .stream,
encoding: $0["encoding"].stringValue encoding: stream["encoding"].string ?? ""
) )
} }
} }
private func extractAdaptiveFormats(from streams: [JSON]) -> [Stream] { private func extractAdaptiveFormats(from streams: [JSON]) -> [Stream] {
let audioAssetURL = streams.first { $0["type"].stringValue.starts(with: "audio/mp4") } guard let audioStream = streams.first(where: { $0["type"].stringValue.starts(with: "audio/mp4") }) else {
guard audioAssetURL != nil else { return .init()
return []
} }
let videoAssetsURLs = streams.filter { $0["type"].stringValue.starts(with: "video/") } let videoStreams = streams.filter { $0["type"].stringValue.starts(with: "video/") }
return videoAssetsURLs.map { return videoStreams.compactMap { videoStream in
Stream( guard let audioAssetURL = audioStream["url"].url,
audioAsset: AVURLAsset(url: audioAssetURL!["url"].url!), let videoAssetURL = videoStream["url"].url
videoAsset: AVURLAsset(url: $0["url"].url!), else {
resolution: Stream.Resolution.from(resolution: $0["resolution"].stringValue), return nil
}
return Stream(
audioAsset: AVURLAsset(url: audioAssetURL),
videoAsset: AVURLAsset(url: videoAssetURL),
resolution: Stream.Resolution.from(resolution: videoStream["resolution"].stringValue),
kind: .adaptive, kind: .adaptive,
encoding: $0["encoding"].stringValue, encoding: videoStream["encoding"].string,
videoFormat: $0["type"].stringValue videoFormat: videoStream["type"].string
) )
} }
} }

View File

@ -58,7 +58,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
} }
configureTransformer(pathPattern("search")) { (content: Entity<JSON>) -> SearchPage in configureTransformer(pathPattern("search")) { (content: Entity<JSON>) -> SearchPage in
let nextPage = content.json.dictionaryValue["nextpage"]?.stringValue let nextPage = content.json.dictionaryValue["nextpage"]?.string
return SearchPage( return SearchPage(
results: self.extractContentItems(from: content.json.dictionaryValue["items"]!), results: self.extractContentItems(from: content.json.dictionaryValue["items"]!),
nextPage: nextPage, nextPage: nextPage,
@ -71,24 +71,24 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
} }
configureTransformer(pathPattern("subscriptions")) { (content: Entity<JSON>) -> [Channel] in configureTransformer(pathPattern("subscriptions")) { (content: Entity<JSON>) -> [Channel] in
content.json.arrayValue.map { self.extractChannel(from: $0)! } content.json.arrayValue.compactMap { self.extractChannel(from: $0) }
} }
configureTransformer(pathPattern("feed")) { (content: Entity<JSON>) -> [Video] in configureTransformer(pathPattern("feed")) { (content: Entity<JSON>) -> [Video] in
content.json.arrayValue.map { self.extractVideo(from: $0)! } content.json.arrayValue.compactMap { self.extractVideo(from: $0) }
} }
configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>) -> CommentsPage in configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>) -> CommentsPage in
let details = content.json.dictionaryValue let details = content.json.dictionaryValue
let comments = details["comments"]?.arrayValue.map { self.extractComment(from: $0)! } ?? [] let comments = details["comments"]?.arrayValue.compactMap { self.extractComment(from: $0) } ?? []
let nextPage = details["nextpage"]?.stringValue let nextPage = details["nextpage"]?.string
let disabled = details["disabled"]?.boolValue ?? false let disabled = details["disabled"]?.bool ?? false
return CommentsPage(comments: comments, nextPage: nextPage, disabled: disabled) return CommentsPage(comments: comments, nextPage: nextPage, disabled: disabled)
} }
configureTransformer(pathPattern("user/playlists")) { (content: Entity<JSON>) -> [Playlist] in configureTransformer(pathPattern("user/playlists")) { (content: Entity<JSON>) -> [Playlist] in
content.json.arrayValue.map { self.extractUserPlaylist(from: $0)! } content.json.arrayValue.compactMap { self.extractUserPlaylist(from: $0) }
} }
if account.token.isNil { if account.token.isNil {
@ -286,11 +286,10 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
private func extractContentItem(from content: JSON) -> ContentItem? { private func extractContentItem(from content: JSON) -> ContentItem? {
let details = content.dictionaryValue let details = content.dictionaryValue
let url: String! = details["url"]?.string
let contentType: ContentItem.ContentType let contentType: ContentItem.ContentType
if !url.isNil { if let url = details["url"]?.string {
if url.contains("/playlist") { if url.contains("/playlist") {
contentType = .playlist contentType = .playlist
} else if url.contains("/channel") { } else if url.contains("/channel") {
@ -330,21 +329,27 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
private func extractChannel(from content: JSON) -> Channel? { private func extractChannel(from content: JSON) -> Channel? {
let attributes = content.dictionaryValue let attributes = content.dictionaryValue
guard let id = attributes["id"]?.stringValue ?? guard let id = attributes["id"]?.string ??
(attributes["url"] ?? attributes["uploaderUrl"])?.stringValue.components(separatedBy: "/").last (attributes["url"] ?? attributes["uploaderUrl"])?.string?.components(separatedBy: "/").last
else { else {
return nil return nil
} }
let subscriptionsCount = attributes["subscriberCount"]?.intValue ?? attributes["subscribers"]?.intValue let subscriptionsCount = attributes["subscriberCount"]?.int ?? attributes["subscribers"]?.int
var videos = [Video]() var videos = [Video]()
if let relatedStreams = attributes["relatedStreams"] { if let relatedStreams = attributes["relatedStreams"] {
videos = extractVideos(from: relatedStreams) videos = extractVideos(from: relatedStreams)
} }
let name = attributes["name"]?.stringValue ?? attributes["uploaderName"]?.stringValue ?? attributes["uploader"]?.stringValue ?? "" let name = attributes["name"]?.string ??
let thumbnailURL = attributes["avatarUrl"]?.url ?? attributes["uploaderAvatar"]?.url ?? attributes["avatar"]?.url ?? attributes["thumbnail"]?.url attributes["uploaderName"]?.string ??
attributes["uploader"]?.string ?? ""
let thumbnailURL = attributes["avatarUrl"]?.url ??
attributes["uploaderAvatar"]?.url ??
attributes["avatar"]?.url ??
attributes["thumbnail"]?.url
return Channel( return Channel(
id: id, id: id,
@ -357,7 +362,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
func extractChannelPlaylist(from json: JSON) -> ChannelPlaylist? { func extractChannelPlaylist(from json: JSON) -> ChannelPlaylist? {
let details = json.dictionaryValue let details = json.dictionaryValue
let id = details["url"]?.stringValue.components(separatedBy: "?list=").last ?? UUID().uuidString let id = details["url"]?.string?.components(separatedBy: "?list=").last ?? UUID().uuidString
let thumbnailURL = details["thumbnail"]?.url ?? details["thumbnailUrl"]?.url let thumbnailURL = details["thumbnail"]?.url ?? details["thumbnailUrl"]?.url
var videos = [Video]() var videos = [Video]()
if let relatedStreams = details["relatedStreams"] { if let relatedStreams = details["relatedStreams"] {
@ -365,7 +370,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
} }
return ChannelPlaylist( return ChannelPlaylist(
id: id, id: id,
title: details["name"]?.stringValue ?? "", title: details["name"]?.string ?? "",
thumbnailURL: thumbnailURL, thumbnailURL: thumbnailURL,
channel: extractChannel(from: json)!, channel: extractChannel(from: json)!,
videos: videos, videos: videos,
@ -375,15 +380,14 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
private func extractVideo(from content: JSON) -> Video? { private func extractVideo(from content: JSON) -> Video? {
let details = content.dictionaryValue let details = content.dictionaryValue
let url = details["url"]?.string
if !url.isNil { if let url = details["url"]?.string {
guard url!.contains("/watch") else { guard url.contains("/watch") else {
return nil return nil
} }
} }
let channelId = details["uploaderUrl"]!.stringValue.components(separatedBy: "/").last! let channelId = details["uploaderUrl"]?.string?.components(separatedBy: "/").last ?? "unknown"
let thumbnails: [Thumbnail] = Thumbnail.Quality.allCases.compactMap { let thumbnails: [Thumbnail] = Thumbnail.Quality.allCases.compactMap {
if let url = buildThumbnailURL(from: content, quality: $0) { if let url = buildThumbnailURL(from: content, quality: $0) {
@ -393,25 +397,25 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
return nil return nil
} }
let author = details["uploaderName"]?.stringValue ?? details["uploader"]!.stringValue let author = details["uploaderName"]?.string ?? details["uploader"]?.string ?? ""
let authorThumbnailURL = details["avatarUrl"]?.url ?? details["uploaderAvatar"]?.url ?? details["avatar"]?.url let authorThumbnailURL = details["avatarUrl"]?.url ?? details["uploaderAvatar"]?.url ?? details["avatar"]?.url
let subscriptionsCount = details["uploaderSubscriberCount"]?.int let subscriptionsCount = details["uploaderSubscriberCount"]?.int
let uploaded = details["uploaded"]?.doubleValue let uploaded = details["uploaded"]?.double
var published = (uploaded.isNil || uploaded == -1) ? nil : (uploaded! / 1000).formattedAsRelativeTime() var published = (uploaded.isNil || uploaded == -1) ? nil : (uploaded! / 1000).formattedAsRelativeTime()
if published.isNil { if published.isNil {
published = (details["uploadedDate"] ?? details["uploadDate"])?.stringValue ?? "" published = (details["uploadedDate"] ?? details["uploadDate"])?.string ?? ""
} }
let live = details["livestream"]?.boolValue ?? (details["duration"]?.intValue == -1) let live = details["livestream"]?.bool ?? (details["duration"]?.int == -1)
return Video( return Video(
videoID: extractID(from: content), videoID: extractID(from: content),
title: details["title"]!.stringValue, title: details["title"]?.string ?? "",
author: author, author: author,
length: details["duration"]!.doubleValue, length: details["duration"]?.double ?? 0,
published: published!, published: published ?? "",
views: details["views"]!.intValue, views: details["views"]?.int ?? 0,
description: extractDescription(from: content), description: extractDescription(from: content),
channel: Channel(id: channelId, name: author, thumbnailURL: authorThumbnailURL, subscriptionsCount: subscriptionsCount), channel: Channel(id: channelId, name: author, thumbnailURL: authorThumbnailURL, subscriptionsCount: subscriptionsCount),
thumbnails: thumbnails, thumbnails: thumbnails,
@ -424,30 +428,29 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
} }
private func extractID(from content: JSON) -> Video.ID { private func extractID(from content: JSON) -> Video.ID {
content.dictionaryValue["url"]?.stringValue.components(separatedBy: "?v=").last ?? content.dictionaryValue["url"]?.string?.components(separatedBy: "?v=").last ??
extractThumbnailURL(from: content)!.relativeString.components(separatedBy: "/")[4] extractThumbnailURL(from: content)?.relativeString.components(separatedBy: "/")[4] ?? ""
} }
private func extractThumbnailURL(from content: JSON) -> URL? { private func extractThumbnailURL(from content: JSON) -> URL? {
content.dictionaryValue["thumbnail"]?.url! ?? content.dictionaryValue["thumbnailUrl"]!.url! content.dictionaryValue["thumbnail"]?.url ?? content.dictionaryValue["thumbnailUrl"]?.url
} }
private func buildThumbnailURL(from content: JSON, quality: Thumbnail.Quality) -> URL? { private func buildThumbnailURL(from content: JSON, quality: Thumbnail.Quality) -> URL? {
let thumbnailURL = extractThumbnailURL(from: content) guard let thumbnailURL = extractThumbnailURL(from: content) else {
guard !thumbnailURL.isNil else {
return nil return nil
} }
return URL(string: thumbnailURL! return URL(string: thumbnailURL
.absoluteString .absoluteString
.replacingOccurrences(of: "hqdefault", with: quality.filename) .replacingOccurrences(of: "hqdefault", with: quality.filename)
.replacingOccurrences(of: "maxresdefault", with: quality.filename) .replacingOccurrences(of: "maxresdefault", with: quality.filename)
)! )
} }
private func extractUserPlaylist(from json: JSON) -> Playlist? { private func extractUserPlaylist(from json: JSON) -> Playlist? {
let id = json["id"].stringValue let id = json["id"].string ?? ""
let title = json["name"].stringValue let title = json["name"].string ?? ""
let visibility = Playlist.Visibility.private let visibility = Playlist.Visibility.private
return Playlist(id: id, title: title, visibility: visibility) return Playlist(id: id, title: title, visibility: visibility)
@ -489,9 +492,10 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
let audioStreams = content let audioStreams = content
.dictionaryValue["audioStreams"]? .dictionaryValue["audioStreams"]?
.arrayValue .arrayValue
.filter { $0.dictionaryValue["format"]?.stringValue == "M4A" } .filter { $0.dictionaryValue["format"]?.string == "M4A" }
.sorted { .sorted {
$0.dictionaryValue["bitrate"]?.intValue ?? 0 > $1.dictionaryValue["bitrate"]?.intValue ?? 0 $0.dictionaryValue["bitrate"]?.int ?? 0 >
$1.dictionaryValue["bitrate"]?.int ?? 0
} ?? [] } ?? []
guard let audioStream = audioStreams.first else { guard let audioStream = audioStreams.first else {
@ -506,19 +510,31 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
return return
} }
let audioAsset = AVURLAsset(url: audioStream.dictionaryValue["url"]!.url!) guard let audioAssetUrl = audioStream.dictionaryValue["url"]?.url,
let videoAsset = AVURLAsset(url: videoStream.dictionaryValue["url"]!.url!) let videoAssetUrl = videoStream.dictionaryValue["url"]?.url
else {
return
}
let videoOnly = videoStream.dictionaryValue["videoOnly"]?.boolValue ?? true let audioAsset = AVURLAsset(url: audioAssetUrl)
let videoAsset = AVURLAsset(url: videoAssetUrl)
let videoOnly = videoStream.dictionaryValue["videoOnly"]?.bool ?? true
let quality = videoStream.dictionaryValue["quality"]?.string ?? "unknown" let quality = videoStream.dictionaryValue["quality"]?.string ?? "unknown"
let qualityComponents = quality.components(separatedBy: "p") let qualityComponents = quality.components(separatedBy: "p")
let fps = qualityComponents.count > 1 ? Int(qualityComponents[1]) : 30 let fps = qualityComponents.count > 1 ? Int(qualityComponents[1]) : 30
let resolution = Stream.Resolution.from(resolution: quality, fps: fps) let resolution = Stream.Resolution.from(resolution: quality, fps: fps)
let videoFormat = videoStream.dictionaryValue["format"]?.stringValue let videoFormat = videoStream.dictionaryValue["format"]?.string
if videoOnly { if videoOnly {
streams.append( streams.append(
Stream(audioAsset: audioAsset, videoAsset: videoAsset, resolution: resolution, kind: .adaptive, videoFormat: videoFormat) Stream(
audioAsset: audioAsset,
videoAsset: videoAsset,
resolution: resolution,
kind: .adaptive,
videoFormat: videoFormat
)
) )
} else { } else {
streams.append( streams.append(
@ -539,19 +555,19 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
private func extractComment(from content: JSON) -> Comment? { private func extractComment(from content: JSON) -> Comment? {
let details = content.dictionaryValue let details = content.dictionaryValue
let author = details["author"]?.stringValue ?? "" let author = details["author"]?.string ?? ""
let commentorUrl = details["commentorUrl"]?.stringValue let commentorUrl = details["commentorUrl"]?.string
let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? "" let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? ""
return Comment( return Comment(
id: details["commentId"]?.stringValue ?? UUID().uuidString, id: details["commentId"]?.string ?? UUID().uuidString,
author: author, author: author,
authorAvatarURL: details["thumbnail"]?.stringValue ?? "", authorAvatarURL: details["thumbnail"]?.string ?? "",
time: details["commentedTime"]?.stringValue ?? "", time: details["commentedTime"]?.string ?? "",
pinned: details["pinned"]?.boolValue ?? false, pinned: details["pinned"]?.bool ?? false,
hearted: details["hearted"]?.boolValue ?? false, hearted: details["hearted"]?.bool ?? false,
likeCount: details["likeCount"]?.intValue ?? 0, likeCount: details["likeCount"]?.int ?? 0,
text: details["commentText"]?.stringValue ?? "", text: details["commentText"]?.string ?? "",
repliesPage: details["repliesPage"]?.stringValue, repliesPage: details["repliesPage"]?.string,
channel: Channel(id: channelId, name: author) channel: Channel(id: channelId, name: author)
) )
} }

View File

@ -529,7 +529,8 @@ final class PlayerModel: ObservableObject {
func updateCurrentArtwork() { func updateCurrentArtwork() {
guard let video = currentVideo, guard let video = currentVideo,
let thumbnailData = try? Data(contentsOf: video.thumbnailURL(quality: .medium)!) let thumbnailURL = video.thumbnailURL(quality: .medium),
let thumbnailData = try? Data(contentsOf: thumbnailURL)
else { else {
return return
} }