Skip to content

Instantly share code, notes, and snippets.

@siyo
Created April 12, 2013 08:05
Show Gist options
  • Save siyo/5370353 to your computer and use it in GitHub Desktop.
Save siyo/5370353 to your computer and use it in GitHub Desktop.
Tweetしたbitly URLのクリック数を表示するし、他人のやつも表示するプラギン / earthquake.gem plugin
# -*- coding: utf-8 -*-
# Display bit.ly clicks / eaarthquake.gem plugin
#
# (Set your Login name & API Key)
#
# Required gems:
# bitly : https://github.com/philnash/bitly
#
# Usage
# e.g.
# :clicks #=> your tweet bit.ly/foobar / clicks user 10 global 100
# :clicks who #=> whose tweet j.mp/fizzbuzz / clicks user 20 global 200
#
require 'bitly'
Earthquake.once do
Bitly.use_api_version_3
end
Earthquake.init do
_ = config[:bitly] ||= {}
_[:login] ||= 'Login_name'
_[:api_key] ||= 'API_Key'
_[:count] ||= 10
command %r|^:clicks\s*(.+)?|, :as => :clicks do |m|
screen_name = m[1] || twitter.info["screen_name"]
param = {
screen_name: screen_name,
include_entities: true,
count: 200
}
bitly_tweets = twitter.user_timeline(param).select{|st|
urls = st["entities"]["urls"]
urls.size > 0 && urls.detect{|url|
url["expanded_url"] =~ /(bit\.ly|j\.mp)/
}
}
if bitly_tweets.size > 0
bitly = Bitly.new( _[:login], _[:api_key] )
mark = ' '.c(config[:colors].sample + 10)
bitly_tweets[0.._[:count]].reverse_each{|st|
clicks = bitly.clicks(st["entities"]["urls"][0]["expanded_url"])
unless clicks.respond_to?(:user_clicks) &&
clicks.respond_to?(:global_clicks)
next
end
created_at = Time.parse(st["created_at"]).strftime(config[:time_format])
puts [ mark,
"[#{id2var(st['id'])}] ".c(:info),
"#{screen_name.c(color_of(screen_name))}: ",
st["text"],
" #{created_at}".c(:info),
" / clicks: ".c(:info),
"user ".c(:info),
clicks.user_clicks.to_s.c(:notice),
" global ".c(:info),
clicks.global_clicks.to_s.c(:notice),
].join
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment