Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using tweet entities all the time #108

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/earthquake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
help
commands
id_var
entities
).each { |name| require_dependency File.expand_path("../earthquake/#{name}", __FILE__) }
89 changes: 89 additions & 0 deletions lib/earthquake/entities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module Earthquake
module Entities
class << self
def apply(item)
if item["retweeted_status"] && item["truncated"]
text = apply(item["retweeted_status"])
screen_name = item["retweeted_status"]["user"]["screen_name"]
return "RT #{"@#{screen_name}".c(Earthquake.color_of(screen_name))}: #{text}"
end
text, item_entities = item.values_at("text", "entities")
if item_entities
text = text.dup
parse(item_entities).reverse_each do |entity|
entity.apply(text)
end
end
text.u
end

private

def parse(item_entities)
item_entities.flat_map{|type, entities|
entities.map{|entity|
klass = type.classify
const_get(klass).new(entity) if const_defined?(klass)
}.compact
}.sort_by!(&:position)
end
end

class Base
def initialize(entity)
@entity = entity
@first, @last = entity["indices"]
end

def position
@first
end

def apply(text)
text[@first...@last] = colored_text
text
end

def string
raise NotImplementedError, "need to define `string'"
end

private

def colored_text
s = string
s.c(Earthquake.color_of(s))
end
end

class UrlBase < Base
def string
Earthquake.config[:expand_url] && @entity["expanded_url"] || @entity["url"]
end

private

def colored_text
string.c(:url)
end
end

class Url < UrlBase
end

class Medium < UrlBase
end

class Hashtag < Base
def string
"#" + @entity["text"]
end
end

class UserMention < Base
def string
"@" + @entity["screen_name"]
end
end
end
end
16 changes: 1 addition & 15 deletions lib/earthquake/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,9 @@ def color_of(screen_name)

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 = Entities.apply(item)
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(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|
Expand Down