From 6e876e28dea6230b6270dd3aa955363479bb9644 Mon Sep 17 00:00:00 2001 From: Simon Szustkowski Date: Mon, 22 Apr 2013 13:37:01 +0200 Subject: [PATCH] Fixed. WTF --- bot.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++ config.yaml.example | 8 -------- 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100755 bot.rb delete mode 100644 config.yaml.example diff --git a/bot.rb b/bot.rb new file mode 100755 index 0000000..33b64a7 --- /dev/null +++ b/bot.rb @@ -0,0 +1,48 @@ +#! /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 + +# 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 + + +# 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." + status = "offline" + elsif ($current_status == "online") + tweet = "Der Raum ist jetzt offen." + status = "online" + else + tweet = "Raumstatus unbekannt." + status = "unknown" + end + client.update(tweet) + $config['worker']['last_status'] = status + File.open(CONFIG_FILE, 'w') { |f| YAML.dump($config, f) } +end diff --git a/config.yaml.example b/config.yaml.example deleted file mode 100644 index 1bd9354..0000000 --- a/config.yaml.example +++ /dev/null @@ -1,8 +0,0 @@ ---- -oauth: - consumer_key: - consumer_secret: - request_token: - request_secret: -worker: - last_status: online