twitter-status-bot/bot.rb

57 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2013-04-22 11:37:01 +00:00
#! /usr/bin/env ruby
require 'twitter'
require 'yaml'
require 'net/http'
require 'uri'
#Specify and load config
CONFIG_FILE = 'config.yaml'
$config = YAML::load(File.open(File.join(File.dirname(__FILE__), CONFIG_FILE)))
2014-09-03 08:41:50 +00:00
client = Twitter::REST::Client.new do |config|
config.consumer_key = $config['oauth']['consumer_key']
config.consumer_secret = $config['oauth']['consumer_secret']
config.access_token = $config['oauth']['request_token']
config.access_token_secret = $config['oauth']['request_secret']
end
2013-04-22 11:37:01 +00:00
# Read last known status from cache
last_status = $config['worker']['last_status'].to_s
2014-09-03 07:36:33 +00:00
#puts "last status #{last_status}"
2013-04-22 11:37:01 +00:00
# Get current status from web
uri = URI.parse('http://status.ctdo.de/api/spaceapi/v13')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
result = JSON.parse(response.body)
2014-09-03 08:41:50 +00:00
$current_status = result["state"]["open"].to_s
2014-09-03 08:50:30 +00:00
#p $current_status
#p last_status
2013-04-22 11:37:01 +00:00
# If status differs from last time checked, put the announcement
if (last_status != $current_status)
2014-09-03 08:41:50 +00:00
if ($current_status == "false")
2013-04-22 11:39:22 +00:00
tweet = "Der Raum ist jetzt GESCHLOSSEN. #ctdo"
2014-09-03 08:41:50 +00:00
status = "false"
2014-09-03 07:36:33 +00:00
#puts "offlinestatus getwittert"
2014-09-03 08:41:50 +00:00
elsif ($current_status == "true")
2013-04-22 11:39:22 +00:00
tweet = "Der Raum ist jetzt OFFEN. #ctdo."
2014-09-03 08:41:50 +00:00
status = "true"
2014-09-03 07:36:33 +00:00
#puts "onlinestatus getwittert"
2013-04-22 11:37:01 +00:00
else
2013-04-22 11:39:22 +00:00
tweet = "Raumstatus UNBEKANNT."
2013-04-22 11:37:01 +00:00
status = "unknown"
2014-09-03 07:36:33 +00:00
#puts "unbekannt getwittert"
2013-04-22 11:37:01 +00:00
end
$config['worker']['last_status'] = status
2014-09-03 08:41:50 +00:00
File.open(File.join(File.dirname(__FILE__), CONFIG_FILE), 'w') { |f| YAML.dump($config, f) }
2013-04-22 11:39:22 +00:00
begin
2014-09-03 08:50:30 +00:00
#puts tweet
2014-09-03 08:41:50 +00:00
client.update(tweet, {:lat => "51.527611", :lon => "7.464944", :display_coordinates => "true"})
2014-09-03 07:36:33 +00:00
rescue Exception => e
puts e
2013-04-22 11:39:22 +00:00
# Nothing to do here
end
2013-04-22 11:37:01 +00:00
end