Skip to content

Instantly share code, notes, and snippets.

@d6rkaiz
Created October 19, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save d6rkaiz/1299776 to your computer and use it in GitHub Desktop.
Save d6rkaiz/1299776 to your computer and use it in GitHub Desktop.
require 'uri'
require 'rubygems'
require 'curb'
require 'json'
require 'rubytter'
module Lingrvim
class << self
AT = ['', '']
OA = ['', '']
BOTID='vimbots'
VERIFIER=""
SAYROOM='vim'
def firsttime
oauth = Rubytter::OAuth.new(*OA)
request_token = oauth.get_request_token
system('open', request_token.authorize_url) || puts("Access here: #{request_token.authorize_url}\nand...")
print "Enter PIN: "
pin = gets.strip
access_token = request_token.get_access_token(
oauth_token: request_token.token,
oauth_verifier: pin
)
p [access_token.token, access_token.secret]
puts "copy them into your code"
end
def fetch(since_id)
abort 'run firsttime and put Access Token' if AT[0].empty?
at = OAuth::AccessToken.new(
OAuth::Consumer.new(*OA, site: 'https://api.twitter.com'),
*AT)
client = OAuthRubytter.new(at)
if since_id
client.replies since_id: since_id
else
client.replies
end
end
# tinyicon('http://something/anything.jpg')
# #=> 'http://tinyurl.com/something#.jpg'
def tinyicon(image_url)
extension = image_url[/\.(\w*)$/, 1]
cc = Curl::Easy.perform("http://tinyurl.com/api-create.php?url=#{image_url}")
cc.body_str + "#.#{extension}"
end
def lingr(name, icon, text)
#text = URI.encode "#{icon} #{name}\n#{text}"
text = URI.encode("#{icon} #{name}\n#{text}", Regexp.new("[^#{URI::PATTERN::ALNUM}]") )
sayurl="http://lingr.com/api/room/say?room=#{SAYROOM}&bot=#{BOTID}&text=#{text}&bot_verifier=#{VERIFIER}"
c = Curl::Easy.perform(sayurl)
j = JSON.parse c.body_str
if j['status'] != 'ok'
p c.body_str
end
end
end
end
if ARGV.shift == 'firsttime'
Lingrvim.firsttime
else
lastid = Lingrvim.fetch(nil).first[:id_str]
loop do
tweets = Lingrvim.fetch(lastid) rescue []
tweets.reverse.each do |tweet|
name, icon, text = [tweet[:user][:screen_name], tweet[:user][:profile_image_url], tweet[:text]]
icon = Lingrvim.tinyicon(icon)
# puts "#{name}: #{text}"
Lingrvim.lingr name, icon, text.sub(/^@lingrvim /, '')
end
lastid = tweets.first[:id_str] unless tweets.empty?
sleep 10
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment