Created
May 31, 2012 14:35
-
-
Save momo-lab/2843804 to your computer and use it in GitHub Desktop.
Pocket(旧 ReadItLater)にツイート内のURLを追加するearthquake.gem用プラグイン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# earthquake.gem plugin | |
# add url to Pocket(http://getpocket.com) | |
require 'uri' | |
require 'open-uri' | |
Earthquake.init do | |
cfg = config[:pocket] || {} | |
command :pocket do |m| | |
pocket_uri = URI.parse('https://readitlaterlist.com/v2/add') | |
item = twitter.status(m[1]) | |
entities = (item["retweeted_status"] && item["truncated"]) ? | |
item["retweeted_status"]["entities"] : item["entities"] | |
if entities | |
entities.values_at("urls", "media").flatten.compact.map do |entity| | |
url, expanded_url = entity.values_at("url", "expanded_url") | |
url = expanded_url if expanded_url | |
if confirm "add to pocket '#{url}'" | |
url | |
else | |
nil | |
end | |
end.compact.each do |url| | |
async do | |
pocket_uri.query = cfg.merge({ | |
:url => url, | |
:title => item['text'], | |
}).map{|k, v| "#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}" }.join('&') | |
begin | |
res = pocket_uri.open | |
insert "added '#{url}': #{res.read}".c(:info) | |
rescue Errno::ETIMEDOUT => e | |
insert "not added '#{url}': connection timed out".c(:notice) | |
rescue OpenURI::HTTPError => e | |
insert "not added '#{url}': #{e.io.meta["x-error"]}".c(:notice) | |
end | |
end | |
end | |
end | |
end | |
help :pocket, 'add url to Pocket(http://getpocket.com)', <<-HELP | |
[$aa] http://www.google.co.jp/ | |
⚡ :pocket $aa | |
add to pocket 'http://www.google.co.jp/' [Yn] y | |
OPTIONS | |
Earthquake.config[:pocket] = { | |
:apikey => 'your apikey', # get apikey from http://getpocket.com/api/signup/ | |
:username => 'your username', | |
:password => 'your password', | |
} | |
HELP | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment