Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@toshia
Created September 2, 2020 14:52
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 toshia/448d99603743830cde4604a905b5ede2 to your computer and use it in GitHub Desktop.
Save toshia/448d99603743830cde4604a905b5ede2 to your computer and use it in GitHub Desktop.
ガストソーシャルから、直近のガストタイトルの画像を取得して、マイクラのガストのテクスチャ画像を出力する
# frozen_string_literal: true
require 'nokogiri'
require 'open-uri'
require 'gtk2'
require 'cairo'
Point = Struct.new(:x, :y)
# ガストのどの面に何枚目の広告を貼るか
POSITION = [
Point.new(1, 1), # 全面
Point.new(0, 1), # 左面
Point.new(2, 1), # 右面
Point.new(3, 1), # 後面
Point.new(1, 0), # 上面
Point.new(2, 0), # 底面
]
SIZE = 128
# 画像ダウンロードしてPixbufを返す。
def download_pixbuf_fit(url, rect)
URI.open(url) do |blob|
loader = GdkPixbuf::PixbufLoader.new
loader.write(blob.read)
loader.close
pixbuf = loader.pixbuf
pixbuf.scale(*calc_fit(pixbuf, rect))
end
end
def calc_fit(src, dst)
if (dst.width * src.height) < (dst.height * src.width)
return src.width * dst.height / src.height, dst.height
else
return dst.width, src.height * dst.width / src.width
end
end
html = URI.open('https://social.gust.co.jp/pc/gust/', &:read)
doc = Nokogiri::HTML.parse(html)
surface = Cairo::ImageSurface.new(SIZE*4, SIZE*2)
context = Cairo::Context.new(surface)
doc.css('#navi-image a img').lazy.map { |img| img['src'] }.take(6).zip(POSITION) do |path, pos|
pixbuf = download_pixbuf_fit("https://social.gust.co.jp#{path}", Gdk::Rectangle.new(0, 0, SIZE, SIZE))
context.save do
context.translate(pos.x * SIZE, pos.y * SIZE)
context.clip do
context.rectangle(0,0,SIZE,SIZE)
context.translate(SIZE/2 - pixbuf.width/2, SIZE/2 - pixbuf.height/2)
context.set_source_pixbuf(pixbuf)
end
context.paint
end
end
# 7つ目は、バナー画像を取り出して脚にする
path = doc.css('#ca-container .ca-item-7 a img').first&.[]('src')
pixbuf = download_pixbuf_fit("https://social.gust.co.jp#{path}", Gdk::Rectangle.new(0, 0, SIZE/2, SIZE/2))
context.save do
context.translate(SIZE/2,0)
context.save do
context.rectangle(0,0,SIZE,SIZE)
context.rotate(Math::PI/2)
context.clip do
context.rectangle(0,0,SIZE,SIZE/2)
context.set_source_pixbuf(pixbuf)
end
context.paint
end
end
surface.write_to_png("assets/minecraft/textures/entity/ghast/ghast.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment