Skip to content

Instantly share code, notes, and snippets.

@1d10t
Last active March 21, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1d10t/6f8b151e50905449c63abc851dceb167 to your computer and use it in GitHub Desktop.
Save 1d10t/6f8b151e50905449c63abc851dceb167 to your computer and use it in GitHub Desktop.
This script downloading remote MP3 files from M3U playlist and saving it with cute file names
fileext_from_url(url, default_ext = "mp3")
{
url_wo_query := RegExReplace(url, "(\?.*)?(#.*)?$", "")
SplitPath, url_wo_query, , , , fn
return, % fn "." ext_from_url(url, default_ext)
}
ext_from_url(url, default = "mp3")
{
url_wo_query := RegExReplace(url, "(\?.*)?(#.*)?$", "")
SplitPath, url_wo_query, , , ext
if ( ext = "" )
{
ext := default
}
return %ext%
}
FileSelectFile, m3u_file_path, 3, , Open a M3U playlist file, Playlist (*.m3u; *.m3u8)
if ( ErrorLevel or m3u_file_path = "" )
{
MsgBox, No playlist selected
return
}
m3u_file_name := fileext_from_url(m3u_file_path)
Menu, Tray, Tip, %m3u_file_name%
FileSelectFolder, output_folder, , 1, Select output folder for downloaded files
if ( ErrorLevel or output_folder = "" )
{
MsgBox, No output directory selected
return
}
output_folder := RegExReplace(output_folder, "\\$")
FileEncoding, UTF-8
next_title := ""
file := ""
title_sanitized := ""
default_ext := "mp3"
Loop
{
FileReadLine, line, %m3u_file_path%, %A_Index%
if ErrorLevel
break
Menu, Tray, Tip, %m3u_file_name% line #%A_Index%
if( RegExMatch(line, "i)^#EXT") )
{
; meta information
if( RegExMatch(line, "iO)^#EXTINF:([^,]*),\s*(.*)$", matchobj) )
{
; msgbox, % "found title " matchobj.Value(2)
next_title := matchobj.Value(2)
}
continue
}
url := line
If ( RegExMatch(line, "i)^data:") )
{
TrayTip, Skip downloading, %url%, 10, 2
continue
}
; msgbox, % "url is " url
; msgbox, % fileext_from_url(url, default_ext)
if(next_title = "")
{
file := output_folder "\" fileext_from_url(url, default_ext)
}
else
{
title_sanitized := RegExReplace(next_title, "[^\w\d\-\(\)\[\]\.\, а-яА-ЯёЁ]", "_")
file := output_folder "\" title_sanitized "." ext_from_url(url, default_ext)
}
next_title := ""
IfExist, %file%
{
TrayTip, Skip downloading, to %file%`nfrom %url%, 10, 2
continue
}
; msgbox, % "file is " file
TrayTip, Downloading..., to %file%`nfrom %url%, 30, 1
UrlDownloadToFile, %url%, %file%
if ( ErrorLevel )
{
TrayTip, Error downloading, to %file%`nfrom %url%, 30, 3
continue
}
FileGetSize, fsize , %file%, K
if ( ErrorLevel or fsize < 1 )
{
TrayTip, Error size, %file%, 30, 3
FileDelete, file
continue
}
}
MsgBox, All files are downloaded
Run, explore %output_folder%
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment