From 9d844bbf3ac6be327325b13a91a6b33f73c49c1d Mon Sep 17 00:00:00 2001 From: RblSb Date: Sun, 28 Apr 2024 07:23:25 +0300 Subject: Raw youtube fallback for unavailable videos Also: - fix `tryLocalIp` replacement (NAT workaround) - improve proxy headers a bit - use json2object fork for better generated diffs --- src/utils/YoutubeUtils.hx | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/utils/YoutubeUtils.hx (limited to 'src/utils') diff --git a/src/utils/YoutubeUtils.hx b/src/utils/YoutubeUtils.hx new file mode 100644 index 0000000..7429565 --- /dev/null +++ b/src/utils/YoutubeUtils.hx @@ -0,0 +1,83 @@ +package utils; + +typedef YoutubeVideoDetails = { + viewCount:String, + videoId:String, + title:String, + thumbnail:{ + thumbnails:Array<{ + url:String, + width:Int, + height:Int, + }> + }, + shortDescription:String, + lengthSeconds:String, + keywords:Array, + isUnpluggedCorpus:Bool, + isPrivate:Bool, + isOwnerViewing:Bool, + isLiveContent:Bool, + isCrawlable:Bool, + channelId:String, + author:String, + allowRatings:Bool +} + +typedef YoutubeVideoFormat = { + ?signatureCipher:String, + itag:Int, + width:Int, + height:Int, + url:String, + qualityLabel:String, // 240p, 1080p, etc + quality:String, + projectionType:String, + mimeType:String, + lastModified:String, + bitrate:Int, + approxDurationMs:String, + ?initRange:{start:Int, end:Int}, + ?indexRange:{start:Int, end:Int}, + ?audioQuality:String, // AUDIO_QUALITY_LOW + ?audioSampleRate:Int, + ?audioChannels:Int +} + +typedef YouTubeVideoInfo = { + public var videoDetails:YoutubeVideoDetails; + public var ?formats:Array; + public var ?adaptiveFormats:Array; + public var ?liveData:{ + manifestUrl:String, + }; +} + +class YoutubeUtils { + static final matchId = ~/youtube\.com.*v=([A-z0-9_-]+)/; + static final matchShort = ~/youtu\.be\/([A-z0-9_-]+)/; + static final matchShorts = ~/youtube\.com\/shorts\/([A-z0-9_-]+)/; + static final matchEmbed = ~/youtube\.com\/embed\/([A-z0-9_-]+)/; + static final matchPlaylist = ~/youtube\.com.*list=([A-z0-9_-]+)/; + + public static function extractVideoId(url:String):String { + if (matchId.match(url)) { + return matchId.matched(1); + } + if (matchShort.match(url)) { + return matchShort.matched(1); + } + if (matchShorts.match(url)) { + return matchShorts.matched(1); + } + if (matchEmbed.match(url)) { + return matchEmbed.matched(1); + } + return ""; + } + + public static function extractPlaylistId(url:String):String { + if (!matchPlaylist.match(url)) return ""; + return matchPlaylist.matched(1); + } +} -- cgit v1.2.3