Advertisement
snick512

TIDAL + YouTube

Dec 27th, 2024
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.07 KB | Source Code | 0 0
  1. # Function to URL encode the search query
  2. function Encode-Url($string) {
  3.     [System.Net.WebUtility]::UrlEncode($string)
  4. }
  5.  
  6. # Function to fetch the first YouTube video URL for a search query
  7. function Get-YouTubeURL($query) {
  8.     $encodedQuery = Encode-Url $query
  9.  
  10.     try {
  11.         $response = Invoke-WebRequest -Uri "https://www.youtube.com/results?search_query=$encodedQuery" -UseBasicParsing
  12.         $content = $response.Content
  13.     } catch {
  14.         Write-Error "Failed to fetch YouTube search results: $_"
  15.         return $null
  16.     }
  17.  
  18.     # Extract the first video URL using regex
  19.     $videoPath = $content | Select-String -Pattern '/watch\?v=[\w-]+' | Select-Object -First 1 -ExpandProperty Matches | ForEach-Object { $_.Value }
  20.  
  21.     if ($videoPath) {
  22.         # Extract the video ID and construct the shortened URL without "https://"
  23.         $videoId = $videoPath -replace '/watch\?v=', ''
  24.         return "youtu.be/$videoId"
  25.     } else {
  26.         return $null
  27.     }
  28. }
  29.  
  30. # Get the current playing TIDAL song and process its title
  31. Get-Process -Name "TIDAL" |
  32.     Where-Object { $_.MainWindowTitle -ne "" } |
  33.     Select-Object -ExpandProperty MainWindowTitle |
  34.     ForEach-Object {
  35.         # Split the title at the hyphen, trim any spaces, and reverse the order
  36.         $parts = $_ -split ' - '
  37.         if ($parts.Length -eq 2) {
  38.             # Reverse the parts and join them back with " - " in between
  39.             $reversedTitle = "$($parts[1].Trim()) - $($parts[0].Trim())"
  40.         } else {
  41.             # If there's no hyphen, keep the title as is
  42.             $reversedTitle = $_
  43.         }
  44.  
  45.         # Fetch the YouTube URL for the reversed title
  46.         $youtubeURL = Get-YouTubeURL $reversedTitle
  47.  
  48.         # Output the result with the URL appended
  49.         if ($youtubeURL) {
  50.             "/me now playing in FLAC: $reversedTitle ($youtubeURL)"
  51.         } else {
  52.             "/me now playing in FLAC: $reversedTitle (YouTube URL not found)"
  53.         }
  54.     } |
  55.     Out-String |
  56.     ForEach-Object { $_ -replace "`r`n", "" } |  # Remove newline characters
  57.     clip
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement