Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Created November 9, 2017 15:25
Show Gist options
  • Save Akkiesoft/135fc0e44efcdb05701d761beda5a945 to your computer and use it in GitHub Desktop.
Save Akkiesoft/135fc0e44efcdb05701d761beda5a945 to your computer and use it in GitHub Desktop.
ツイートをLZMA圧縮してBASE64にしてツイートするプラグイン。zlibよりはもう少し効率が良いけどBASE64がまあ。
# -*- frozen_string_literal: true -*-
source 'https://rubygems.org'
gem 'ruby-lzma', '0.4.3'
# -*- coding: utf-8 -*-
require 'lzma'
require 'base64'
Plugin.create(:tweet_lzma) do
def deflate(string)
dst = LZMA.compress(string)
dst64 = Base64.encode64(dst)
#dst64.chomp!
dst
end
def inflate(string)
dst = Base64.decode64(string)
buf = LZMA.decompress(dst)
buf
end
defactivity "tweet_lzma", "tweet_lzma"
command(:tweet_lzma,
name: 'LZMA圧縮してツイートする',
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_lzma, "文字数がオーバーしています(#{deflated.length}文字)"
else
Service.primary.update(:message => deflated)
Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text = ''
end
end
end
command(:tweet_lzma_count,
name: 'LZMA圧縮後の文字数を確認する',
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_lzma, "圧縮後の文字列: #{deflated}"
activity :tweet_lzma, "いまは#{length}文字くらいです"
end
end
command(:tweet_lzma_deflate,
name: 'LZMA圧縮された文字列を展開する',
condition: lambda{ |opt| true },
visible: true,
role: :timeline) do |opt|
begin
opt.messages.each do |message|
inflated = inflate("#{message}")
activity :tweet_lzma, "#{inflated}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment