Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Do the whole param thing
- Param(
- [string]$configlocation,
- [switch]$bugger,
- [switch]$simulate,
- [switch]$resetdate
- )
- # Initialize Variables
- $downloading = $true #Variable to exit the YT download loop
- $iran = $true #Variable that checks if the script ran
- $count = 0 #Counter for the Link Parser
- $usercount = 0 #Counter for the User link to Vid link converter
- $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
- $launchedlinks = New-Object System.Collections.ArrayList #Arraylist that is used for comparison
- $rootdir = $PSScriptRoot #Finds the root directory
- $rootdri = ((Get-Location).Path.Split(":")).Get(0)+':' #Builds a driveletter for pathing
- $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
- #Debug Header
- if ($bugger){
- Write-Host "\\\\>>INITIALIZING DEBUG MODE>>"
- Write-Host "DB: Currently operating from $rootdir"
- }
- #Load the config from a location
- if ($configLocation -ne $null) {
- if (Test-Path $configlocation) {$config = Get-Content $configLocation}
- else {Write-Host "No Such Config, using Default" ; $configLocation = $null}
- }
- if ($configLocation -eq $null) {
- Write-Host "Checking default Config Location"
- if (Test-Path "$rootdir\bin\scriptconf.txt") {
- Write-Host "Config File Found!"
- $configLocation = "$rootdir\bin\scriptconf.txt"
- $config = Get-Content $configLocation
- }
- else {Write-Output "No Default Config File Found, using Script Default"}
- }
- if ($config -ne $null) {
- if($bugger){Write-Host "DB: Config Loaded"}
- ForEach ($configuration in $config) {
- if ($bugger) {
- if($configuration -eq $null) {
- Write-Host "DB: Config cmd: Null"
- } else {
- Write-Host "DB: Config cmd: $configuration"
- }
- }
- if (($configuration -match "#") -or ($configuration -eq $null)) {continue}
- try {Invoke-Expression $configuration}
- catch {$failed += $configuration}
- }
- } elseif ($configLocation -ne $null) {
- if (Test-Path $configlocation) {
- $config = Get-Content $configLocation
- if($bugger){Write-Host "DB: Pulled Config from Default Location"}
- }
- else {Write-Host "No Such Path" ; $configLocation = $null}
- }
- if ($configLocation -eq $null) {
- $config = @'
- $simulate = $false
- $douser = $true
- $dolinks = $true
- $dolists
- $doupdate = $true
- $dlpath = "$rootdri\Downloads"
- $saveform = '%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s'
- $allowedthreads = 8
- $archiveLocation = "$rootdir\archive"
- $alwaysdlLocation = "$rootdir\alwaysdl"
- $currentlinksLocation = "$rootdir\currentlinks"
- $backuplinksLocation = "$rootdir\backuplinks"
- $ytdlLocation = "$rootdir\youtube-dl.exe"
- $ytdlconfLocation = "$rootdir\ytdlconf"
- $keepLogs = $false
- $logsdir = "$rootdir\logs"
- $foxtrotflag = "-f bestvideo`"[height<=1080]`"+bestaudio"
- $ffmpegLocation = ""
- $externalDownloader = ""
- '@
- }
- if ($bugger -and ($failed -ne $null)) { Write-Host "DB: `$failed Commands include: $failed" }
- if ($allowedthreads -eq 0) {Write-Host "Allowed threads equals $allowedthreads" ; break}
- if ($resetdate) {$dldate = $null;if($bugger){Write-Host "DB: dldate reset via Flag"}}
- #Build $stdflags from various variables
- #Build $oscarflag - Sample: -o "..\dlvideos\%(uploader)s\%(uploader)s - %(upload_date)s - %(title)s.%(ext)s"
- $oscarflag = " -o `"$dlpath\$saveform`""
- $stdflags = $foxtrotflag + " --download-archive $archiveLocation" + $oscarflag + $ffmpegLocation + $externalDownloader
- if($bugger){Write-Host "DB: Current Flags:" $stdflags;Write-Host}
- #Grab New and Reference Content from information files
- if (Test-Path $currentlinksLocation) { $newlinks = Get-Content $currentlinksLocation}
- else{ $newlinks = $null; Write-Host "The currentlinks file is missing!`n"}
- if (Test-Path $archiveLocation) {$archive = Get-Content $archiveLocation }
- else {$archive = $null; Write-Host "No archive file available!"}
- if (Test-Path $alwaysdlLocation) { $userlinks = Get-Content $alwaysdlLocation}
- else {$userlinks = $null; Write-Host "No userlinks file available!"}
- # Check for new version of Youtube-DL
- if ($doupdate) {
- Write-Host "Checking for new Youtube-DL Version`n"
- Start-Process -Filepath $ytdlLocation -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)) {
- if($bugger -and ($newlinks -ne $null)) {
- $newlinks | Out-File -FilePath $backuplinksLocation
- Write-Host "DB: Backed up currentlinks"
- }
- elseif ($newlinks -eq $null) {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 ($archive -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 $currentlinksLocation
- }
- else {Write-Host "Currentlinks disabled in scriptconf.txt"}
- # Launch Youtube-DL
- $doytdl = ($dldate -ne $today) -or ($wrlinks.Count -gt 0)
- if ($dldate -eq $today) {Write-Host "Ignoring Userlinks"}
- elseif ($dldate -ne $null) {Write-Host "Downloading videos after $dldate"}
- else {
- Write-Host "Never Downloaded users before."
- #Begin super sketch method of preventing accidental "DOWNLOAD ALL" accidents
- #Can currently be broken by typing weird things in the date prompt
- #Such as any valid YTDL flag. This will run YTDL with that flag
- #Anything else should just cause it to fail and possibly get caught in a boot loop
- #and now I've made this worse by making a flag to force this prompt
- $pro = Read-Host "Are you sure you want to download all videos? (Type Yes to proceed):"
- $pro.ToUpper()
- if (-not(($pro -eq "YES") -or ($pro -eq "Y"))) {$dldate = Read-Host "Please enter date to download from (YYYYMMDD):"}
- else {Write-Host "Downloading all user videos."}
- }
- if($doytdl) {Write-Host "Launching Youtube-DL`n"}
- #This makes an array of link objects with url, id, and linktype
- $wrlinks = $userlinks + $wrlinks
- $objlinks = New-Object System.Collections.ArrayList
- ForEach ($link in $wrlinks) {
- if ($link -like "https://www.youtube.com/user/*") {$id = $link -replace 'https://www.youtube.com/user/',''; $linktype = "channel"}
- elseif ($link -like "https://www.youtube.com/watch?v=*") {$id = $link.replace('https://www.youtube.com/watch?v=',''); $linktype = "video"}
- elseif ($link -like "*&index=*") {$linktype = "playlist"}
- $objlink = New-Object PSObject -Property @{
- URL = $link
- ID = $id
- LinkType = $linktype
- }
- 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}
- $objlinks += $objlink
- }
- # Mechanism for downloading videos
- $downloading = $true
- while ($downloading) {
- if(($procs.count -eq 0) -and ($launchedlinks.count -eq $objlinks.count - 1)) {$downloading = $false}
- ForEach ($link in $objlinks) {
- 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}
- $procs = Get-WmiObject Win32_process -Filter "name = 'youtube-dl.exe'"
- if ($procs.count -eq 0 -and ($launchedlinks.count -eq $objlinks.count - 1)) {break}
- if ($procs.count -ge $allowedthreads) {break}
- if ($launchedlinks -contains $link) {continue}
- $tempid = $link.ID
- #Download Channels
- if ($link.LinkType = "channel" -and $douser) {
- $args = $link.URL + " -i --config-location $ytdlconfLocation --dateafter $dldate $stdflags"
- if($simulate) {$args += " -s"}
- New-Variable "$tempid-log" -Value "NO LOG"
- Write-Host "$tempid Downloading..."
- Start-Process $ytdlLocation -ArgumentList ($args) -WindowStyle Hidden -RedirectStandardOutput "$logsdir\stdout_$tempid.txt"
- $launchedlinks += $link
- #Download Individual Videos
- } elseif ($link.LinkType = "video" -and $dolinks) {
- $args = $link.URL + " --config-location $ytdlconfLocation $stdflags"
- if($simulate) {$args += " -s"}
- 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}
- Write-Host "$tempid Downloading..."
- Start-Process $ytdlLocation -ArgumentList ($args) -WindowStyle Hidden -RedirectStandardOutput "$logsdir\stdout_$tempid.txt"
- $launchedlinks += $link
- } else {$launchedlinks += $link; continue}
- }
- #Still need to differentiate types of videos below
- #Need a proper "Process has exited" indicator, currently indicates only if the script terminates it
- while (($procs.count -gt 0)) {
- $procs = Get-WmiObject Win32_process -Filter "name = 'youtube-dl.exe'"
- if ($procs.count -ge $allowedthreads) {break}
- ForEach ($proc in $procs) {
- ForEach ($link in $objlinks) {
- 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}
- if ($link.LinkType -eq "channel") {
- $log = "$logsdir\stdout_" + $link.ID + ".txt"
- $logcheck = (@(Get-Content $log -ErrorAction SilentlyContinue | Where-Object {$_.Contains("not in range")}).count -gt 0 )
- $tempurl = $link.URL
- if (($proc.Commandline -like "*$tempurl*") -and $logcheck) {
- $proc.Terminate() | Out-Null
- Write-Host $link.ID " Terminated"
- Start-Sleep -m 20
- }
- } elseif ($link.LinkType -eq "video") { $placeholder ++ }
- elseif ($link.LinkType -eq "playlist") { $placeholder2 ++ }
- }
- if ($proc.HasExited) {Write-Host "$tempid Finished"}
- }
- }
- $procs = Get-WmiObject Win32_process -Filter "name = 'youtube-dl.exe'"
- $iran = $true
- }
- if ($iran) {
- $config[4] = '$dldate = ' + $today
- $config | Out-File $configlocation
- if((-not $keepLogs) -and (-not $simulate)){Remove-Item "$logsdir\*.*" -ea SilentlyContinue}
- }
- 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