Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@momo-lab
Created May 31, 2012 14:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save momo-lab/2843804 to your computer and use it in GitHub Desktop.
Save momo-lab/2843804 to your computer and use it in GitHub Desktop.
Pocket(旧 ReadItLater)にツイート内のURLを追加するearthquake.gem用プラグイン
# -*- 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