-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MichMich/v2-beta
Update V2 beta from master
- Loading branch information
Showing
9 changed files
with
312 additions
and
192 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
node_modules | ||
/node_modules | ||
|
||
!/modules/node_helper | ||
!/modules/node_helper/** |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* Magic Mirror | ||
* Fetcher | ||
* | ||
* By Michael Teeuw http://michaelteeuw.nl | ||
* MIT Licensed. | ||
*/ | ||
|
||
var NewsFetcher = require('./newsfetcher.js'); | ||
|
||
/* Fetcher | ||
* Responsible for requesting an update on the set interval and broadcasting the data. | ||
* | ||
* attribute url string - URL of the news feed. | ||
* attribute reloadInterval number - Reload interval in milliseconds. | ||
*/ | ||
|
||
var Fetcher = function(url, reloadInterval) { | ||
var self = this; | ||
var newsFetcher = new NewsFetcher(); | ||
if (reloadInterval < 1000) { | ||
reloadInterval = 1000; | ||
} | ||
|
||
var reloadTimer = null; | ||
var items = []; | ||
|
||
var fetchFailedCallback = function() {}; | ||
var itemsReceivedCallback = function() {}; | ||
|
||
/* private methods */ | ||
|
||
/* fetchNews() | ||
* Request the new items from the newsFetcher. | ||
*/ | ||
|
||
var fetchNews = function() { | ||
//console.log('Fetch news.'); | ||
clearTimeout(reloadTimer); | ||
reloadTimer = null; | ||
newsFetcher.fetchNews(url, function(fetchedItems) { | ||
items = fetchedItems; | ||
self.broadcastItems(); | ||
scheduleTimer(); | ||
}, function(error) { | ||
fetchFailedCallback(self, error); | ||
scheduleTimer(); | ||
}); | ||
}; | ||
|
||
/* scheduleTimer() | ||
* Schedule the timer for the next update. | ||
*/ | ||
|
||
var scheduleTimer = function() { | ||
//console.log('Schedule update timer.'); | ||
clearTimeout(reloadTimer); | ||
reloadTimer = setTimeout(function() { | ||
fetchNews(); | ||
}, reloadInterval); | ||
}; | ||
|
||
/* public methods */ | ||
|
||
/* setReloadInterval() | ||
* Update the reload interval, but only if we need to increase the speed. | ||
* | ||
* attribute interval number - Interval for the update in milliseconds. | ||
*/ | ||
this.setReloadInterval = function(interval) { | ||
if (interval > 1000 && interval < reloadInterval) { | ||
reloadInterval = interval; | ||
} | ||
}; | ||
|
||
/* startFetch() | ||
* Initiate fetchNews(); | ||
*/ | ||
this.startFetch = function() { | ||
fetchNews(); | ||
}; | ||
|
||
/* broadcastItems() | ||
* Broadcast the exsisting items. | ||
*/ | ||
this.broadcastItems = function() { | ||
if (items.length <= 0) { | ||
//console.log('No items to broadcast yet.'); | ||
return; | ||
} | ||
//console.log('Broadcasting ' + items.length + ' items.'); | ||
itemsReceivedCallback(self); | ||
}; | ||
|
||
this.onReceive = function(callback) { | ||
itemsReceivedCallback = callback; | ||
}; | ||
|
||
this.onError = function(callback) { | ||
fetchFailedCallback = callback; | ||
}; | ||
|
||
this.url = function() { | ||
return url; | ||
}; | ||
|
||
this.items = function() { | ||
return items; | ||
}; | ||
}; | ||
|
||
module.exports = Fetcher; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* Magic Mirror | ||
* NewsFetcher | ||
* | ||
* By Michael Teeuw http://michaelteeuw.nl | ||
* MIT Licensed. | ||
*/ | ||
|
||
var FeedMe = require('feedme'); | ||
var request = require('request'); | ||
|
||
var NewsFetcher = function() { | ||
var self = this; | ||
|
||
self.successCallback = function(){}; | ||
self.errorCallback = function(){}; | ||
|
||
self.items = []; | ||
|
||
var parser = new FeedMe(); | ||
|
||
parser.on('item', function(item) { | ||
//console.log(item); | ||
self.items.push({ | ||
title: item.title, | ||
pubdate: item.pubdate, | ||
}); | ||
}); | ||
|
||
parser.on('end', function(item) { | ||
self.successCallback(self.items); | ||
}); | ||
|
||
parser.on('error', function(error) { | ||
self.errorCallback(error); | ||
}); | ||
|
||
/* public methods */ | ||
|
||
/* fetchNews() | ||
* Fetch the new news items. | ||
* | ||
* attribute url string - The url to fetch. | ||
* attribute success function(items) - Callback on succes. | ||
* attribute error function(error) - Callback on error. | ||
*/ | ||
self.fetchNews = function(url, success, error) { | ||
self.successCallback = success; | ||
self.errorCallback = error; | ||
request(url).pipe(parser); | ||
}; | ||
}; | ||
|
||
module.exports = NewsFetcher; |
Oops, something went wrong.