Created
March 17, 2014 17:00
-
-
Save kianryan/9603501 to your computer and use it in GitHub Desktop.
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
require 'mechanize' | |
module CCCB::Core::TwitterMessage | |
extend Module::Requirements | |
TWITTER_REGEX = /twitter.com\/(?<name>[A-Za-z0-9_]*)\//i | |
needs :bot, :links | |
def module_load | |
default_setting true, "options", "log_twitter_message" | |
twitter_message.history ||= [] | |
add_hook :twitter_message, :uri_found do |message, uri_data| | |
next unless message.to_channel? | |
next unless match = TWITTER_REGEX.match(uri_data[:uri]) | |
agent = Mechanize.new | |
mobile_url = agent.get(uri_data[:uri]).meta_refresh[0].href | |
mobile_page = agent.get(mobile_url) | |
name = match[:name] | |
tweet = mobile_page.search('.main-tweet .tweet-content .tweet-text div').text() | |
date = mobile_page.search('.main-tweet .tweet-content .metadata a').text() | |
message_reply = "twitter \x0311|\x0F #{tweet} \x0311|\x0F tweeted by @#{name} at #{date}" | |
message.reply message_reply | |
next unless message.user.get_setting("options", "log_twitter_message") | |
twitter_message.history ||= [] | |
twitter_message.history << [message.nick, source, uri_data[:uri], name, date, tweet] | |
twitter_message.history.shift if twitter_message.history.count > 1024 | |
end | |
add_request :twitter_message, /^link search (?<pattern>.*?)\s*$/ do |match, message| | |
message.reply "Searching..." | |
message.reply twitter_message.history | |
pattern = Regexp.escape(match[:pattern]) | |
pattern.gsub! /%/, '.*' | |
regex = Regexp.new(pattern) | |
seen = {} | |
twitter_message.history.select { |(n,s,u,name, date, tweet)| | |
regex.match(name) || regex.match(date) || regex.match(tweet) | |
}.each do |(nick, source, uri, name, date, tweet)| | |
next if seen.include? uri | |
message.reply "from #{nick} \x0311|x0F #{uri} \x0311|\x0F @#{name} \x0311|\x0F @#{date} \x0311|\x0F @#{tweet}" | |
seen[uri] = true | |
end | |
nil # requests automatically respond with whatever the block returns | |
# ending with nil prevents this | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment