Advertisement
notuserfriendly

YT_Run

Nov 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Initialize Variables
  2. $terminate= $true                                   #Exit variable for the DoUntil Loop
  3. $simulate = $false                                  #Variable to prevent yt execution
  4. $count = 0                                          #Counter for the Link Parser
  5. $usercount = 0                                      #Counter for the User link to Vid link converter
  6. $confcnt = 0                                        #Counter for Configuration File items
  7. $allowedthreads = 4                                 #Total number of Userdownloads that can happen concurrently Set to 0 for unlimited
  8. $wrlinks = New-Object System.Collections.ArrayList  #The Arraylist that is passed to Youtube-DL
  9. $userlinks = New-Object System.Collections.ArrayList #The Arraylist used for user items
  10.  
  11. $DEBUG = $false                                     #Debug Bool
  12. $rootdir = $PSScriptRoot                            #finds the root directory
  13. $rootdri = ((Get-Location).Path.Split(":")).Get(0)+':'  #builds a driveletter for pathing
  14.  
  15. $dldate = 19691231                                  #This is backup in case dldate is not specified in $config
  16. $dlrelpath                                          #this is the variable from scriptconf.txt that specifies where to put videos
  17. $dlpath                                             #This variable is built from config file terms and the rootpath
  18. $saveform                                           #this variable contains the format to save files in
  19. $oscarflag                                          #this is the complete -o flag for ytdl
  20.  
  21. $today = Get-Date -UFormat "%Y%m%d"                 #This contains the date the script is being run on
  22. $userlinkspull = $null                              #This array contains links pulled from user channels
  23.  
  24. #Check for, then set various config files
  25. if (Test-Path "$rootdir\alwaysdl")
  26. {$userlinks = Get-Content "$rootdir\alwaysdl" }     #This array has links for Users that are always downloaded
  27.  
  28. if (Test-Path "$rootdir\scriptconf.txt") {
  29.     $config = Get-Content "$rootdir\scriptconf.txt" #Grabs the config file, or makes it null
  30. }else { $PS
  31.     $config = $null
  32.     $DEBUG = $true
  33. }
  34.  
  35.  
  36. #Set important variables from the config file
  37. if ($config -ne $null) {
  38.     ForEach ($item in $config) {
  39.         if (($item -match '=') -and ($item -notmatch '#')) {
  40.             $cmd = $item
  41.             if ($DEBUG) {Write-Host "Executing Script Configuration: "; Write-Host "Running $cmd"}
  42.             try {Invoke-Expression $cmd}
  43.             catch {$failed.Add($cmd)}
  44.         }
  45.     }
  46.     Write-Host
  47. }
  48. else {
  49.     $config = "#I'm an empty config file. You can initialize user adjustable variables here"
  50.     $dlpath = "$rootdir\downloads"
  51.     $doupdate = $true
  52.     $douser = $false
  53.     $dolinks = $true
  54.     $saveform = '%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s'
  55.  
  56. }
  57. #Debug Header
  58. if ($DEBUG){
  59.     Write-Host "\\\\>>INITIALIZING DEBUG MODE>>"
  60.     Write-Host "Currently operating from $rootdir"
  61. }
  62.  
  63. if ($DEBUG -and ($failed -ne $null)) { Write-Host "Debug: `$failed Commands include: $failed" }
  64.  
  65. $confcnt ++
  66.  
  67. #Build $oscarflag from various variables
  68. #Sample: -o "..\dlvideos\%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s"
  69. $oscarflag = " -o `"$dlpath\$saveform`""
  70.  
  71. #Grab New and Reference Content from information files
  72. if (Test-Path "$rootdir\currentlinks") { $newlinks = Get-Content "$rootdir\currentlinks"}
  73. else{ $newlinks = $null; Write-Host "The currentlinks file is empty!`n"}
  74. if (Test-Path "$rootdir\SystemArchive") {$oldlinks = Get-Content "$rootdir\SystemArchive" }
  75. else {$oldlinks = $null; Write-Host "No archive availible!"}
  76.    
  77. # Check for new version of Youtube-DL
  78. if ($doupdate) {
  79.     Write-Host "Checking for new Youtube-DL Version`n"
  80.     Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ("-U") -NoNewWindow -Wait
  81.     Write-Host "`nCheck Complete`n"
  82. }
  83.  
  84. #This needs to be changed to include the alwaysdl options
  85. if (($newlinks -ne $null) -or ($userlinks -ne $null)) {
  86.     #Backs up links. This may end up being a debug option
  87.     if($newlinks -ne $null) {
  88.         $newlinks | Out-File -FilePath "$rootdir\backuplinks";
  89.         if ($DEBUG) {Write-Host "Debug: Backed up currentlinks"}
  90.     }
  91.     else {Write-Host "Failed to backup currentlinks, as there are no currentlinks" }
  92.        
  93.        
  94.     # Clears link arraylist to allow fresh links to be written
  95.     $wrlinks.Clear()
  96.  
  97.     # Add links not in SystemArchive from currentlinks
  98.     if ($dolinks) {
  99.         ForEach ($newlink in $newlinks) {
  100.             $newlink = $newlink -replace '&index=\d{1,3}&list=\w{1,34}',''
  101.             $newlink = $newlink.Replace('https://www.youtube.com/watch?v=', 'youtube ')
  102.             if ($oldlinks -contains $newlink) {$count ++}
  103.             else {
  104.                 $newlink = $newlink.Replace("youtube ", "https://www.youtube.com/watch?v=")
  105.                 $wrlinks.Add($newlink) | Out-Null
  106.             }
  107.         }
  108.  
  109.         if ($count -ne 0) {Write-Host "Successfully Removed $count Links"}
  110.         else {Write-Host "No Links Removed"}
  111.         $wrlinks | Out-File -FilePath "$rootdir\currentlinks"
  112.     }
  113.     else {Write-Host "Currentlinks disabled in scriptconf.txt"}
  114.  
  115.     # Launch Youtube-DL
  116.     $doytdl = ($dldate -ne $today) -or ($wrlinks.Count -gt 0)
  117.     if ($dldate -ne 19691231) {Write-Host "Last Downloaded users on $dldate"}
  118.     else {Write-Host "Never Downloaded users before"}
  119.     if($doytdl) {Write-Host "Launching Youtube-DL`n"}
  120.  
  121.         # Mechanism for downloading channel videos
  122.     if ($douser -and ($dldate -ne $today)) {
  123.         Write-Host "Downloading User Videos"
  124.         $counter = 0
  125.         ForEach ($user in $userlinks) {
  126.             if ($allowedthreads -eq 0) {$allowedthreads = $userlinks.Count}
  127.             $args = "$user -i --config-location $rootdir\ytdlconf --dateafter $dldate $oscarflag"
  128.  
  129.             if ($counter -lt $allowedthreads) {
  130.                 Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args)
  131.             } elseif ($counter -eq $allowedthreads) {
  132.                 $counter = 0
  133.                 Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args) -Wait
  134.                 Write-Host "Pausing Script."
  135.             } elseif ($counter -eq ($allowedthreads - 1)) {
  136.                 Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args) -Wait
  137.                 Write-Host "Running User download Threads"
  138.             }
  139.             $counter ++
  140.         }
  141.         $config[4] = '$dldate = ' + $today
  142.         $config | Out-File "$rootdir\scriptconf.txt"
  143.     }
  144.  
  145.         # Mechanism for downloading individual videos
  146.     if ($dolinks -and ($wrlinks.Count -gt 0)){
  147.         Write-Host "Downloading Individual Videos`n"
  148.         if ($DEBUG) {Write-Host "%%   $wrlinks   %%"}
  149.         $args = "$wrlinks --config-location $rootdir\ytdlconf $oscarflag"
  150.         Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args) -NoNewWindow -Wait
  151.     }
  152.     if (-not ($dolinks -and $douser)) { Write-Host "No Links Set to download!" }
  153.     if($doytdl) {Write-Host "`nYoutube-DL Terminated`n"}
  154. }
  155. else {Write-Host "`nNo links set for download!`n"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement