Advertisement
notuserfriendly

Start-YtdlManager

Nov 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Do the whole param thing
  2. Param(
  3.     [string]$configlocation,
  4.     [switch]$bugger,
  5.     [switch]$simulate,
  6.     [switch]$resetdate
  7. )
  8.  
  9. # Initialize Variables
  10. $downloading = $true                                #Variable to exit the YT download loop
  11. $iran = $true                                       #Variable that checks if the script ran
  12. $count = 0                                          #Counter for the Link Parser
  13. $usercount = 0                                      #Counter for the User link to Vid link converter
  14. $wrlinks = New-Object System.Collections.ArrayList  #The Arraylist that is passed to Youtube-DL
  15. $userlinks = New-Object System.Collections.ArrayList #The Arraylist used for user items
  16. $launchedlinks = New-Object System.Collections.ArrayList #Arraylist that is used for comparison
  17.  
  18. $rootdir = $PSScriptRoot                            #Finds the root directory
  19. $rootdri = ((Get-Location).Path.Split(":")).Get(0)+':'  #Builds a driveletter for pathing
  20.  
  21. $dlpath                                             #This variable is built from config file terms and the rootpath
  22. $saveform                                           #This variable contains the format to save files in
  23. $oscarflag                                          #This is the complete -o flag for ytdl
  24.  
  25. $today = Get-Date -UFormat "%Y%m%d"                 #This contains the date the script is being run on
  26.  
  27. #Debug Header
  28. if ($bugger){
  29.     Write-Host "\\\\>>INITIALIZING DEBUG MODE>>"
  30.     Write-Host "DB: Currently operating from $rootdir"
  31. }
  32.  
  33. #Load the config from a location
  34.  
  35. if ($configLocation -ne $null) {
  36.     if (Test-Path $configlocation) {$config = Get-Content $configLocation}
  37.     else {Write-Host "No Such Config, using Default" ; $configLocation = $null}
  38. }
  39. if ($configLocation -eq $null) {
  40.     Write-Host "Checking default Config Location"
  41.     if (Test-Path "$rootdir\bin\scriptconf.txt") {
  42.         Write-Host "Config File Found!"
  43.         $configLocation = "$rootdir\bin\scriptconf.txt"
  44.         $config = Get-Content $configLocation
  45.     }
  46.     else {Write-Output "No Default Config File Found, using Script Default"}
  47. }
  48.  
  49. if ($config -ne $null) {
  50.     if($bugger){Write-Host "DB: Config Loaded"}
  51.     ForEach ($configuration in $config) {
  52.         if ($bugger) {
  53.             if($configuration -eq $null) {
  54.                 Write-Host "DB: Config cmd: Null"
  55.             } else {
  56.                 Write-Host "DB: Config cmd: $configuration"
  57.             }
  58.         }
  59.         if (($configuration -match "#") -or ($configuration -eq $null)) {continue}
  60.         try {Invoke-Expression $configuration}
  61.         catch {$failed += $configuration}
  62.     }
  63. } elseif ($configLocation -ne $null) {
  64.         if (Test-Path $configlocation) {
  65.             $config = Get-Content $configLocation
  66.             if($bugger){Write-Host "DB: Pulled Config from Default Location"}
  67.         }
  68.         else {Write-Host "No Such Path" ; $configLocation = $null}
  69.     }
  70.     if ($configLocation -eq $null) {
  71.         $config = @'
  72. $simulate = $false
  73. $douser = $true
  74. $dolinks = $true
  75. $dolists
  76. $doupdate = $true
  77. $dlpath = "$rootdri\Downloads"
  78. $saveform = '%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s'
  79. $allowedthreads = 8
  80. $archiveLocation = "$rootdir\archive"
  81. $alwaysdlLocation = "$rootdir\alwaysdl"
  82. $currentlinksLocation = "$rootdir\currentlinks"
  83. $backuplinksLocation = "$rootdir\backuplinks"
  84. $ytdlLocation = "$rootdir\youtube-dl.exe"
  85. $ytdlconfLocation = "$rootdir\ytdlconf"
  86. $keepLogs = $false
  87. $logsdir = "$rootdir\logs"
  88. $foxtrotflag = "-f bestvideo`"[height<=1080]`"+bestaudio"
  89. $ffmpegLocation = ""
  90. $externalDownloader = ""
  91. '@
  92.     }
  93.  
  94. if ($bugger -and ($failed -ne $null)) { Write-Host "DB: `$failed Commands include: $failed" }
  95.  
  96. if ($allowedthreads -eq 0) {Write-Host "Allowed threads equals $allowedthreads" ; break}
  97. if ($resetdate) {$dldate = $null;if($bugger){Write-Host "DB: dldate reset via Flag"}}
  98.  
  99. #Build $stdflags from various variables
  100. #Build $oscarflag - Sample: -o "..\dlvideos\%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s"
  101. $oscarflag = " -o `"$dlpath\$saveform`""
  102. $stdflags = $foxtrotflag + " --download-archive $archiveLocation" + $oscarflag + $ffmpegLocation + $externalDownloader
  103. if($bugger){Write-Host "DB: Current Flags:" $stdflags;Write-Host}
  104.  
  105.  
  106. #Grab New and Reference Content from information files
  107. if (Test-Path $currentlinksLocation) { $newlinks = Get-Content $currentlinksLocation}
  108. else{ $newlinks = $null; Write-Host "The currentlinks file is missing!`n"}
  109. if (Test-Path $archiveLocation) {$archive = Get-Content $archiveLocation }
  110. else {$archive = $null; Write-Host "No archive file available!"}
  111. if (Test-Path $alwaysdlLocation) { $userlinks = Get-Content $alwaysdlLocation}
  112. else {$userlinks = $null; Write-Host "No userlinks file available!"}
  113.    
  114. # Check for new version of Youtube-DL
  115. if ($doupdate) {
  116.     Write-Host "Checking for new Youtube-DL Version`n"
  117.     Start-Process -Filepath $ytdlLocation -ArgumentList ("-U") -NoNewWindow -Wait
  118.     Write-Host "`nCheck Complete`n"
  119. }
  120.  
  121. #This needs to be changed to include the alwaysdl options
  122. if (($newlinks -ne $null) -or ($userlinks -ne $null)) {
  123.     if($bugger -and ($newlinks -ne $null)) {
  124.         $newlinks | Out-File -FilePath $backuplinksLocation
  125.         Write-Host "DB: Backed up currentlinks"
  126.     }
  127.     elseif ($newlinks -eq $null) {Write-Host "Failed to backup currentlinks, as there are no currentlinks" }
  128.        
  129.        
  130.     # Clears link arraylist to allow fresh links to be written
  131.     $wrlinks.Clear()
  132.  
  133.     # Add links not in SystemArchive from currentlinks
  134.     if ($dolinks) {
  135.         ForEach ($newlink in $newlinks) {
  136.             $newlink = $newlink -replace '&index=\d{1,3}&list=\w{1,34}',''
  137.             $newlink = $newlink.Replace('https://www.youtube.com/watch?v=', 'youtube ')
  138.             if ($archive -contains $newlink) {$count ++}
  139.             else {
  140.                 $newlink = $newlink.Replace("youtube ", "https://www.youtube.com/watch?v=")
  141.                 $wrlinks.Add($newlink) | Out-Null
  142.             }
  143.         }
  144.  
  145.         if ($count -ne 0) {Write-Host "Successfully Removed $count Links"}
  146.         else {Write-Host "No Links Removed"}
  147.         $wrlinks | Out-File -FilePath $currentlinksLocation
  148.     }
  149.     else {Write-Host "Currentlinks disabled in scriptconf.txt"}
  150.  
  151.     # Launch Youtube-DL
  152.     $doytdl = ($dldate -ne $today) -or ($wrlinks.Count -gt 0)
  153.     if ($dldate -eq $today) {Write-Host "Ignoring Userlinks"}
  154.     elseif ($dldate -ne $null) {Write-Host "Downloading videos after $dldate"}
  155.     else {
  156.         Write-Host "Never Downloaded users before."
  157.        
  158.         #Begin super sketch method of preventing accidental "DOWNLOAD ALL" accidents
  159.         #Can currently be broken by typing weird things in the date prompt
  160.         #Such as any valid YTDL flag. This will run YTDL with that flag
  161.         #Anything else should just cause it to fail and possibly get caught in a boot loop
  162.         #and now I've made this worse by making a flag to force this prompt
  163.         $pro = Read-Host "Are you sure you want to download all videos? (Type Yes to proceed):"
  164.         $pro.ToUpper()
  165.         if (-not(($pro -eq "YES") -or ($pro -eq "Y"))) {$dldate = Read-Host "Please enter date to download from (YYYYMMDD):"}
  166.         else {Write-Host "Downloading all user videos."}
  167.     }
  168.     if($doytdl) {Write-Host "Launching Youtube-DL`n"}
  169.  
  170.     #This makes an array of link objects with url, id, and linktype
  171.     $wrlinks = $userlinks + $wrlinks
  172.     $objlinks = New-Object System.Collections.ArrayList
  173.  
  174.     ForEach ($link in $wrlinks) {
  175.         if ($link -like "https://www.youtube.com/user/*") {$id = $link -replace 'https://www.youtube.com/user/',''; $linktype = "channel"}
  176.         elseif ($link -like "https://www.youtube.com/watch?v=*") {$id = $link.replace('https://www.youtube.com/watch?v=',''); $linktype = "video"}
  177.         elseif ($link -like "*&index=*") {$linktype = "playlist"}
  178.  
  179.         $objlink = New-Object PSObject -Property @{
  180.             URL = $link
  181.             ID = $id
  182.             LinkType = $linktype
  183.         }
  184.         if($objlink.LinkType -eq $true){Write-Host "JUST MADE"; Write-Host "LINK:" $objlink.URL ; Write-Host "ID:  " $objlink.ID ; Write-Host "TYPE:" $objlink.LinkType; Write-Host}
  185.         $objlinks += $objlink
  186.     }
  187.     # Mechanism for downloading videos
  188.     $downloading = $true
  189.     while ($downloading) {
  190.         if(($procs.count -eq 0) -and ($launchedlinks.count -eq $objlinks.count - 1)) {$downloading = $false}
  191.         ForEach ($link in $objlinks) {
  192.             if($objlink.LinkType -eq $true){Write-Host "LINE 191"; Write-Host "LINK:" $link.URL ; Write-Host "ID:  " $link.ID ; Write-Host "TYPE:" $link.LinkType; Write-Host}
  193.  
  194.             $procs = Get-WmiObject Win32_process -Filter "name = 'youtube-dl.exe'"
  195.            
  196.             if ($procs.count -eq 0 -and ($launchedlinks.count -eq $objlinks.count - 1)) {break}
  197.             if ($procs.count -ge $allowedthreads) {break}
  198.             if ($launchedlinks -contains $link) {continue}
  199.  
  200.             $tempid = $link.ID
  201.  
  202.             #Download Channels
  203.             if ($link.LinkType = "channel" -and $douser) {
  204.                 $args = $link.URL + " -i --config-location $ytdlconfLocation --dateafter $dldate $stdflags"
  205.                 if($simulate) {$args += " -s"}
  206.                 New-Variable "$tempid-log" -Value "NO LOG"
  207.                 Write-Host "$tempid Downloading..."
  208.                 Start-Process $ytdlLocation -ArgumentList ($args) -WindowStyle Hidden -RedirectStandardOutput "$logsdir\stdout_$tempid.txt"
  209.                 $launchedlinks += $link
  210.             #Download Individual Videos
  211.             } elseif ($link.LinkType = "video" -and $dolinks) {
  212.                 $args = $link.URL + " --config-location $ytdlconfLocation $stdflags"
  213.                 if($simulate) {$args += " -s"}
  214.                 if($objlink.LinkType -eq $true){Write-Host "VIDEO LAUNCH ZONE"; Write-Host "LINK:" $link.URL ; Write-Host "ID:  " $link.ID ; Write-Host "TYPE:" $link.LinkType; Write-Host}
  215.                 Write-Host "$tempid Downloading..."
  216.                 Start-Process $ytdlLocation -ArgumentList ($args) -WindowStyle Hidden -RedirectStandardOutput "$logsdir\stdout_$tempid.txt"
  217.                 $launchedlinks += $link
  218.             } else {$launchedlinks += $link; continue}            
  219.         }
  220.         #Still need to differentiate types of videos below
  221.         #Need a proper "Process has exited" indicator, currently indicates only if the script terminates it
  222.  
  223.         while (($procs.count -gt 0)) {
  224.             $procs = Get-WmiObject Win32_process -Filter "name = 'youtube-dl.exe'"
  225.             if ($procs.count -ge $allowedthreads) {break}
  226.            
  227.             ForEach ($proc in $procs) {
  228.                 ForEach ($link in $objlinks) {
  229.                     if($objlink.LinkType -eq $true){Write-Host "LINE 228"; Write-Host "LINK:" $link.URL ; Write-Host "ID:  " $link.ID ; Write-Host "TYPE:" $link.LinkType; Write-Host}
  230.                     if ($link.LinkType -eq "channel") {
  231.                         $log = "$logsdir\stdout_" + $link.ID + ".txt"
  232.                         $logcheck = (@(Get-Content $log -ErrorAction SilentlyContinue | Where-Object {$_.Contains("not in range")}).count -gt 0 )
  233.                         $tempurl = $link.URL
  234.                         if (($proc.Commandline -like "*$tempurl*") -and $logcheck) {
  235.                             $proc.Terminate() | Out-Null
  236.                             Write-Host $link.ID " Terminated"
  237.                             Start-Sleep -m 20
  238.                         }
  239.                     } elseif ($link.LinkType -eq "video") { $placeholder ++ }
  240.                     elseif ($link.LinkType -eq "playlist") { $placeholder2 ++ }
  241.                 }
  242.                 if ($proc.HasExited) {Write-Host "$tempid Finished"}
  243.             }
  244.         }
  245.         $procs = Get-WmiObject Win32_process -Filter "name = 'youtube-dl.exe'"
  246.         $iran = $true
  247.     }
  248.     if ($iran) {
  249.         $config[4] = '$dldate = ' + $today
  250.         $config | Out-File $configlocation
  251.         if((-not $keepLogs) -and (-not $simulate)){Remove-Item "$logsdir\*.*" -ea SilentlyContinue}
  252.     }
  253.     if (-not ($dolinks -and $douser)) { Write-Host "No Links Set to download!" }
  254.     if($doytdl) {Write-Host "`nYoutube-DL Terminated`n"}
  255. }
  256. else {Write-Host "`nNo links set for download!`n"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement