diff options
| author | RblSb <msrblsb@gmail.com> | 2024-04-28 07:23:25 +0300 |
|---|---|---|
| committer | RblSb <msrblsb@gmail.com> | 2024-04-28 21:51:50 +0300 |
| commit | 9d844bbf3ac6be327325b13a91a6b33f73c49c1d (patch) | |
| tree | 52108f2300ca84decf33a1e7b3552e81166ba5ac /src/utils | |
| parent | 8679f8edcb6d2f3142db30848c640aed6fe883b8 (diff) | |
Raw youtube fallback for unavailable videos
Also:
- fix `tryLocalIp` replacement (NAT workaround)
- improve proxy headers a bit
- use json2object fork for better generated diffs
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/YoutubeUtils.hx | 83 |
1 files changed, 83 insertions, 0 deletions
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<String>, + 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<YoutubeVideoFormat>; + public var ?adaptiveFormats:Array<YoutubeVideoFormat>; + 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); + } +} |
