Skip to content

Instantly share code, notes, and snippets.

@kenrett

kenrett/README.md Secret

Last active Jan 31, 2019
Embed
What would you like to do?

MLB Standings Widget

Description

A Dashing widget to display Major League Baseball Standings using Baseball-Reference.com.

Preview

Screen Shot

Useage

To use this widget, copy mlb.coffee, mlb.html, and mlb.scss into the /widgets/mlb directory of your Dashing app. This directory does not exist in new Dashing apps, so you may have to create it. Copy the mlb.rb file into your /jobs folder.

To include the widget in a dashboard, add the following to your dashboard layout file:

#####dashboards/sample.erb

...
  <li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
    <div data-id="mlb" data-view="Mlb" data-title="Mlb Standings"></div>
  </li>
...

Requirements

Code

#####widgets/mlb/mlb.coffee

class Dashing.Mlb extends Dashing.Widget

  ready: ->

  onData: (data) ->

#####widgets/mlb/mlb.html

<img class="mlb-logo" src="http://mlb.mlb.com/images/icons/mlb_logo.gif">

<h1 class="mlb-title" data-bind="title"></h1>

<div id="all_teams">
  <table>
    <th class='mlb-th'>Team</th>
    <tr class='mlb-tr' data-class="mlb" data-foreach-standings="teams">
      <td class='mlb-td' data-bind="standings | raw"></td>
    </tr>
  </table>
  <table>
    <th class='mlb-th'>Wins</th>
    <tr data-class="mlb" data-foreach-standings="wins">
      <td class='mlb-td' data-bind="standings | raw"></td>
    </tr>
  </table>
  <table>
    <th class='mlb-th'>Losses</th>
    <tr data-class="mlb" data-foreach-standings="losses">
      <td class='mlb-td' data-bind="standings | raw"></td>
    </tr>
  </table>
  <table>
    <th class='mlb-th'>GB</th>
    <tr data-class="mlb" data-foreach-standings="gb">
      <td class='mlb-td' data-bind="standings | raw"></td>
    </tr>
  </table>
</div>

<p class="mlb-info">Powered by Baseball-Reference.com</p>
<p class="mlb-updated-at" data-bind="updatedAtMessage"></p>

#####widgets/mlb/mlb.scss

table { float: left;
        max-width: 20%;
        width: 200px; }

.mlb-th, .mlb-tr, .mlb-td, 
.mlb-title, .mlb-logo, .mlb-info, 
.mlb-updated-at  { padding-left: 90px;}

#####jobs/mlb.rb

Uncomment the division you want to see. You can currently only view 1 division at a time.

require 'rubygems'
require 'nokogiri'
require 'open-uri'


SCHEDULER.every '5m', :first_in => 0 do
  doc = Nokogiri::HTML(open('http://www.baseball-reference.com'))

  al_standings = []

  al = doc.css('#div_AL_standings').text
  nl = doc.css('#div_NL_standings').text

  by_division = al.split("SRS")
  by_division << nl.split("SRS")

  # Uncomment the division you want to see.
  # At this time you can only view 1 division at a time.

# # AL EAST  
#   al_east = by_division[1].split(" ").each_slice(6).to_a
#   al_east.each { |r| r.pop }
#   al_east.pop
#   div_strings = al_east.each { |t| t.to_s }
# # AL CENTRAL
#   al_cent = by_division[2].split(" ").each_slice(6).to_a
#   al_cent.each { |r| r.pop }
#   al_cent.pop
#   div_strings = al_cent.each { |t| t.to_s }
# AL WEST
  al_west = by_division[3].split(" ").each_slice(6).to_a
  al_west.each { |r| r.pop }
  # west does not need an extra pop...
  div_strings = al_west.each { |t| t.to_s }

# # NL EAST
#   nl_east = by_division[4].split(" ").each_slice(6).to_a
#   nl_east.each { |r| r.pop }
#   nl_east.pop
#   div_strings = nl_east.each { |t| t.to_s }
# # NL CENTRAL
#   nl_cent = by_division[5].split(" ").each_slice(6).to_a
#   nl_cent.each { |r| r.pop }
#   nl_cent.pop
#   div_strings = nl_cent.each { |t| t.to_s }
# # NL WEST
#   nl_west = by_division[6].split(" ").each_slice(6).to_a
#   nl_west.each { |r| r.pop }
#   div_strings = nl_west.each { |t| t.to_s }

  divisions = []
  divisions = div_strings.each { |f| f.delete(f[1]) }
  team = divisions.transpose

  standings = { 
    :teams => team[0],
    :wins => team[1],
    :losses => team[2],
    :gb => team[3]
  }

  send_event('mlb', standings)
end
@Drabert

This comment has been minimized.

Copy link

@Drabert Drabert commented Jul 25, 2015

I am having a problem with using nokogiri. It is installed but when i try to start the dash board i get cannot load such file -- nokogiri. Sorry for my inexperience, but could someone please help? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.