Advertisement
atheos42

Save-RedditVideo

Jul 23rd, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Clear-Host
  2.  
  3. function New-RandomFileName{
  4.     Param ( [Parameter(Mandatory=0)][string]$Path ="$ENV:Temp",
  5.             [Parameter(Mandatory=0)][string]$Extension = '.tmp',
  6.             [Parameter(Mandatory=0)][int]$MaxLen = 7
  7.     )
  8.  
  9.     if($Extension[0] -ne '.') { $Extension = $('.' + $Extension) }
  10.     return Join-Path $Path $(((New-Guid).Guid).replace('-',(0..9 | Get-Random)).SubString(0,$MaxLen) + $Extension)
  11. }
  12.  
  13. function Save-RedditVideo{
  14.     param(
  15.         [Parameter(Mandatory=$true, ValueFromPipeline=$true, HelpMessage="url", Position=0)]
  16.         [string]$Url  
  17.     )
  18.  
  19.     try{
  20.         $d = Invoke-RestMethod "$Url.json"
  21.         $link = $d[0].data.children.data.secure_media.reddit_video.fallback_url
  22.         Write-Output $link
  23.         $fn = New-RandomFileName -Extension '.mp4'
  24.         Write-Output $fn
  25.         [System.Net.WebClient]::New().DownloadFile($link, $fn)
  26.         #Invoke-WebRequest $link -Out $fn
  27.         Invoke-Expression $fn
  28.     } catch {
  29.         $_.Exception.Message | Write-Warning
  30.         return $null
  31.     }
  32. }
  33.  
  34. Save-RedditVideo 'https://www.reddit.com/r/ThisLooksFun/comments/w0glkr/thislooksfun/'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement