Skip to content

Instantly share code, notes, and snippets.

@JaHIY
Last active May 30, 2022 16:30
Show Gist options
  • Save JaHIY/cf7257906568ee5ef80049044e294f71 to your computer and use it in GitHub Desktop.
Save JaHIY/cf7257906568ee5ef80049044e294f71 to your computer and use it in GitHub Desktop.
bilibili emoji downloader
#!/usr/bin/env ruby
require 'fileutils'
require 'json'
require 'net/http'
$download_dir = 'bilibili_emojis'
$cookie = 'SESSDATA=PUT_YOUR_SESSDATA_HERE'
def download(url, filename)
if File.exists? filename
puts "#{filename} is existed, so skip it."
return
end
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.request_get(uri.path) do | res |
if not res.is_a?(Net::HTTPOK)
puts res
return
end
FileUtils.mkdir_p File.dirname(filename)
File.open(filename, 'wb') do | file |
file.binmode
res.read_body do | chunk |
file << chunk
end
end
puts "#{filename} downloaded complete"
end
end
uri = URI('http://api.bilibili.com/x/emote/setting/panel')
params = { 'business' => 'reply' }
uri.query = URI.encode_www_form(params)
headers = { 'cookie' => $cookie }
text = Net::HTTP.get(uri, headers)
emote = JSON.parse(text)
all_packages = emote['data']['all_packages']
emoji_packages = all_packages.select do | package |
package['type'] != 4
end.map do | package |
emoji = package['emote'].map do | em |
{:name => em['text'], :url => em['url']}
end
{:name => package['text'], :emoji => emoji}
end
emoji_packages.each do | package |
package[:emoji].each do | emoji |
download(emoji[:url], "#{$download_dir}/#{package[:name]}/#{emoji[:name].sub(/^\[(.*)\]$/, '\1')}#{File.extname(emoji[:url])}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment