Skip to content

Instantly share code, notes, and snippets.

@iainjmitchell
Last active May 29, 2019 14:20
Show Gist options
  • Save iainjmitchell/5271830 to your computer and use it in GitHub Desktop.
Save iainjmitchell/5271830 to your computer and use it in GitHub Desktop.
BBC News Widget for Dashing

##Description Simple Dashing widget (and associated job) to display BBC News Stories.

##Dependencies nokogiri

Add it to dashing's gemfile:

gem 'nokogiri'

and run bundle install.

##Usage To use this widget, copy news.html, news.coffee, and news.scss into the /widgets/news directory. Put the news.rb file in your /jobs folder.

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

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="news" data-view="News"></div>
</li>
class Dashing.News extends Dashing.Widget
ready: ->
@currentIndex = 0
@headlineElem = $(@node).find('.headline-container')
@nextComment()
@startCarousel()
onData: (data) ->
@currentIndex = 0
startCarousel: ->
setInterval(@nextComment, 8000)
nextComment: =>
headlines = @get('headlines')
if headlines
@headlineElem.fadeOut =>
@currentIndex = (@currentIndex + 1) % headlines.length
@set 'current_headline', headlines[@currentIndex]
@headlineElem.fadeIn()
<div class="heading">
<image src="http://static.bbci.co.uk/frameworks/barlesque/2.35.2/orb/4/img/bbc-blocks-dark.png"></image>
<span> News</span>
</div>
<div class="headline-container">
<h1 data-bind='current_headline.title' class="title"></h1>
<div class="headline">
<div>
<p class="description" data-bind='current_headline.description'></p>
</div>
</div>
</div>
class BbcNews
def initialize()
@http = Net::HTTP.new('feeds.bbci.co.uk')
end
def latest_headlines()
response = @http.request(Net::HTTP::Get.new("/news/rss.xml"))
doc = Nokogiri::XML(response.body)
news_headlines = [];
doc.xpath('//channel/item').each do |news_item|
news_headline = NewsHeadlineBuilder.BuildFrom(news_item)
news_headlines.push(news_headline)
end
news_headlines
end
end
class NewsHeadlineBuilder
def self.BuildFrom(news_item)
{
title: news_item.xpath('title').text,
description: news_item.xpath('description').text,
}
end
end
@BBC_News = BbcNews.new()
SCHEDULER.every '15m', :first_in => 0 do |job|
headlines = @BBC_News.latest_headlines
send_event('news', { :headlines => headlines})
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #47bbb3;
$value-color: #fff;
$title-color: rgba(255, 255, 255, 0.7);;
$moreinfo-color: rgba(255, 255, 255, 0.7);;
// ----------------------------------------------------------------------------
// Widget-weather styles
// ----------------------------------------------------------------------------
.widget-news {
background-color: $background-color;
vertical-align: baseline !important;
.headline-container {
display: none;
}
.heading span {
color: black;
}
.heading img {
margin-top: -5px;
}
.description {
font-size: 0.8em;
}
.heading{
font-size: 1.2em;
padding: 20px;
}
}
@gordhawkins
Copy link

I'm trying to get this working with Google News... all good except for the text that is returned by "//channel/item" is pretty messy.

Thoughts?

@willjohnson
Copy link

I'm receiving the following error when trying to run this on CentOS (though it worked fine on Ubuntu) with Ruby version 1.9.3-p327:

news.rb:3:in `initialize': uninitialized constant BbcNews::Net (NameError)

@moollaza
Copy link

moollaza commented Nov 3, 2013

@willjohnson it's because news.rb is missing require 'nokogiri' at the top. That will fix it :)

@jtheoof
Copy link

jtheoof commented Jan 26, 2014

Also require 'net/http' is missing on top.

@fjfo
Copy link

fjfo commented Jul 21, 2016

bundler: failed to load command: thin (/usr/local/bin/thin)
LoadError: cannot load such file -- nokogiri

@manifolded
Copy link

I'm trying to convert this over to work TechCrunch's RSS feed with mixed success. Their feed includes image links in the descriptions. How can we filter these out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment