Last active
August 29, 2015 14:01
-
-
Save kiyoto/d950de1dbee35de81573 to your computer and use it in GitHub Desktop.
Steam friend online status plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fluent | |
class SteamFriendsOnlineInput < Input | |
Plugin.register_input('steam_friends_online', self) | |
config_param :steam_id, :integer | |
config_param :tag, :string, default:"steam_stats" | |
config_param :friend_nicknames, :string | |
config_param :stats_interval, :time, default:300 | |
def initialize | |
super | |
require 'steam-condenser' | |
end | |
def configure(conf) | |
super | |
@friend_nicknames = @friend_nicknames.gsub(" ", "").split(",").map {|n| n.downcase} | |
end | |
def start | |
@loop = Coolio::Loop.new | |
tw = TimerWatcher.new(@stats_interval, true, @log, &method(:get_steam_stats)) | |
tw.attach(@loop) | |
@thread = Thread.new(&method(:run)) | |
end | |
def get_steam_stats | |
steam_id = SteamId.new(@steam_id) | |
time = Engine.now | |
online_statuses = {} | |
@friend_nicknames.each { |nickname| online_statuses[nickname] = false } | |
steam_id.friends.each { |friend_id| | |
friend = SteamId.new(friend_id.id) | |
nickname = friend.nickname.downcase | |
online_statuses[nickname] = friend.online? | |
} | |
log.error online_statuses.to_s | |
online_statuses.each do |k,v| | |
return if not v | |
end | |
Engine.emit(@tag, time, { "friends_online" => true }) | |
end | |
def run | |
@loop.run | |
rescue | |
log.error "unexpected error", :error=>$!.to_s | |
log.error_backtrace | |
end | |
def shutdown | |
@loop.stop | |
@thread.join | |
end | |
class TimerWatcher < Coolio::TimerWatcher | |
def initialize(interval, repeat, log, &callback) | |
@callback = callback | |
@log = log | |
super(interval, repeat) | |
end | |
def on_timer | |
@callback.call | |
rescue | |
@log.error $!.to_s | |
@log.error_backtrace | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment