import "pkg:/components/VideoFeed/FeedLoadState.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/TimeUtils.bs" namespace InvidiousContent function ToRowCellContentNode(item as object, instance as dynamic) as object if item.videoId <> invalid and (item.type = invalid or item.type = "shortVideo") item.type = "video" end if if item.playlistId <> invalid and (item.type = invalid or item.type = "invidiousPlaylist") item.type = "playlist" end if if item.type <> "video" and item.type <> "playlist" and item.type <> "channel" return invalid end if if item.type = "video" return ToVideoContentNode(invalid, item, instance) else if item.type = "playlist" return ToPlaylistContentNode(invalid, item, instance) else if item.type = "channel" return ToChannelContentNode(invalid, item, instance) else return invalid end if end function function ToVideoContentNode(node as object, item as object, instance as dynamic) as object if node = invalid node = CreateObject("roSGNode", "VideoContentNode") end if node.type = "video" ' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation. SetIfExists(node, "_author", item, "author") SetIfExists(node, "authorId", item, "authorId") SetIfExists(node, "lengthSeconds", item, "lengthSeconds") node.lengthText = TimeUtils.GetFormattedTime(item.lengthSeconds) node.liveNow = VideoIsLive(item) VideoSetPremiereTimestampText(node, item) SetIfExists(node, "publishedText", item, "publishedText") VideoSetThumbnail(node, item, instance) SetIfExists(node, "title", item, "title") SetIfExists(node, "timestamp", item, "timestamp") SetIfExists(node, "id", item, "videoId") SetIfExists(node, "videoId", item, "videoId") node.viewCountText = VideoGetViewCountText(item) SetIfExists(node, "index", item, "index") return node end function function ToPlaylistContentNode(node as object, item as object, instance as dynamic) as object if node = invalid node = CreateObject("roSGNode", "PlaylistContentNode") node.loadState = FeedLoadState.None end if node.type = "playlist" ' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation. SetIfExists(node, "_author", item, "author") SetIfExists(node, "authorId", item, "authorId") SetIfExists(node, "description", item, "description") SetIfExists(node, "id", item, "playlistId") SetIfExists(node, "playlistId", item, "playlistId") SetIfExists(node, "title", item, "title") SetIfExists(node, "videoCount", item, "videoCount") node.videoCountText = GetVideoCountText(item) SetIfExists(node, "viewCount", item, "viewCount") node.viewCountText = PlaylistGetViewCountText(item) SetIfExists(node, "updated", item, "updated") node.updatedText = PlaylistGetUpdatedText(item) if item.videos <> invalid childCount = node.getChildCount() newNodes = [] for each video in item.videos video.type = "video" videoNode = ToVideoContentNode(invalid, video, instance) if videoNode <> invalid index = video.index if index <> invalid and index > -1 and index < childCount node.replaceChild(videoNode, index) else newNodes.push(videoNode) end if end if end for if newNodes.Count() > 0 node.appendChildren(newNodes) end if end if PlaylistSetThumbnail(node, "thumbnail", item, instance) PlaylistSetThumbnail(node, "thumbnailBackground", item, instance, "maxres") return node end function function ToChannelContentNode(node as object, item as object, instance as dynamic) as object if node = invalid node = CreateObject("roSGNode", "ChannelContentNode") node.loadState = FeedLoadState.None end if node.type = "channel" ' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation. SetIfExists(node, "_author", item, "author") SetIfExists(node, "id", item, "authorId") SetIfExists(node, "authorId", item, "authorId") SetIfExists(node, "channelHandle", item, "channelHandle") SetIfExists(node, "description", item, "description") SetIfExists(node, "subCount", item, "subCount") node.subCountText = GetChannelSubCountText(item) SetIfExists(node, "videoCount", item, "videoCount") node.videoCountText = GetVideoCountText(item) node.thumbnail = GetChannelThumbnail(item.authorThumbnails) node.banner = GetChannelBanner(item.authorBanners) SetIfExists(node, "tabs", item, "tabs") return node end function function SetIfExists(dst as object, dstField as string, src as object, srcField as string) if src.DoesExist(srcField) dst[dstField] = src[srcField] end if end function function VideoSetThumbnail(node as object, videoItem as object, instance as dynamic, quality = "medium" as string) as boolean videoThumbnails = videoItem.videoThumbnails if videoThumbnails = invalid or videoThumbnails.Count() = 0 if StringUtils.IsNullOrEmpty(instance) node.thumbnail = "pkg:/images/thumbnail-missing.jpg" else node.thumbnail = `${instance}/vi/${videoItem.videoId}/mqdefault.jpg` end if return false end if url = invalid for each thumbnail in videoThumbnails if thumbnail.quality = quality url = thumbnail.url exit for end if end for if url = invalid url = videoThumbnails[0].url end if if url.startsWith("/") if StringUtils.IsNullOrEmpty(instance) return false end if url = instance + url end if node.thumbnail = url return true end function function VideoIsLive(videoItem as object) as boolean if videoItem.liveNow = true return true end if ' TODO:P2 metadata.liveNow is returning false for live videos... return videoItem.lengthSeconds = 0 and videoItem.viewCount = 0 end function function VideoIsUpcoming(videoItem as object) as boolean return videoItem.isUpcoming = true and ValidInt(videoItem.premiereTimestamp) <> 0 end function function VideoSetPremiereTimestampText(node as object, videoItem as object) if VideoIsUpcoming(videoItem) node.isUpcoming = true node.premiereTimestampText = `Premeres in ${TimeUtils.GetFormattedTimeLeft(videoItem.premiereTimestamp)}` end if end function function VideoGetViewCountText(videoItem as object) as string viewCount = videoItem.viewCount if viewCount = invalid return "" end if if viewCount < 1000 return `${FormatFloat(viewCount)} views` end if viewCount = viewCount / 1000 if viewCount < 1000 return `${FormatFloat(viewCount)}K views` end if viewCount = viewCount / 1000 if viewCount < 1000 return `${FormatFloat(viewCount)}M views` end if viewCount = viewCount / 1000 return `${FormatFloat(viewCount)}B views` end function function FormatFloat(X as float) as float X = X * 10 X = X + 0.5 X = Fix(X) X = X / 10 return X end function function PlaylistSetThumbnail(node as object, nodeField as string, playlistItem as object, instance as dynamic, quality = "medium" as string) as void thumbnail = "" if IsString(playlistItem.playlistThumbnail) thumbnailUrl = playlistItem.playlistThumbnail if thumbnailUrl.startsWith("/") and not StringUtils.IsNullOrEmpty(playlistItem.instance) thumbnailUrl = playlistItem.instance + thumbnailUrl end if thumbnail = thumbnailUrl else if playlistItem.videos <> invalid and playlistItem.videos.Count() > 0 and playlistItem.videos[0].index = 0 if VideoSetThumbnail(node, playlistItem.videos[0], instance, quality) return end if else if node.getChildCount() > 0 thumbnail = node.getChild(0).thumbnail end if if StringUtils.IsNullOrEmpty(thumbnail) thumbnail = "pkg:/images/thumbnail-missing.jpg" end if node[nodeField] = thumbnail end function function GetChannelThumbnail(authorThumbnails as object) as string if authorThumbnails = invalid or authorThumbnails.Count() = 0 return "" end if url = authorThumbnails[authorThumbnails.Count() - 1].url if url.startsWith("//") url = "https:" + url end if return url end function function GetChannelBanner(authorBanners as object) as string if authorBanners = invalid or authorBanners.Count() = 0 return "" end if return authorBanners[0].url end function function GetVideoCountText(item as object) as string videoCount = ValidInt(item.videoCount) return videoCount = 1 ? "1 video" : `${videoCount} videos` end function function GetChannelSubCountText(channel as object) as string subCount = ValidInt(channel.subCount) return subCount = 1 ? "1 subscriber" : `${subCount} subscribers` end function function PlaylistGetViewCountText(playlistItem as object) as string return VideoGetViewCountText(playlistItem) end function function PlaylistGetUpdatedText(playlistItem as object) as string updated = playlistItem.updated if not IsInt(updated) return "" end if span = TimeUtils.Now().AsSeconds() - updated if span < 1 return "" end if count = "" total_days = span \ 86400 if total_days > 365 years = total_days \ 365 count = years > 1 ? `${years} years` : "1 year" else if total_days > 30 months = total_days \ 30 count = months > 1 ? `${months} months` : "1 month" else if total_days > 7 weeks = total_days \ 7 count = weeks > 1 ? `${weeks} weeks` : "1 week" else if total_days > 1 count = `${total_days} days` else if span > 3600 hours = span \ 3600 count = hours > 1 ? `${hours} hours` : "1 hour" else if span > 60 minutes = span \ 60 count = minutes > 1 ? `${minutes} minutes` : "1 minute" else count = span > 1 ? `${span} seconds` : "1 second" end if if StringUtils.IsNullOrEmpty(count) return "" end if return `Updated ${count} ago` end function end namespace