Created
April 9, 2024 06:56
-
-
Save JaHIY/5d009675ff848e651f192cb5ac41fad9 to your computer and use it in GitHub Desktop.
bilibili-emote-downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
def download(URL from, File to) { | |
if (to.exists()) { | |
println "Skip `${to.canonicalPath}`"; | |
} else { | |
to.createParentDirectories(); | |
println "Download `${to.canonicalPath}` from `${from.toString()}`"; | |
to << from.openStream(); | |
} | |
return to; | |
} | |
def saveDirectory = './emote'; | |
1.upto(5000) { id -> | |
def result = new URI("https://api.bilibili.com/x/emote/package?business=reply&ids=${id}") | |
.toURL() | |
.openConnection() | |
.inputStream | |
.text; | |
def slurper = new JsonSlurper(); | |
def packages = slurper.parseText(result).data.packages; | |
packages?.each { pkg -> | |
def pkgName = pkg.text; | |
pkg.emote?.findAll { | |
it.type != 4 | |
}.each { emote -> | |
def url = new URI(emote.url).toURL(); | |
def emoteName = emote.text.replaceAll(/(^\[|\]$)/, ""); | |
def ext = emote.url.find(/\.\w+$/); | |
def savePath = new File("${saveDirectory}/${pkgName}/${emoteName}${ext}"); | |
download(url, savePath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment