57 lines
1.7 KiB
Ruby
Executable File
57 lines
1.7 KiB
Ruby
Executable File
#! /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)))
|
|
|
|
# Configure and create the twitter client
|
|
Twitter.configure do |config|
|
|
config.consumer_key = $config['oauth']['consumer_key']
|
|
config.consumer_secret = $config['oauth']['consumer_secret']
|
|
config.oauth_token = $config['oauth']['request_token']
|
|
config.oauth_token_secret = $config['oauth']['request_secret']
|
|
end
|
|
|
|
client = Twitter::Client.new
|
|
|
|
# Read last known status from cache
|
|
last_status = $config['worker']['last_status'].to_s
|
|
#puts "last status #{last_status}"
|
|
# Get current status from web
|
|
url = URI.parse('http://www.chaostreff-dortmund.de/')
|
|
res = Net::HTTP.start(url.host, url.port) {|http|
|
|
http.get('/raumstatus.php?txt')
|
|
}
|
|
$current_status = res.body.to_s.strip
|
|
#puts "current status #{$current_status}"
|
|
|
|
# If status differs from last time checked, put the announcement
|
|
if (last_status != $current_status)
|
|
if ($current_status == "offline")
|
|
tweet = "Der Raum ist jetzt GESCHLOSSEN. #ctdo"
|
|
status = "offline"
|
|
#puts "offlinestatus getwittert"
|
|
elsif ($current_status == "online")
|
|
tweet = "Der Raum ist jetzt OFFEN. #ctdo."
|
|
status = "online"
|
|
#puts "onlinestatus getwittert"
|
|
else
|
|
tweet = "Raumstatus UNBEKANNT."
|
|
status = "unknown"
|
|
#puts "unbekannt getwittert"
|
|
end
|
|
$config['worker']['last_status'] = status
|
|
File.open(File.join(File.dirname(__FILE__), CONFIG_FILE), 'w') { |f| YAML.dump($config, f) }
|
|
begin
|
|
client.update(tweet, {:lat => "51.527611", :lon => "7.464944", :display_coordinates => "true"})
|
|
rescue Exception => e
|
|
puts e
|
|
# Nothing to do here
|
|
end
|
|
end
|