Created
February 21, 2012 06:31
-
-
Save hnagato/1874290 to your computer and use it in GitHub Desktop.
location 表示 + screen_name をもうちょいカラフルに
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 -*- | |
module Earthquake | |
module Output | |
require 'uri' | |
def puts_items(items) | |
mark_color = [ config[:colors].sample, 1 ].flatten # random & bold | |
[items].flatten.reverse_each do |item| | |
next if output_filters.any? { |f| f.call(item) == false } | |
if item["text"] && !item["_stream"] | |
item['_mark'] = ' '.c(mark_color) + item['_mark'].to_s | |
end | |
outputs.each do |o| | |
begin | |
o[:block].call(item) | |
rescue => e | |
error e | |
end | |
end | |
end | |
end | |
def color_of_hashtag(hashtag) | |
if hashtag.delete("^0-9a-zA-Z").empty? | |
config[:colors][URI.escape(hashtag).delete("^0-9a-zA-Z").to_i(16) % config[:colors].size] | |
else | |
color_of(hashtag) | |
end | |
end | |
def indent(length, maxlength = 12) | |
' ' * (length > maxlength ? 1 : maxlength - length) | |
end | |
end | |
end | |
Earthquake.init do | |
output :tweet do |item| | |
next unless item["text"] | |
info = [] | |
if item["in_reply_to_status_id"] | |
info << "(reply to #{id2var(item["in_reply_to_status_id"])})" | |
elsif item["retweeted_status"] | |
info << "(retweet of #{id2var(item["retweeted_status"]["id"])})" | |
end | |
if !config[:hide_time] && item["created_at"] | |
info << Time.parse(item["created_at"]).strftime(config[:time_format]) | |
end | |
if !config[:hide_app_name] && item["source"] | |
info << (item["source"].u =~ />(.*)</ ? $1 : 'web') | |
end | |
id = id2var(item["id"]) | |
text = (item["retweeted_status"] && item["truncated"] ? "RT @#{item["retweeted_status"]["user"]["screen_name"]}: #{item["retweeted_status"]["text"]}" : item["text"]).u | |
text.gsub!(/\s+/, ' ') unless config[:raw_text] | |
text.prepend("\n") if config[:raw_text] | |
text = text.coloring(/@[0-9A-Za-z_]+/) { |i| color_of(i) } | |
text = text.coloring(/(^#[^\s ]+)|([\s ]+#[^\s ]+)/) { |i| color_of_hashtag(i) } | |
if config[:expand_url] | |
entities = (item["retweeted_status"] && item["truncated"]) ? item["retweeted_status"]["entities"] : item["entities"] | |
if entities | |
entities.values_at("urls", "media").flatten.compact.each do |entity| | |
url, expanded_url = entity.values_at("url", "expanded_url") | |
if url && expanded_url | |
text = text.sub(url, expanded_url) | |
end | |
end | |
end | |
end | |
text = text.coloring(URI.regexp(["http", "https"]), :url) | |
if item["_highlights"] | |
item["_highlights"].each do |h| | |
color = config[:color][:highlight].nil? ? [ color_of(h), 1 ].flatten : :highlight | |
text = text.coloring(/#{h}/i, color) | |
end | |
end | |
mark = item["_mark"] || "" | |
screen_name = item["user"]["screen_name"] | |
location = item["user"]["location"] || "unkonwn" | |
status = [ | |
"#{mark}" + "#{id}".c(:info), | |
"#{screen_name.c(color_of(screen_name))}#{indent(screen_name.size)}", | |
"#{text}", | |
(item["user"]["protected"] ? "[P]".c(:notice) : nil), | |
info.join(' - ').c(:info), | |
location.c(:location) | |
].compact.join(" ") | |
puts status | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
~/.earthquake/config