Skip to content

Instantly share code, notes, and snippets.

@toddq
Forked from iainjmitchell/README.md
Last active October 13, 2022 07:03
Show Gist options
  • Save toddq/5422482 to your computer and use it in GitHub Desktop.
Save toddq/5422482 to your computer and use it in GitHub Desktop.

##Description Simple Dashing widget (and associated job) to display RSS feeds. Based on Iaian Mitchell's News widget.

##Screenshot

##Dependencies nokogiri

htmlentities

Add it to dashing's gemfile:

gem 'nokogiri'
gem 'htmlentities'

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="seattle-times" data-view="News" data-title="Seattle Times" data-interval="30"></div>
</li>

At the top of the news.rb job file, add any rss feed urls you want fetched to the hash, and the widget id to send data to. In the dashboard layout file, the optional data-interval binding can be used to specify how frequently to rotate the news items.

class Dashing.News extends Dashing.Widget
ready: ->
@currentIndex = 0
@headlineElem = $(@node).find('.headline-container')
@nextComment()
@startCarousel()
onData: (data) ->
@currentIndex = 0
startCarousel: ->
interval = $(@node).attr('data-interval')
interval = "30" if not interval
setInterval(@nextComment, parseInt( interval ) * 1000)
nextComment: =>
headlines = @get('headlines')
if headlines
@headlineElem.fadeOut =>
@currentIndex = (@currentIndex + 1) % headlines.length
@set 'current_headline', headlines[@currentIndex]
@headlineElem.fadeIn()
<div class="heading" data-bind="title"></div>
<div class="headline-container">
<h1 class="title" data-bind="current_headline.title | raw"></h1>
<div class="headline description" data-bind="current_headline.description | raw"></div>
</div>
require 'net/http'
require 'uri'
require 'nokogiri'
require 'htmlentities'
news_feeds = {
"seattle-times" => "http://seattletimes.com/rss/home.xml",
}
Decoder = HTMLEntities.new
class News
def initialize(widget_id, feed)
@widget_id = widget_id
# pick apart feed into domain and path
uri = URI.parse(feed)
@path = uri.path
@http = Net::HTTP.new(uri.host)
end
def widget_id()
@widget_id
end
def latest_headlines()
response = @http.request(Net::HTTP::Get.new(@path))
doc = Nokogiri::XML(response.body)
news_headlines = [];
doc.xpath('//channel/item').each do |news_item|
title = clean_html( news_item.xpath('title').text )
summary = clean_html( news_item.xpath('description').text )
news_headlines.push({ title: title, description: summary })
end
news_headlines
end
def clean_html( html )
html = html.gsub(/<\/?[^>]*>/, "")
html = Decoder.decode( html )
return html
end
end
@News = []
news_feeds.each do |widget_id, feed|
begin
@News.push(News.new(widget_id, feed))
rescue Exception => e
puts e.to_s
end
end
SCHEDULER.every '60m', :first_in => 0 do |job|
@News.each do |news|
headlines = news.latest_headlines()
send_event(news.widget_id, { :headlines => headlines })
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #47bbb3;
$value-color: #fff;
$title-color: rgba(255, 255, 255, 0.7);;
$moreinfo-color: rgba(255, 255, 255, 0.8);;
// ----------------------------------------------------------------------------
// Widget-weather styles
// ----------------------------------------------------------------------------
.widget-news {
background-color: $background-color;
vertical-align: baseline !important;
.headline-container {
display: none;
}
.description {
font-size: 0.8em;
color: $moreinfo-color;
}
.heading{
color: black;
}
}
@truemonolith
Copy link

this has trouble reading a lot of websites.

@lukebo
Copy link

lukebo commented Nov 15, 2014

Dear Todd,

thanks a lot for this great widget. I know, it is pritty unusual: How would one load an RSS-feed, that requires username and password? I tried
news_feeds = {
"feed" => "https://username:pass@url.to.feed/feed.xml",
}
That works pretty well in firefox but in this context, it doesn't. The feed is provided by a joomla site, that contains internal information for our organization....

Best regards

Luke

@pauldsmyth
Copy link

Why does the data-id in the dashboard snippet not match the data being sent by the job?

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