Created
December 5, 2011 12:46
-
-
Save no6v/1433488 to your computer and use it in GitHub Desktop.
Show top 10 trends for the location
This file contains 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 -*- | |
# monkey patch to support Twitter API v1.1 | |
Earthquake.once do | |
Module.new do |api_v1_1| | |
def trends(woeid) | |
get("/trends/place.json?id=#{woeid}") | |
end | |
TwitterOAuth::Client.__send__(:prepend, api_v1_1) | |
end | |
end | |
Earthquake.init do | |
command :trends do | |
location = config[:trends_location] || "Worldwide" | |
input(":trends #{location}") | |
end | |
command :trends do |m| | |
location = m[1] | |
if cached = cache.read("trend:#{location}") and cached[:expires_in].future? | |
ap(candidates = cached[:names]) | |
else | |
unless trends_available = cache.read("trends:available") | |
puts "caching available trends information...".c(:info) | |
trends_available = twitter.trends_available.each_with_object({}) do |info, hash| | |
hash[info["name"]] = info["woeid"] | |
end | |
cache.write("trends:available", trends_available) | |
end | |
if woeid = trends_available[location] | |
names = twitter.trends(woeid)[0]["trends"].map{|trend| trend["name"]} | |
cache.write("trend:#{location}", names: names, expires_in: 5.minutes.from_now) | |
ap(candidates = names) | |
else | |
names = trends_available.keys.grep(/#{Regexp.escape(location)}/i) | |
print "Unknown location. ".c(:info) | |
if names.empty? | |
puts "Nothing matched.".c(:info) | |
else | |
puts "Did you mean:".c(:info) | |
ap names | |
end | |
end | |
end | |
if candidates | |
index = ((Integer(ask("search trend? (just ENTER to leave) [0-9]: ")) % 10) rescue next) | |
command = ":search #{candidates[index]}" | |
Readline::HISTORY << command | |
input(command) | |
end | |
end | |
help :trends, "show top 10 trends for the location", <<-'HELP' | |
# switch to search if unknown location (case-insensitive) | |
⚡ :trends ja | |
Unknown location. Did you mean: | |
[ | |
[0] "Japan", | |
[1] "Jakarta", | |
[2] "Jackson", | |
[3] "Rio de Janeiro" | |
] | |
# show (and search) trends if known place (case-sensitive) | |
⚡ :trends Japan | |
[ | |
[0] "#earthquakegem", | |
[1] "Ruby", | |
(snip) | |
[9] "awesome" | |
] | |
search trend? (just ENTER to leave) [0-9]: 0 | |
(invoke ':search #earthquakegem') | |
# in short, set config[:trends_location] to your favorite | |
⚡ :trends | |
(show top 10 trends of specified location or "Worldwide" as a default) | |
HELP | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment