Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Last active November 9, 2017 15:28
Show Gist options
  • Save Akkiesoft/ab1c196f4fe7f7050b6b3b74cab37a15 to your computer and use it in GitHub Desktop.
Save Akkiesoft/ab1c196f4fe7f7050b6b3b74cab37a15 to your computer and use it in GitHub Desktop.
ツイートをzlib圧縮してBASE64エンコードでツイートするmikutterプラグイン。デコードはまだ。
# -*- coding: utf-8 -*-
require 'zlib'
require 'base64'
Plugin.create(:tweet_zlib) do
def deflate(string)
z = Zlib::Deflate.new(9)
dst = z.deflate(string, Zlib::FINISH)
z.close
dst64 = Base64.encode64(dst)
dst64
end
def inflate(string)
dst = Base64.decode64(string)
z = Zlib::Inflate.new
buf = z.inflate(dst)
z.finish
z.close
buf
end
defactivity "tweet_zlib", "tweet_zlib"
command(:tweet_zlib,
name: '圧縮してツイートする',
condition: lambda{ |opt| true },
visible: true,
role: :postbox) do |opt|
begin
message = Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text
deflated = deflate(message)
if 280 < deflated.length then
activity :tweet_zlib, "文字数がオーバーしています(#{deflated.length}文字)"
else
Service.primary.update(:message => deflated)
Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text = ''
end
end
end
command(:tweet_zlib_count,
name: '圧縮後の文字数を確認する',
condition: lambda{ |opt| true },
visible: true,
role: :postbox) do |opt|
begin
message = Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text
deflated = deflate(message)
length = deflated.length
activity :tweet_zlib, "圧縮後の文字列: #{deflated}"
activity :tweet_zlib, "いまは#{length}文字くらいです"
end
end
command(:tweet_zlib_deflate,
name: '圧縮された文字列を展開する',
condition: lambda{ |opt| true },
visible: true,
role: :timeline) do |opt|
begin
opt.messages.each do |message|
inflated = inflate("#{message}")
activity :tweet_zlib, "#{inflated}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment