Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Initialize Variables
- $terminate= $true #Exit variable for the DoUntil Loop
- $simulate = $false #Variable to prevent yt execution
- $count = 0 #Counter for the Link Parser
- $usercount = 0 #Counter for the User link to Vid link converter
- $confcnt = 0 #Counter for Configuration File items
- $allowedthreads = 4 #Total number of Userdownloads that can happen concurrently Set to 0 for unlimited
- $wrlinks = New-Object System.Collections.ArrayList #The Arraylist that is passed to Youtube-DL
- $userlinks = New-Object System.Collections.ArrayList #The Arraylist used for user items
- $DEBUG = $false #Debug Bool
- $rootdir = $PSScriptRoot #finds the root directory
- $rootdri = ((Get-Location).Path.Split(":")).Get(0)+':' #builds a driveletter for pathing
- $dldate = 19691231 #This is backup in case dldate is not specified in $config
- $dlrelpath #this is the variable from scriptconf.txt that specifies where to put videos
- $dlpath #This variable is built from config file terms and the rootpath
- $saveform #this variable contains the format to save files in
- $oscarflag #this is the complete -o flag for ytdl
- $today = Get-Date -UFormat "%Y%m%d" #This contains the date the script is being run on
- $userlinkspull = $null #This array contains links pulled from user channels
- #Check for, then set various config files
- if (Test-Path "$rootdir\alwaysdl")
- {$userlinks = Get-Content "$rootdir\alwaysdl" } #This array has links for Users that are always downloaded
- if (Test-Path "$rootdir\scriptconf.txt") {
- $config = Get-Content "$rootdir\scriptconf.txt" #Grabs the config file, or makes it null
- }else { $PS
- $config = $null
- $DEBUG = $true
- }
- #Set important variables from the config file
- if ($config -ne $null) {
- ForEach ($item in $config) {
- if (($item -match '=') -and ($item -notmatch '#')) {
- $cmd = $item
- if ($DEBUG) {Write-Host "Executing Script Configuration: "; Write-Host "Running $cmd"}
- try {Invoke-Expression $cmd}
- catch {$failed.Add($cmd)}
- }
- }
- Write-Host
- }
- else {
- $config = "#I'm an empty config file. You can initialize user adjustable variables here"
- $dlpath = "$rootdir\downloads"
- $doupdate = $true
- $douser = $false
- $dolinks = $true
- $saveform = '%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s'
- }
- #Debug Header
- if ($DEBUG){
- Write-Host "\\\\>>INITIALIZING DEBUG MODE>>"
- Write-Host "Currently operating from $rootdir"
- }
- if ($DEBUG -and ($failed -ne $null)) { Write-Host "Debug: `$failed Commands include: $failed" }
- $confcnt ++
- #Build $oscarflag from various variables
- #Sample: -o "..\dlvideos\%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s"
- $oscarflag = " -o `"$dlpath\$saveform`""
- #Grab New and Reference Content from information files
- if (Test-Path "$rootdir\currentlinks") { $newlinks = Get-Content "$rootdir\currentlinks"}
- else{ $newlinks = $null; Write-Host "The currentlinks file is empty!`n"}
- if (Test-Path "$rootdir\SystemArchive") {$oldlinks = Get-Content "$rootdir\SystemArchive" }
- else {$oldlinks = $null; Write-Host "No archive availible!"}
- # Check for new version of Youtube-DL
- if ($doupdate) {
- Write-Host "Checking for new Youtube-DL Version`n"
- Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ("-U") -NoNewWindow -Wait
- Write-Host "`nCheck Complete`n"
- }
- #This needs to be changed to include the alwaysdl options
- if (($newlinks -ne $null) -or ($userlinks -ne $null)) {
- #Backs up links. This may end up being a debug option
- if($newlinks -ne $null) {
- $newlinks | Out-File -FilePath "$rootdir\backuplinks";
- if ($DEBUG) {Write-Host "Debug: Backed up currentlinks"}
- }
- else {Write-Host "Failed to backup currentlinks, as there are no currentlinks" }
- # Clears link arraylist to allow fresh links to be written
- $wrlinks.Clear()
- # Add links not in SystemArchive from currentlinks
- if ($dolinks) {
- ForEach ($newlink in $newlinks) {
- $newlink = $newlink -replace '&index=\d{1,3}&list=\w{1,34}',''
- $newlink = $newlink.Replace('https://www.youtube.com/watch?v=', 'youtube ')
- if ($oldlinks -contains $newlink) {$count ++}
- else {
- $newlink = $newlink.Replace("youtube ", "https://www.youtube.com/watch?v=")
- $wrlinks.Add($newlink) | Out-Null
- }
- }
- if ($count -ne 0) {Write-Host "Successfully Removed $count Links"}
- else {Write-Host "No Links Removed"}
- $wrlinks | Out-File -FilePath "$rootdir\currentlinks"
- }
- else {Write-Host "Currentlinks disabled in scriptconf.txt"}
- # Launch Youtube-DL
- $doytdl = ($dldate -ne $today) -or ($wrlinks.Count -gt 0)
- if ($dldate -ne 19691231) {Write-Host "Last Downloaded users on $dldate"}
- else {Write-Host "Never Downloaded users before"}
- if($doytdl) {Write-Host "Launching Youtube-DL`n"}
- # Mechanism for downloading channel videos
- if ($douser -and ($dldate -ne $today)) {
- Write-Host "Downloading User Videos"
- $counter = 0
- ForEach ($user in $userlinks) {
- if ($allowedthreads -eq 0) {$allowedthreads = $userlinks.Count}
- $args = "$user -i --config-location $rootdir\ytdlconf --dateafter $dldate $oscarflag"
- if ($counter -lt $allowedthreads) {
- Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args)
- } elseif ($counter -eq $allowedthreads) {
- $counter = 0
- Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args) -Wait
- Write-Host "Pausing Script."
- } elseif ($counter -eq ($allowedthreads - 1)) {
- Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args) -Wait
- Write-Host "Running User download Threads"
- }
- $counter ++
- }
- $config[4] = '$dldate = ' + $today
- $config | Out-File "$rootdir\scriptconf.txt"
- }
- # Mechanism for downloading individual videos
- if ($dolinks -and ($wrlinks.Count -gt 0)){
- Write-Host "Downloading Individual Videos`n"
- if ($DEBUG) {Write-Host "%% $wrlinks %%"}
- $args = "$wrlinks --config-location $rootdir\ytdlconf $oscarflag"
- Start-Process -Filepath $rootdir\youtube-dl.exe -ArgumentList ($args) -NoNewWindow -Wait
- }
- if (-not ($dolinks -and $douser)) { Write-Host "No Links Set to download!" }
- if($doytdl) {Write-Host "`nYoutube-DL Terminated`n"}
- }
- else {Write-Host "`nNo links set for download!`n"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement