Skip to content

Instantly share code, notes, and snippets.

@siyo
Last active September 29, 2015 13:18
Show Gist options
  • Save siyo/1607449 to your computer and use it in GitHub Desktop.
Save siyo/1607449 to your computer and use it in GitHub Desktop.
自分のTLで最近の診断されたやつを俺もする / earthquake.gem plugin
# -*- coding: utf-8 -*-
# 自分のTLで最近の診断されたやつを俺もする
#
# usage:
# :shindan [NUMBER] [hogehoge]
# or
# :shindan [option]
#
# option:
# -l: display recent shindan list
#
require 'httpclient'
require 'nokogiri'
module Earthquake::Input
def select_shindan_tweets
regexp = %r|(https*://shindanmaker.com/\d+)|
ids = ("aa".."zz").to_a.inject([]){|s,v|
break s unless id = var2id("$#{v}");
s << id
}
ids.sort.inject([]){|s,id|
next s unless st = cache.read("status:#{id}")
if st["entities"] && !st["entities"]["urls"].empty? &&
st["entities"]["urls"].detect{|url| regexp =~ url["expanded_url"]}
s << {:status=>st, :url=>$1}
end
s
}
end
end
Earthquake.init do
command %r|^:shindan\s?(-?[\dl])?\s?(.+)?$|, :as => :shindan do |m|
opt = m[1] || '-1'
list = select_shindan_tweets
case opt
when '-l'
mark = config[:colors].sample + 10
list.each_with_index{|shindan,i|
st = shindan[:status]
user = st['user']['screen_name']
puts "%s%s %s: %s" % [ " ".c(mark),
i.to_s.c(:info),
user.c(color_of(user)),
st['text'] ]
}
when /(-?\d)/
if shindan = list[opt.to_i]
url = shindan[:url]
u = m[2] || twitter.info["screen_name"]
res = HTTPClient.new.post(url, {:u => u, :from => ""} )
html = Nokogiri::HTML(res.body)
text = html.css("#forcopy").text.strip
if confirm(text)
twitter.update text
end
else
puts "Not found shindan tweet".c(:info)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment