-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry.ps1
174 lines (145 loc) · 4.19 KB
/
country.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
param (
# Playlist URL
[Parameter(Mandatory=$true,
Position=0)]
[Alias("c", "pl", "playlist", "list")]
[String[]]
$Country,
# Country download path
[Parameter(Mandatory=$false)]
[Alias("p", "path", "output", "o")]
[String[]]
$Countryroad="country",
# Path to extracted playlist and other files that the script uses
[Parameter(Mandatory=$false)]
[Alias("m", "meta")]
[String[]]
$Metafiles=".\script_files",
# Skip playing after download
[Parameter(Mandatory=$false)]
[Alias("n","np", "no")]
[Switch]
$noplay,
# Skip fetching playlist
# Really only useful for development
[Parameter(Mandatory=$false)]
[Alias("sl","nolist", "slist")]
[Switch]
$skiplist,
[Parameter(Mandatory=$false)]
[Alias("s", "mute", "shutup", "shut", "fuckoff")]
[Switch]
$silent,
# Use --flat-playlist (speeds up YouTube playlist processing)
# Does not work for Soundcloud :/
# Does not work at all rn
# TODO: Make-URL (youtu.be/$song.Id) when using -yt
[Parameter(Mandatory=$false)]
[Alias("f", "yt")]
[Switch]
$flat
)
class Song {
[string]$Id
[string]$Title
[string]$Url
[string]$Filename
[string]ToString(){
return ("{0} | {1}" -f $this.Id, $this.Title)
}
}
function Play-List {
Write-Host "Playing #Country" -ForegroundColor Green
& 'C:\Program Files (x86)\foobar2000\foobar2000.exe' $Countryroad '/runcmd=Playback/Order/Random'
}
function Make-List {
Write-Host "Fetching playlist" -ForegroundColor Blue
$output = "[%(id)s] %(uploader)s - %(title)s"
if($flat.IsPresent) {
youtube-dl -i --flat-playlist --get-title --get-id --get-filename -o $output --get-url $Country | Out-File "$($Metafiles)\list.txt" -Encoding oem
} else {
youtube-dl -i --get-title --get-id --get-filename -o $output --get-url $Country | Out-File "$($Metafiles)\list.txt" -Encoding oem
}
}
function Get-List-Array {
$array = Get-Content "$($Metafiles)\list.txt" -Raw
$array = $array -split "`r?`n"
return $array
}
function Song-Downloaded($song) {
$songpath = "$($Countryroad)" + '\*' + '``[' + "$($song.Id)" + '``]' + '*.*'
Log-Message("We do a little trolling: ?$($song.Id)?")
return Test-Path $songpath
}
function Download-Song($song) {
Log-Message("Downloading $($song.Title)")
$output = "$($Countryroad)\$($song.Filename).%(ext)s"
$format = "bestaudio"
$quiet = "--quiet"
if(-Not $silent.IsPresent) {
$quiet = ""
}
# Call youtube-dl
youtube-dl `
-o $output `
--download-archive "$($Metafiles)\downloaded.txt" `
--extract-audio `
--ignore-config `
--ignore-errors `
$quiet `
$song.Url
}
function Log-Message($message) {
if(-Not $silent.IsPresent) {
Write-Host $message
}
}
function Country {
# Make script_files directory and downloaded.txt if missing
if(!(Test-Path $Metafiles)) {
New-Item -Path . -Name "$($Metafiles)\" -ItemType "directory"
}
if(!(Test-Path "$($Metafiles)\downloaded.txt")) {
New-Item -Path "$($Metafiles)\" -Name "downloaded.txt" -ItemType "file"
}
# Skip making list if the -skiplist switch is present
if(-Not $skiplist.IsPresent) {
Make-List
}
# Get the list from file
$array = Get-List-Array
# Check if youtube-dl extracted one or two urls
if ($array[3].SubString(0,4) -ne "http") {
Write-Host "Extracting from audio only source" -ForegroundColor Blue
$increment = 4
} else {
Write-Host "Extracting from video + audio source" -ForegroundColor Blue
$increment = 5
}
# Process all songs in the list array
# $array.Length - 1 because Get-Content -Raw gets the last newline
for ($i = 0; $i -lt ($array.Length - 1); ($i = $i + $increment)) {
$song = [Song]::new()
$song.Title = $array[$i]
$song.Id = $array[$i + 1]
$song.Url = $array[$i + $increment - 2]
# Remove brackets from song names smh
$foilname = $array[$i + $increment - 1]
$foilname = $foilname -replace '[\(|\)|\%]'
$song.Filename = $foilname
Write-Host "Processing: [$($song.Id)]" -ForegroundColor Magenta
if(Song-Downloaded($song)) {
Write-Host "$($song.Title) is downloaded" -ForegroundColor DarkGreen
} else {
Write-Host "$($song.Title) is not downloaded" -ForegroundColor Red
Download-Song($song)
}
}
# Finish
Write-Host "All songs downloaded" -ForegroundColor Green
# Play if -np is not present
if(-Not $noplay.IsPresent) {
Play-List
}
}
Country