-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzd.ps1
114 lines (94 loc) · 4.25 KB
/
zd.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
# Zotify Downloader wrapper - OPUS FORMAT
# parameter = Spotify-URL
# Assumes installation of LLzotify https://github.com/lolo83560/LLzotify
param (
[string]$url
)
# if no parameters give, display usage info
if ($psboundparameters.count -eq 0)
{
"Usage: ZD `<spotify URL`>"
exit
}
# trim ?si=... option at the end of url
# these occur when copied from spot/spotx app
# e.g. 'https://open.spotify.com/playlist/3DuMUKpBVP6lIaQEEmye5j?si=933eb6420de54795
$url = $url -replace '(.*?)\?.*$','$1'
# retrieve page title from Spotify URL webpage
$page = Invoke-webrequest -Uri $url
$page.content -match "<title>(.+)</title>" | out-null # get the <title> section
$title = $matches[1]
$title -match "(.*) \| Spotify" | out-null # remove the ' | Spotify' right part
$temppath = $matches[1]
# replace problem chars for filenames [ < > : " / \ | ? * ] with a dash
$localpath = $temppath -replace '[\<\>\:\"\/\\\|\?\*]','-'
#dbg write-host [$url] [$localPath]
# from the URL, get the download type (track/album/playlist/episode/artist) & Spotify ID, transform into URI
$url -match "^.*/(.*?)/(.*?)$" | out-null
$DLDtype = $matches[1]
$spotID = $matches[2]
$spoturi = "spotify:" + $DLDtype + ":" + $spotID
#dbg write-host [$DLDtype] [$spotID] [$spoturi]
$rootpath = ".\\" # + $localpath
$output = switch ($DLDtype) {
"playlist" { "{playlist}/{playlist_num} - {artist} - {song_name}.opus" ; break}
"artist" { "{artist}/{album}/{track_number} - {song_name}.opus" ; break}
"album" { "{artist}/{album}/{track_number} - {song_name}.opus" ; break}
"track" { "{artist}/{album} - {track_number} - {song_name}.opus" ; break}
"episode" { "{artist}/{song_name}.opus"; break}
default { "{artist}/{song_name}.opus"; break}
}
# make unique name for logfile, generated from $localpath hash
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($localpath))).Replace("-", "").ToLower()
$logfilename = 'log-'+$hash+'.txt'
# now go ahead loop it till completion
$iteration = 1
$startime = get-date
do {
# add command line on top of logfile for easier rerun
"zd $url" | out-file $logfilename
$("=" * 80)
$("-" * $localpath.length)
$localpath
$("-" * $localpath.length)
">>>> ITERATION $iteration <<<<"
$("=" * 80)
"Log file name: $logfilename"
"URL: $url"
$("=" * 80)
# if ($iteration -gt 1) { # beginnning at 2nd iteration, pause 30s to let Spotify cool down
# " "
# "~~~~~~~ waiting 30s to let Spotify server cool down :) ~~~~~~~"
# " "
# start-sleep -seconds 30
# }
# run Zotify download on url
# assume spotify credentials stored into C:\Users\<your_name>\AppData\Roaming\Zotify\credentials.json
# Hint: to regenerate
# -> use librespot-auth making sure turning firewall off
# install this one, better https://github.com/ManiArasteh/librespot-auth.git
zotify --root-path $rootpath --output $output --retry-attempts 5 --print-download-progress=false --download-lyrics=false --download-format opus --download-quality high $spoturi 2>&1 | Tee-Object -file $logfilename -append
$logerr = get-content $logfilename
$iteration++
"ErrGal " + ($logerr -match 'GENERAL DOWNLOAD ERROR').count
"ErrKey " + ($logerr -match 'Audio key error').count
"ErrCod " + ($logerr -match 'Traceback \(most recent call last\)').count
}
while (
(($logerr -match 'GENERAL DOWNLOAD ERROR').count -ne 0) -or
(($logerr -match 'Audio key error').count -ne 0) -or
(($logerr -match 'Traceback \(most recent call last\)').count -ne 0)
)
$stoptime = get-date
remove-item -force $logfilename
" "
"██████ ██████ ███ ██ ███████ ██ "
"██ ██ ██ ██ ████ ██ ██ ██ "
"██ ██ ██ ██ ██ ██ ██ █████ ██ "
"██ ██ ██ ██ ██ ██ ██ ██ "
"██████ ██████ ██ ████ ███████ ██ "
" "
$deltime = ( "{0:hh\:mm\:ss}" -f [timespan]::fromseconds(($stoptime-$startime).totalseconds) )
'Time taken H:M:S: '+$deltime
"Iterations : $iteration"