Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Last active May 4, 2016 10:12
Show Gist options
  • Select an option

  • Save Akkiesoft/6f270460604e0614f476 to your computer and use it in GitHub Desktop.

Select an option

Save Akkiesoft/6f270460604e0614f476 to your computer and use it in GitHub Desktop.
【これはふるい。新しいのはここ→ https://github.com/Akkiesoft/mikutter_pictcollect 】mikutter画像これくしょんプラグイン(mikutter3.2以降用)の開発中。
# 画像これくしょん(mikutter3.2以降)
require "open-uri"
require "FileUtils"
# From http://d.hatena.ne.jp/gan2/20080531/1212227507
def save_file(url, filename)
# これより、ファイルを開きます
open(filename, 'wb') do |file|
open(url) do |data|
file.write(data.read)
end
end
end
Plugin.create(:mikutter_pictcollect) do
defactivity "pictcollect", "画像これくしょん"
command(
:mikutter_pictcollect,
name: '画像をコレクションする',
condition: lambda{ |opt| true },
visible: true,
role: :timeline
) do |opt|
begin
savedir = UserConfig[:collect_savedir]
# true か false で答えてください
if (! FileTest.exist?(savedir))
#あなたはね疑惑のエラーと呼ばれてるけど疑惑の総合商社ですよ!
raise "設定されているディレクトリが存在しません"
end
# 保存先ディレクトリの取得と必要に応じて/の補完
if savedir !=~ /\/$/
savedir = savedir + "/"
end
# 選択されたツイートに対してそれぞれ実行
opt.messages.each { |message|
# スレッド作成
# ツイートに含まれる画像のURLを取得
urls = message.entity.select{ |entity| %i<urls media>.include? entity[:slug] }
urls.each_with_index { |url, i|
saved = 0
case url[:slug]
when :media
# pic.twitter.com
saveurl = url[:media_url] + ":orig"
savebase = url[:expanded_url]
savebase =~ %r{http://twitter.com/(.+)/status/(.+)/photo/([0-9]+)}
filename = "#{savedir}#{$~[1]}_#{$~[2]}_#{i+1}" + File.extname(url[:media_url])
Thread.new(saveurl) { |saveurl|
save_file(saveurl, filename)
}
saved = 1
when :urls
# 他の画像サービス系
pair = Plugin.filtering(:openimg_raw_image_from_display_url, url[:expanded_url], nil)
_, stream = *pair
saveurl = stream.path
savebase = url[:expanded_url]
url = stream.path
puts stream.path
# File.(ここで力尽きている)
end
if saved
activity :pictcollect, "ほぞんした!! #{filename}"
end
}
}
rescue => msg
# 嘘!TLで全世界の人が見てるんです!
activity :pictcollect, msg.to_s
end
end
settings "画像これくしょん" do
input("画像を保存するディレクトリ",:collect_savedir)
end
end
# 以上を持ちまして、画像これくしょんプラグインに対する記述は、終了いたしました
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment