Skip to content

Instantly share code, notes, and snippets.

@tyru
Forked from ujihisa/thincr.rb
Created February 10, 2011 09:14
Show Gist options
  • Select an option

  • Save tyru/820183 to your computer and use it in GitHub Desktop.

Select an option

Save tyru/820183 to your computer and use it in GitHub Desktop.
require 'uri'
require 'mechanize'
require 'rubytter'
module Thincr
class << self
AT = ['', '']
LINGR = ['', '']
OA = ['tNJwVPsE56fw8Nh4mmc9g', 'Zr95pR8m94l8sBoWAJtdoN1Z31wQrXfaabqzOhO5Wyk']
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
def lingr(name, text)
return if text == ''
m = Mechanize.new
page = m.get 'http://lingr.com/signin'
form = page.forms[0]
form['signin[email]'] = LINGR[0]
form['signin[password]'] = LINGR[1]
page = form.submit
sessionid = page.body[/sessionId: '(.*?)'/, 1]
text = URI.encode "> #{name}:\n> #{text}"
m.get "http://lingr.com/api/room/say?session=#{sessionid}&room=vim&nickname=ujihisa&text=#{text}&local_id=pending-A97B4P-2&callback=Lingr.callback9"
end
end
end
if ARGV.shift == 'firsttime'
Thincr.firsttime
else
lastid = Thincr.fetch(nil).first[:id_str]
loop do
tweets = Thincr.fetch(lastid)
tweets.reverse.each do |tweet|
name, text = [tweet[:user][:screen_name], tweet[:text]]
puts "#{name}: #{text}"
Thincr.lingr name, text.sub(/^@lingrvim\s+/, '')
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