Skip to content

Instantly share code, notes, and snippets.

@Fustrate
Last active December 20, 2015 15:09
Show Gist options
  • Save Fustrate/6152553 to your computer and use it in GitHub Desktop.
Save Fustrate/6152553 to your computer and use it in GitHub Desktop.
  1. Install ruby
  2. Install gems: gem install nokogiri snoo
  3. Set a cron job to run update.rb every hour/day/etc.
  4. Add [](/updates) at the top and bottom of the current table/header. Anything inside this pair of links will be replaced.
  5. Update the usernames and password at the top of the file - the account must have config mod permissions
#!/bin/ruby
require 'cgi'
require 'nokogiri'
require 'open-uri'
require 'snoo'
# Update these to actual values
reddit = Snoo::Client.new(useragent: "/r/soundersfc sidebar script by /u/YourUsername")
reddit.log_in "BotUsername", "BotPassword"
data = Nokogiri::HTML open 'http://espnfc.com/team/_/id/9726/seattle-sounders-fc?cc=5901'
table = data.css('#content .span-2.last table').first
teams = []
table.css('tr.oddrow, tr.evenrow').each do |team|
name, games, wins, draws, losses, gd, points = team.children.map(&:inner_text)
teams << {
name: name,
games: games.to_i,
wins: wins.to_i,
draws: draws.to_i,
losses: losses.to_i,
gd: gd.to_i,
points: points.to_i
}
end
teams = teams.sort_by do |team|
team[:points]
end.reverse
# For anything more than this, I'd use an ERB template file instead.
standings = '## 2013 Western Conference Standings
Club|Pts|GP|W|L|T|GD
-|-|-|-|-|-|-'
teams.each do |team|
standings << "\n#{team[:name]}|#{team[:points]}|#{team[:games]}|#{team[:wins]}|#{team[:losses]}|#{team[:draws]}|#{team[:gd]}"
end
# -----------------------------------------------------------------------------
settings = reddit.get_subreddit_settings 'soundersfc'
new_sidebar = CGI.unescapeHTML(settings["data"]["description"]).sub(/\[\]\(\/updates\)(.*)\[\]\(\/updates\)/m, "[](/updates)\n#{ standings }\n[](/updates)")
# All of this needs to be sent, or reddit overwrites it with default values
reddit.subreddit_settings("",
:sr => settings["data"]["subreddit_id"],
:name => settings["data"]["name"],
:title => settings["data"]["title"],
:wikimode => settings["data"]["wikimode"],
:wiki_edit_karma => settings["data"]["wiki_edit_karma"],
:exclude_banned_modqueue => settings["data"]["exclude_banned_modqueue"],
:show_media => settings["data"]["show_media"],
:public_description => settings["data"]["public_description"],
:"header-title" => settings["data"]["header_hover_text"],
:submit_link_label => settings["data"]["submit_link_label"],
:submit_text_label => settings["data"]["submit_text_label"],
:description => new_sidebar
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment