Skip to content

Instantly share code, notes, and snippets.

@aNNiMON
Last active September 26, 2022 10:16
Show Gist options
  • Save aNNiMON/59fea725645ce9b1740b7da590dbbd28 to your computer and use it in GitHub Desktop.
Save aNNiMON/59fea725645ce9b1740b7da590dbbd28 to your computer and use it in GitHub Desktop.
AudioDL

AudioDL

Downloads music from Telegram to specified directory

*.own linguist-language=Scala
audiodl.json
own-modules
{
"saveFolderPath": "/path/to/music/directory",
"botToken":"1234567890:AABBCCDDEE"
}
use ["std", "http", "json", "files", "functional", "java", "types", "downloader"]
include "own-modules/telegram-bot/TelegramBot.own"
AudioFileIO = newClass("org.jaudiotagger.audio.AudioFileIO")
FieldKey = newClass("org.jaudiotagger.tag.FieldKey")
File = newClass("java.io.File")
StandardArtwork = newClass("org.jaudiotagger.tag.images.StandardArtwork")
MBYTES = 1024.0 * 1024.0
f = fopen("audiodl.json", "r")
config = jsondecode(readText(f))
// config.saveFolderPath = "E:/"
fclose(f)
bot = new TelegramBot(config.botToken)
// Get audio files sent to bot
files = stream(bot.getUpdatesSync())
.filter(def(u) = u.message.audio ?? false)
.map(def(u) = u.message.audio)
.filter(def(a) = (a.mime_type ?? "") == "audio/mpeg")
.map(def(a) {
a.performer = a.performer ?? "Unknown artist"
a.title = a.title ?? "Unknown track"
a.fullname = a.performer + " - " + a.title
a.path = config.saveFolderPath + a.fullname + ".mp3"
a.file_size = a.file_size ?? 0
return a
})
// Filter already stored audio files
.filter(def(a) = !exists(a.path))
.toArray()
if (files.isEmpty()) {
println "No audio found. Send some music to your bot"
}
// Read user's input
println "Save path: " + config.saveFolderPath
println "0. Exit"
for a, index : files {
println sprintf("%d. %s (%.2f MiB)",
index + 1, a.fullname,
a.file_size / MBYTES)
}
if (!files.isEmpty()) {
println "a|all. Download all"
}
print "Choose: "
choice = default(readln(), "0")
choiceParts = match choice.lower {
case "all": range(1, files.length + 1)
case "a": range(1, files.length + 1)
case _: choice.split("(,| )")
}
stream(choiceParts)
.map(::trim)
.filter(def(s) = !s.isEmpty())
.map(def(s) = parseInt(s) - 1)
.filter(def(i) = (0 <= i && i < files.length))
.forEach(def(i) = storeAudio(files[i]))
def storeAudio(a) {
println "\nProcessing " + a.fullname
println "Downloading..."
filePath = bot.getFileSync(a.file_id).file_path
url = bot.toFileUrl(filePath)
downloader(url, a.path)
println "Download finished"
thumbPath = ""
if (arrayKeyExists("thumb", a)) {
println "Found artwork, downloading..."
thumbTgFilePath = bot.getFileSync(a.thumb.file_id).file_path
thumbPath = "thumb.jpg"
status = downloader(bot.toFileUrl(thumbTgFilePath), thumbPath)
println "Artwork downloaded"
}
println "Writing tags..."
writeTags(a.path, a.performer, a.title, thumbPath)
println "Tags wrote"
if exists(thumbPath) {
delete(thumbPath)
}
}
def writeTags(path, artist, title, thumbPath = "") {
af = AudioFileIO.read(new File(path))
tag = default(af.getTag(), af.createDefaultTag())
emptyTag = def(key) = string(tag.getFirst(key)).trim().isEmpty()
artistKey = FieldKey.valueOf("ARTIST")
if (emptyTag(artistKey)) {
tag.setField(artistKey, [artist])
}
titleKey = FieldKey.valueOf("TITLE")
if (emptyTag(titleKey)) {
tag.setField(titleKey, [title])
}
if !thumbPath.isEmpty() {
art = StandardArtwork.createArtworkFromFile(new File(thumbPath))
tag.setField(art)
}
af.setTag(tag)
AudioFileIO.write(af)
}
[
{
"id": "18f1894447dfa72000a83011511a817c",
"name": "telegram-bot"
}
]
use ["std"]
if (ARGS.length >= 2 && ARGS[0] == "owm") {
use ["files", "json", "java"]
File = newClass("java.io.File")
runtime = newClass("java.lang.Runtime").getRuntime()
def loadModulesJson(path = "modules.json") {
f = fopen(path, "r")
modules = jsondecode(readText(f))
fclose(f)
return modules
}
def exec(cmd, dir = ".") = runtime.exec(cmd, null, new File(dir))
match (ARGS[1]) {
case "install": {
modulesDir = "own-modules"
if (!exists(modulesDir)) {
mkdir(modulesDir)
}
for module : loadModulesJson() {
print module.name
moduleDir = modulesDir + "/" + module.name
if (!exists(moduleDir)) {
mkdir(moduleDir)
cmd = "git clone https://gist.github.com/" + module.id + ".git " + module.name
exec(cmd, modulesDir)
println " installed"
} else {
exec("git pull origin master", moduleDir)
println " updated"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment