Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function to URL encode the search query
- function Encode-Url($string) {
- [System.Net.WebUtility]::UrlEncode($string)
- }
- # Function to fetch the first YouTube video URL for a search query
- function Get-YouTubeURL($query) {
- $encodedQuery = Encode-Url $query
- try {
- $response = Invoke-WebRequest -Uri "https://www.youtube.com/results?search_query=$encodedQuery" -UseBasicParsing
- $content = $response.Content
- } catch {
- Write-Error "Failed to fetch YouTube search results: $_"
- return $null
- }
- # Extract the first video URL using regex
- $videoPath = $content | Select-String -Pattern '/watch\?v=[\w-]+' | Select-Object -First 1 -ExpandProperty Matches | ForEach-Object { $_.Value }
- if ($videoPath) {
- # Extract the video ID and construct the shortened URL without "https://"
- $videoId = $videoPath -replace '/watch\?v=', ''
- return "youtu.be/$videoId"
- } else {
- return $null
- }
- }
- # Get the current playing TIDAL song and process its title
- Get-Process -Name "TIDAL" |
- Where-Object { $_.MainWindowTitle -ne "" } |
- Select-Object -ExpandProperty MainWindowTitle |
- ForEach-Object {
- # Split the title at the hyphen, trim any spaces, and reverse the order
- $parts = $_ -split ' - '
- if ($parts.Length -eq 2) {
- # Reverse the parts and join them back with " - " in between
- $reversedTitle = "$($parts[1].Trim()) - $($parts[0].Trim())"
- } else {
- # If there's no hyphen, keep the title as is
- $reversedTitle = $_
- }
- # Fetch the YouTube URL for the reversed title
- $youtubeURL = Get-YouTubeURL $reversedTitle
- # Output the result with the URL appended
- if ($youtubeURL) {
- "/me now playing in FLAC: $reversedTitle ($youtubeURL)"
- } else {
- "/me now playing in FLAC: $reversedTitle (YouTube URL not found)"
- }
- } |
- Out-String |
- ForEach-Object { $_ -replace "`r`n", "" } | # Remove newline characters
- clip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement