Skip to content

Instantly share code, notes, and snippets.

@takano32
Created May 20, 2009 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takano32/115104 to your computer and use it in GitHub Desktop.
Save takano32/115104 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'rubygems'
require 'mechanize'
require 'pit'
config = Pit.get("twitter",
:require => {
"username_or_email" => "your twitter username or email",
"password" => "your twitter password"
})
class TwitterClient
@@uri = URI.parse('http://twitter.com/')
def initialize(username_or_email, password)
@agent = WWW::Mechanize.new
@username_or_email = username_or_email
@password = password
@agent.basic_auth(@username_or_email, @password)
end
def login
response = @agent.get(@@uri)
form = response.forms.last
form['session[username_or_email]'] = @username_or_email
form['session[password]'] = @password
response = @agent.submit(form)
end
def replies
response = @agent.get(@@uri + 'replies')
response.body
end
def update(status)
query = {'status' => status }
response = @agent.post(@@uri + 'statuses/update.json', query)
end
end
require 'scrapi'
scraper = Scraper.define do
process "ol.statuses > li.hentry > span.status-body " +
"> span.entry-content", "statuses[]" => :text
process "ol.statuses > li.hentry > span.status-body " +
"> span.entry-meta > a.entry-date", "urls[]" => "@href"
result :statuses, :urls
end
require 'serialport'
port = "/dev/tty.usbserial-A9007L8W"
sp = SerialPort.new(port, 9600, 8, 1, SerialPort::NONE)
tc = TwitterClient.new(config['username_or_email'], config['password'])
tc.login
require 'thread'
mutex = Mutex.new
thread = Thread.start do
switch = prev_switch = false
status = prev_status = nil
statuses = [
'カチ',
'コチ',
'チク',
'タク',
'プチ',
]
loop do
c = sp.getc
prev_switch = switch
case c
when 0 then
switch = false
when 1 then
switch = true
end
if prev_switch == false and switch == true then
prev_status = status
loop do
status = statuses.choice
break unless prev_status == status
end
puts status
mutex.synchronize do
tc.update(status + ' #hoge')
end
end
end
end
loop do
src = nil
links = nil
begin
mutex.synchronize do
src = tc.replies
end
links = (scraper.scrape(src, :parser_options => {:char_encoding => 'utf8'}))
links = links.statuses.zip(links.urls)
rescue Exception
end
next unless src or links
status = links.first.first
words = status.dup.split(' ')
bits = 0
puts status
bits |= 0b001 if words.include?('red')
bits |= 0b010 if words.include?('green')
bits |= 0b100 if words.include?('blue')
case true
when words.include?('on') then
sp.putc bits
when words.include?('off') then
bits |= 0b10000000
sp.putc bits
end
sleep 10
end
thread.join
sp.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment