Files
playlet-piped/playlet-lib/src/components/Services/Invidious/InvidiousToContentNode.bs
T
Brahim Hadricheandgithub-action linter 7d1ba07286 Cast using Youtube app (DIAL/LoungeAPI) (#276)
* Dial+Lounge API PoC

* format

* Fix for chrome

* comments

* Rate limit and logger stuff

* rename route

* remove invalid routes

* remove device image

* Simplify device description

* command test

* Lint fix

* register pairing code

* tv code UI

* Lint fix

* Avoid creating lots of objects

* dpad command

* small refactor

* tweak

* validation

* timer

* fix

* Revert "timer"

This reverts commit e349565910882408898c9bddda55dadaa9d743de.

* Commands

* setVolume command

* lounge service timer

* Some state messages

* Lint fix

* player state sync

* voice handler

* Adjustments, and add a TODO list

* Lint fix

* comment

* play at timestamp

* Remote screen UI

* UI adjustment

* Small refactor

* Refactor long polling

* Add TODO

* Manage logging a bit better to avoid spam in DIAL server

* cache dial file

* logging

* comment

* Join lounge lazily

* long poller tweak

* ui tweak

* set shouldQuit field

* remote connect notification

* Lint fix

* Randomize device id, new device on start

* Lint fix

* Lounge disconnect on start

* Speed up lounge by not blocking on sent messages

* add version to lounge data saved

* dial server fix rendezvous

* todo

* no message

* Display network name

* playlist changes

* Lint fix

* tweak params

* tweak

* player state

* comments

* Refactor Video Queue (#295)

* Refactor Video Queue

* Video queue and playlists

* Web app casting

* queue playlist notification

* VideoQueueView work

* restore properties

* changes

* queue notification and previous/next state

* Lint fix

* very basic error handling

* Update changelog and privacy policy

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2024-02-19 16:30:48 -05:00

305 lines
11 KiB
Plaintext

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