Skip to content

Instantly share code, notes, and snippets.

@siyo
Created October 19, 2011 04:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siyo/1297496 to your computer and use it in GitHub Desktop.
Save siyo/1297496 to your computer and use it in GitHub Desktop.
指定ユーザーの最近のtweetに対して(reply|favorite|retweet|delete|thread|replace) したいとき用earthquake.gem plugin
# -*- coding: utf-8 -*-
# (reply|favorite|retweet|delete|thread|repalce) to recent tweet of specified user in cache.
# you can specifiy '-n' index.
#
module Earthquake::Input
def recent_status_id(name,index = -1)
a = ("aa".."zz").to_a.inject([]){|s,v|
break s unless id = var2id("$#{v}");
next s unless st = cache.read("status:#{id}");
s << id if st["user"]["screen_name"] && st["user"]["screen_name"] == name
s
}
a.empty? ? nil : a.sort[index]
end
end
Earthquake.init do
# reply
#
# e.g. :at who hi!
# :at who yo! -2
#
command %r|^:at\s+(\w+)\s+(-\d+)?(.+)$|, :as => :at do |m|
index = (m[2] || -1).to_i
cmd = if id = recent_status_id(m[1], index)
":reply #{id}"
else
":update @#{m[1]}"
end
input("%s %s" % [cmd, m[3]])
end
# favorite
#
# e.g. :fav who
# :fav who -2
#
command %r|^:fav\s+(\w+)\s*(-\d+)?$|, :as => :fav do |m|
if id = recent_status_id(m[1],(m[2] || -1).to_i)
input(":favorite #{id}")
else
puts("Not found @#{m[1]}'s tweet")
end
end
# retweet
#
# e.g. :rt who
# :rt who -2
#
command %r|^:rt\s+(\w+)\s*(-\d+)?$|, :as => :rt do |m|
if id = recent_status_id(m[1],(m[2] || -1).to_i)
input(":retweet #{id}")
else
puts("Not found @#{m[1]}'s tweet")
end
end
# delete my recent tweet
#
# e.g. :del
# :del -2
#
command %r|^:del\s*(-\d+)?$|, :as => :del do |m|
name = twitter.info["screen_name"]
if id = recent_status_id(name, (m[1] || -1).to_i)
input(":delete #{id}")
else
puts("Not found @#{name}'s tweet")
end
end
# thread
#
# e.g. :th who
# :th who -2
#
command %r|^:th\s+(\w+)\s*(-\d+)?$|, :as => :th do |m|
if id = recent_status_id(m[1],(m[2] || -1).to_i)
input(":thread #{id}")
else
puts("Not found @#{m[1]}'s tweet")
end
end
# replace my recent tweet
#
# e.g. :s/foo/bar/
#
command %r|^:s\/(.+)\/(.+)\/$|, :as => :s do |m|
name = twitter.info["screen_name"]
if id = recent_status_id(name, -1)
src = cache.read("status:#{id}")["text"]
dest = src.gsub(/#{m[1]}/,m[2])
if confirm("%s => %s" % [src, dest])
twitter.status_destroy(id)
twitter.update(dest)
end
else
puts("Not found @#{name}'s tweet")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment